Skip to content

Commit 65d2784

Browse files
authored
chore: minor code clean up to modernize code base (#302)
1 parent efa722c commit 65d2784

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

adbe/adb_enhanced.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,10 @@ def _get_device_serials() -> list[str]:
454454
continue
455455
device_serial = device_info.split()[0]
456456
if "unauthorized" in device_info:
457-
device_info = " ".join(device_info.split()[1:])
458457
print_error(
459458
f'Unlock Device "{device_serial}" and give USB debugging access to '
460459
"this PC/Laptop by unlocking and reconnecting "
461-
f'the device. More info about this device: "{device_info}"\n')
460+
f'the device. More info about this device: "{" ".join(device_info.split()[1:])}"\n')
462461
else:
463462
device_serials.append(device_serial)
464463
return device_serials
@@ -503,8 +502,7 @@ def _get_top_activity_data() -> tuple[None, None]:
503502
if return_code != 0 and not output:
504503
print_error_and_exit("Device returned no response, is it still connected?")
505504
for line in output.split("\n"):
506-
line = line.strip()
507-
regex_result = re.search(r"ActivityRecord{.* (\S+)/(\S+)", line)
505+
regex_result = re.search(r"ActivityRecord{.* (\S+)/(\S+)", line.strip())
508506
if regex_result is None:
509507
continue
510508
app_name, activity_name = regex_result.group(1), regex_result.group(2)
@@ -1001,18 +999,20 @@ def get_permissions_in_permission_group(permission_group: str) -> list[str] | li
1001999

10021000

10031001
@ensure_package_exists
1004-
def grant_or_revoke_runtime_permissions(package_name: str, action_grant: bool, permissions: list[str]) -> None:
1002+
def grant_or_revoke_runtime_permissions(package_name: str, action_type: Literal["grant", "revoke"], permissions: list[str]) -> None:
10051003
_error_if_min_version_less_than(23)
10061004

10071005
app_info_dump = execute_adb_shell_command(f"dumpsys package {package_name}")
10081006
permissions_formatted_dump = _get_permissions_info_above_api_23(app_info_dump).split("\n")
10091007

1010-
if action_grant:
1008+
if action_type == "grant":
10111009
base_cmd = f"pm grant {package_name}"
1012-
action_display_name = "Granting"
1013-
else:
1010+
elif action_type == "revoke":
10141011
base_cmd = f"pm revoke {package_name}"
1015-
action_display_name = "Revoking"
1012+
else:
1013+
print_error_and_exit(f"Invalid action type: {action_type}")
1014+
return
1015+
10161016
num_permissions_granted = 0
10171017
for permission in permissions:
10181018
if permission not in permissions_formatted_dump:
@@ -1021,7 +1021,7 @@ def grant_or_revoke_runtime_permissions(package_name: str, action_grant: bool, p
10211021
if permission == "android.permission.POST_NOTIFICATIONS":
10221022
_error_if_min_version_less_than(33)
10231023
num_permissions_granted += 1
1024-
print_message(f"{action_display_name} {permission} permission to {package_name}")
1024+
print_message(f"{action_type} {permission} permission to {package_name}")
10251025
execute_adb_shell_command(base_cmd + " " + permission)
10261026
if num_permissions_granted == 0:
10271027
print_error_and_exit(f"None of these permissions were granted to {package_name}: {permissions}")
@@ -1604,11 +1604,9 @@ def print_app_signature(app_name: str) -> None:
16041604
print_verbose(f"Executing command {print_signature_cmd}")
16051605
with subprocess.Popen(print_signature_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as ps1:
16061606
for line in ps1.stdout:
1607-
line = line.decode("utf-8").strip()
1608-
print_message(line)
1607+
print_message(line.decode("utf-8").strip())
16091608
for line in ps1.stderr:
1610-
line = line.decode("utf-8").strip()
1611-
print_error(line)
1609+
print_error(line.decode("utf-8").strip())
16121610

16131611

16141612
# Uses abe.jar taken from https://sourceforge.net/projects/adbextractor/

adbe/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def _grant_revoke_permissions(app_name: str, args: dict[str, typing.Any]) -> Non
317317
elif not permissions:
318318
print_error_and_exit(f"No permissions found in permissions group: {permission_group}")
319319
adb_enhanced.grant_or_revoke_runtime_permissions(
320-
app_name, args["grant"], permissions)
320+
app_name, action_type="grant" if args["grant"] else "revoke", permissions=permissions)
321321

322322

323323
def _perform_backup(app_name: str, backup_tar_file_path: str | None) -> None:

0 commit comments

Comments
 (0)