Skip to content

Commit 7572f9f

Browse files
authored
chore: improve codebase using SIM (#288)
Ref: https://docs.astral.sh/ruff/rules/#flake8-simplify-sim
1 parent 195c6ff commit 7572f9f

File tree

2 files changed

+33
-106
lines changed

2 files changed

+33
-106
lines changed

adbe/adb_enhanced.py

Lines changed: 23 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,7 @@ def get_current_rotation_direction() -> int:
214214

215215

216216
def handle_layout(value: bool):
217-
if value:
218-
cmd = "setprop debug.layout true"
219-
else:
220-
cmd = "setprop debug.layout false"
217+
cmd = "setprop debug.layout true" if value else "setprop debug.layout false"
221218
execute_adb_shell_command_and_poke_activity_service(cmd)
222219

223220

@@ -306,10 +303,7 @@ def get_battery_saver_state() -> str:
306303
@partial(print_state_change_decorator, title="Battery saver", get_state_func=get_battery_saver_state)
307304
def handle_battery_saver(turn_on: bool):
308305
_error_if_min_version_less_than(19)
309-
if turn_on:
310-
cmd = "put global low_power 1"
311-
else:
312-
cmd = "put global low_power 0"
306+
cmd = "put global low_power 1" if turn_on else "put global low_power 0"
313307

314308
if turn_on:
315309
return_code, _, _ = execute_adb_shell_command2(get_battery_unplug_cmd())
@@ -435,7 +429,7 @@ def handle_list_devices():
435429
_print_device_info(device_serial)
436430

437431

438-
def _get_device_serials() -> [str]:
432+
def _get_device_serials() -> list[str]:
439433
cmd = "devices -l"
440434
return_code, stdout, stderr = execute_adb_command2(cmd)
441435
if return_code != 0:
@@ -475,16 +469,17 @@ def _print_device_info(device_serial=None):
475469
model = get_adb_shell_property("ro.product.model", device_serial=device_serial)
476470
# This worked on 4.4.3 API 19 Moto E
477471
display_name = get_adb_shell_property("ro.product.display", device_serial=device_serial)
472+
478473
# First fallback: undocumented
479-
if not display_name or display_name == "null":
474+
if ((not display_name or display_name == "null")
475+
and get_device_android_api_version(device_serial=device_serial) >= 19):
480476
# This works on 4.4.4 API 19 Galaxy Grand Prime
481-
if get_device_android_api_version(device_serial=device_serial) >= 19:
482-
display_name = execute_adb_shell_settings_command("get system device_name", device_serial=device_serial)
477+
display_name = execute_adb_shell_settings_command("get system device_name", device_serial=device_serial)
478+
483479
# Second fallback, documented to work on API 25 and above
484480
# Source: https://developer.android.com/reference/android/provider/Settings.Global.html#DEVICE_NAME
485-
if not display_name or display_name == "null":
486-
if get_device_android_api_version(device_serial=device_serial) >= 25:
487-
display_name = execute_adb_shell_settings_command("get global device_name", device_serial=device_serial)
481+
if (not display_name or display_name == "null") and get_device_android_api_version(device_serial=device_serial) >= 25:
482+
display_name = execute_adb_shell_settings_command("get global device_name", device_serial=device_serial)
488483

489484
# ABI info
490485
abi = get_adb_shell_property("ro.product.cpu.abi", device_serial=device_serial)
@@ -595,10 +590,7 @@ def get_wifi_state() -> str:
595590

596591
@partial(print_state_change_decorator, title="Wi-Fi", get_state_func=get_wifi_state)
597592
def set_wifi(turn_on: bool):
598-
if turn_on:
599-
cmd = "svc wifi enable"
600-
else:
601-
cmd = "svc wifi disable"
593+
cmd = "svc wifi enable" if turn_on else "svc wifi disable"
602594
return_code, _, _ = execute_adb_shell_command2(cmd)
603595
if return_code != 0:
604596
print_error_and_exit("Failed to change Wi-Fi setting")
@@ -608,21 +600,15 @@ def set_wifi(turn_on: bool):
608600
# https://stackoverflow.com/questions/26539445/the-setmobiledataenabled-method-is-no-longer-callable-as-of-android-l-and-later
609601
@partial(print_state_change_decorator, title="Mobile data", get_state_func=get_mobile_data_state)
610602
def handle_mobile_data(turn_on: bool):
611-
if turn_on:
612-
cmd = "svc data enable"
613-
else:
614-
cmd = "svc data disable"
603+
cmd = "svc data enable" if turn_on else "svc data disable"
615604
return_code, _, _ = execute_adb_shell_command2(cmd)
616605
if return_code != 0:
617606
print_error_and_exit("Failed to change mobile data setting")
618607

619608

620609
def force_rtl(turn_on: bool):
621610
_error_if_min_version_less_than(19)
622-
if turn_on:
623-
cmd = "put global debug.force_rtl 1"
624-
else:
625-
cmd = "put global debug.force_rtl 0"
611+
cmd = "put global debug.force_rtl 1" if turn_on else "put global debug.force_rtl 0"
626612
execute_adb_shell_settings_command_and_poke_activity_service(cmd)
627613

628614

@@ -710,10 +696,7 @@ def get_mobile_data_saver_state():
710696
# https://developer.android.com/training/basics/network-ops/data-saver.html
711697
@partial(print_state_change_decorator, title="Mobile data saver", get_state_func=get_mobile_data_saver_state)
712698
def handle_mobile_data_saver(turn_on: bool):
713-
if turn_on:
714-
cmd = "cmd netpolicy set restrict-background true"
715-
else:
716-
cmd = "cmd netpolicy set restrict-background false"
699+
cmd = "cmd netpolicy set restrict-background true" if turn_on else "cmd netpolicy set restrict-background false"
717700
return_code, _, _ = execute_adb_shell_command2(cmd)
718701
if return_code != 0:
719702
print_error_and_exit("Failed to modify data saver mode setting")
@@ -759,10 +742,7 @@ def handle_dont_keep_activities_in_background(turn_on: bool):
759742

760743

761744
def toggle_animations(turn_on: bool):
762-
if turn_on:
763-
value = 1
764-
else:
765-
value = 0
745+
value = 1 if turn_on else 0
766746

767747
# Source: https://github.com/jaredsburrows/android-gif-example/blob/824c493285a2a2cf22f085662431cf0a7aa204b8/.travis.yml#L34
768748
cmd1 = "put global window_animation_scale %d" % value
@@ -791,10 +771,7 @@ def get_show_taps_state():
791771

792772
@partial(print_state_change_decorator, title="Show user taps", get_state_func=get_show_taps_state)
793773
def toggle_show_taps(turn_on: bool):
794-
if turn_on:
795-
value = 1
796-
else:
797-
value = 0
774+
value = 1 if turn_on else 0
798775

799776
# Source: https://stackoverflow.com/a/32621809/434196
800777
cmd = "put system show_touches %d" % value
@@ -820,12 +797,8 @@ def get_stay_awake_while_charging_state():
820797
title="Stay awake while charging",
821798
get_state_func=get_stay_awake_while_charging_state)
822799
def stay_awake_while_charging(turn_on: bool):
823-
if turn_on:
824-
# 1 for USB charging, 2 for AC charging, 4 for wireless charging. Or them together to get 7.
825-
value = 7
826-
else:
827-
value = 0
828-
800+
# 1 for USB charging, 2 for AC charging, 4 for wireless charging. Add them together to get 7.
801+
value = 7 if turn_on else 0
829802
cmd1 = "put global stay_on_while_plugged_in %d" % value
830803
execute_adb_shell_settings_command_and_poke_activity_service(cmd1)
831804

@@ -868,11 +841,7 @@ def list_permission_groups():
868841

869842
def list_permissions(dangerous_only_permissions: bool):
870843
# -g is to group permissions by permission groups.
871-
if dangerous_only_permissions:
872-
# -d => dangerous only permissions
873-
cmd = "pm list permissions -g -d"
874-
else:
875-
cmd = "pm list permissions -g"
844+
cmd = "pm list permissions -g -d" if dangerous_only_permissions else "pm list permissions -g"
876845
return_code, stdout, stderr = execute_adb_shell_command2(cmd)
877846
if return_code != 0:
878847
print_error_and_exit("Failed to list permissions: (stdout: %s, stderr: %s)" % (stdout, stderr))
@@ -1488,10 +1457,7 @@ def print_app_info(app_name):
14881457
def _get_permissions_info_below_api_23(app_info_dump):
14891458
install_time_permissions_regex = re.search(r"grantedPermissions:(.*)", app_info_dump,
14901459
re.IGNORECASE | re.DOTALL)
1491-
if install_time_permissions_regex is None:
1492-
install_time_permissions_string = []
1493-
else:
1494-
install_time_permissions_string = install_time_permissions_regex.group(1).split("\n")
1460+
install_time_permissions_string = [] if install_time_permissions_regex is None else install_time_permissions_regex.group(1).split("\n")
14951461

14961462
install_time_permissions_string = filter(None, install_time_permissions_string)
14971463
install_time_granted_permissions = list(install_time_permissions_string)
@@ -1912,15 +1878,9 @@ def print_notifications():
19121878
output_for_this_notification = output_for_this_notification.split(notification_records[i + 1])[0]
19131879
notification_package = re.findall(r"pkg=(\S*)", notification_record)[0]
19141880
titles = re.findall(r"android.title=(.*)", output_for_this_notification)
1915-
if len(titles) > 0:
1916-
notification_title = titles[0]
1917-
else:
1918-
notification_title = None
1881+
notification_title = titles[0] if len(titles) > 0 else None
19191882
texts = re.findall(r"android.text=(.*)", output_for_this_notification)
1920-
if len(texts) > 0:
1921-
notification_text = texts[0]
1922-
else:
1923-
notification_text = None
1883+
notification_text = texts[0] if len(texts) > 0 else None
19241884
notification_actions = []
19251885
action_strings = re.findall(r"actions=\{(.*?)\n\}", output_for_this_notification, re.MULTILINE | re.DOTALL)
19261886
if len(action_strings) > 0 and (
@@ -2079,10 +2039,7 @@ def alarm_manager(param):
20792039

20802040
def toggle_location(turn_on):
20812041
_error_if_min_version_less_than(_MIN_API_FOR_LOCATION)
2082-
if turn_on:
2083-
cmd = "put secure location_mode 3"
2084-
else:
2085-
cmd = "put secure location_mode 0"
2042+
cmd = "put secure location_mode 3" if turn_on else "put secure location_mode 0"
20862043
execute_adb_shell_settings_command(cmd)
20872044

20882045

tests/adbe_tests.py

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ def test_binary(testpythoninstallation):
5454

5555

5656
def test_rotate():
57-
if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION:
58-
check = _assert_success
59-
else:
60-
check = _assert_fail
57+
check = _assert_success if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION else _assert_fail
6158

6259
check("rotate landscape")
6360
check("rotate portrait")
@@ -87,10 +84,7 @@ def test_layout():
8784

8885

8986
def test_airplane():
90-
if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION:
91-
check = _assert_success
92-
else:
93-
check = _assert_fail
87+
check = _assert_success if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION else _assert_fail
9488

9589
check("airplane on")
9690
check("airplane off")
@@ -100,10 +94,7 @@ def test_battery_sub_cmds():
10094
_assert_fail("battery level -1")
10195
_assert_fail("battery level 104")
10296

103-
if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION:
104-
check = _assert_success
105-
else:
106-
check = _assert_fail
97+
check = _assert_success if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION else _assert_fail
10798

10899
check("battery level 10")
109100
check("battery saver on")
@@ -112,20 +103,14 @@ def test_battery_sub_cmds():
112103

113104

114105
def test_dark_mode():
115-
if _get_device_sdk_version() >= _DARK_MODE_ANDROID_VERSION:
116-
check = _assert_success
117-
else:
118-
check = _assert_fail
106+
check = _assert_success if _get_device_sdk_version() >= _DARK_MODE_ANDROID_VERSION else _assert_fail
119107

120108
check("dark mode on")
121109
check("dark mode off")
122110

123111

124112
def test_doze():
125-
if _get_device_sdk_version() >= _DOZE_MODE_ANDROID_VERSION:
126-
check = _assert_success
127-
else:
128-
check = _assert_fail
113+
check = _assert_success if _get_device_sdk_version() >= _DOZE_MODE_ANDROID_VERSION else _assert_fail
129114

130115
check("doze on")
131116
check("doze off")
@@ -139,20 +124,14 @@ def test_mobile_data():
139124

140125

141126
def test_rtl():
142-
if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION:
143-
check = _assert_success
144-
else:
145-
check = _assert_fail
127+
check = _assert_success if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION else _assert_fail
146128

147129
check("rtl on")
148130
check("rtl off")
149131

150132

151133
def test_animations():
152-
if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION:
153-
check = _assert_success
154-
else:
155-
check = _assert_fail
134+
check = _assert_success if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION else _assert_fail
156135

157136
check("animations on")
158137
check("animations off")
@@ -395,10 +374,7 @@ def test_take_screenshot():
395374

396375

397376
def test_keep_activities():
398-
if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION:
399-
check = _assert_success
400-
else:
401-
check = _assert_fail
377+
check = _assert_success if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION else _assert_fail
402378

403379
check("dont-keep-activities on")
404380
check("dont-keep-activities off")
@@ -409,10 +385,7 @@ def test_ls():
409385

410386

411387
def test_stay_awake_while_charging():
412-
if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION:
413-
check = _assert_success
414-
else:
415-
check = _assert_fail
388+
check = _assert_success if _get_device_sdk_version() >= _SETTINGS_CMD_VERSION else _assert_fail
416389

417390
check("stay-awake-while-charging on")
418391
# This causes Circle CI to hang.
@@ -457,10 +430,7 @@ def test_notifications():
457430

458431

459432
def test_location():
460-
if _get_device_sdk_version() >= _LOCATION_CHANGE_ANDROID_VERSION:
461-
check = _assert_success
462-
else:
463-
check = _assert_fail
433+
check = _assert_success if _get_device_sdk_version() >= _LOCATION_CHANGE_ANDROID_VERSION else _assert_fail
464434
check("location on")
465435
check("location off")
466436

0 commit comments

Comments
 (0)