Skip to content

Commit 6f7e469

Browse files
authored
Code refactoring, more readable and easier to modify (#736)
* Add global variables * Add global variables * Code refactoring * Refactoring
1 parent 34ebd04 commit 6f7e469

20 files changed

+456
-1002
lines changed

auto_cpufreq/__init__.py

Whitespace-only changes.
+13-13
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
#!/usr/bin/env python3
2-
import subprocess
2+
from subprocess import PIPE, run
33

4-
from auto_cpufreq.battery_scripts.thinkpad import thinkpad_setup, thinkpad_print_thresholds
5-
from auto_cpufreq.battery_scripts.ideapad_acpi import ideapad_acpi_setup, ideapad_acpi_print_thresholds
6-
from auto_cpufreq.battery_scripts.ideapad_laptop import ideapad_laptop_setup, ideapad_laptop_print_thresholds
4+
from auto_cpufreq.battery_scripts.ideapad_acpi import ideapad_acpi_print_thresholds, ideapad_acpi_setup
5+
from auto_cpufreq.battery_scripts.ideapad_laptop import ideapad_laptop_print_thresholds, ideapad_laptop_setup
6+
from auto_cpufreq.battery_scripts.thinkpad import thinkpad_print_thresholds, thinkpad_setup
77

8-
def lsmod(module): return module in subprocess.run(['lsmod'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True).stdout
9-
10-
def battery_setup():
11-
if lsmod("thinkpad_acpi"): thinkpad_setup()
12-
elif lsmod("ideapad_acpi"): ideapad_acpi_setup()
13-
elif lsmod("ideapad_laptop"): ideapad_laptop_setup()
14-
else: return
8+
def lsmod(module): return module in run(['lsmod'], stdout=PIPE, stderr=PIPE, text=True).stdout
159

1610
def battery_get_thresholds():
17-
if lsmod("thinkpad_acpi"): thinkpad_print_thresholds()
18-
elif lsmod("ideapad_acpi"): ideapad_acpi_print_thresholds()
11+
if lsmod("ideapad_acpi"): ideapad_acpi_print_thresholds()
1912
elif lsmod("ideapad_laptop"): ideapad_laptop_print_thresholds()
13+
elif lsmod("thinkpad_acpi"): thinkpad_print_thresholds()
14+
else: return
15+
16+
def battery_setup():
17+
if lsmod("ideapad_acpi"): ideapad_acpi_setup()
18+
elif lsmod("ideapad_laptop"): ideapad_laptop_setup()
19+
elif lsmod("thinkpad_acpi"): thinkpad_setup()
2020
else: return

auto_cpufreq/battery_scripts/ideapad_acpi.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/usr/bin/env python3
2-
import os
3-
import subprocess
4-
from auto_cpufreq.utils.config import config
2+
import os, subprocess
53

6-
POWER_SUPPLY_DIR = "/sys/class/power_supply/"
4+
from auto_cpufreq.config.config import config
5+
from auto_cpufreq.globals import POWER_SUPPLY_DIR
76

87
def set_battery(value, mode, bat):
98
path = f"{POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold"
@@ -25,7 +24,7 @@ def ideapad_acpi_setup():
2524
for bat in batteries:
2625
set_battery(get_threshold_value("start"), "start", bat)
2726
set_battery(get_threshold_value("stop"), "stop", bat)
28-
else: print(f"WARNING: could NOT access {POWER_SUPPLY_DIR}")
27+
else: print("WARNING: could NOT access", POWER_SUPPLY_DIR)
2928

3029
def ideapad_acpi_print_thresholds():
3130
batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')]
@@ -35,4 +34,4 @@ def ideapad_acpi_print_thresholds():
3534
try:
3635
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_start_threshold")}')
3736
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_stop_threshold")}')
38-
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds: ", repr(e))
37+
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds:", repr(e))

auto_cpufreq/battery_scripts/ideapad_laptop.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/usr/bin/env python3
2-
import os
3-
import subprocess
4-
from auto_cpufreq.utils.config import config
2+
import os, subprocess
53

6-
POWER_SUPPLY_DIR = "/sys/class/power_supply/"
4+
from auto_cpufreq.config.config import config
5+
from auto_cpufreq.globals import CONSERVATION_MODE_FILE, POWER_SUPPLY_DIR
76

87
def set_battery(value, mode, bat):
98
path = f"{POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold"
@@ -17,14 +16,14 @@ def get_threshold_value(mode):
1716

1817
def conservation_mode(value):
1918
try:
20-
subprocess.check_output(f"echo {value} | tee /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode", shell=True, text=True)
19+
subprocess.check_output(f"echo {value} | tee {CONSERVATION_MODE_FILE}", shell=True, text=True)
2120
print(f"conservation_mode is {value}")
2221
except: print("unable to set conservation mode")
2322
return
2423

2524
def check_conservation_mode():
2625
try:
27-
value = subprocess.check_output("cat /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode", shell=True, text=True)
26+
value = subprocess.check_output(["cat", CONSERVATION_MODE_FILE], text=True)
2827
if value == "1": return True
2928
elif value == "0": return False
3029
else:
@@ -66,4 +65,4 @@ def ideapad_laptop_print_thresholds():
6665
try:
6766
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_start_threshold")}')
6867
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_stop_threshold")}')
69-
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds: ", repr(e))
68+
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds:", repr(e))

auto_cpufreq/battery_scripts/thinkpad.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/usr/bin/env python3
2-
import os
3-
import subprocess
4-
from auto_cpufreq.utils.config import config
2+
import os, subprocess
53

6-
POWER_SUPPLY_DIR = "/sys/class/power_supply/"
4+
from auto_cpufreq.config.config import config
5+
from auto_cpufreq.globals import POWER_SUPPLY_DIR
76

87
def set_battery(value, mode, bat):
98
path = f"{POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold"
@@ -36,4 +35,4 @@ def thinkpad_print_thresholds():
3635
try:
3736
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_start_threshold")}')
3837
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_stop_threshold")}')
39-
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds: ", repr(e))
38+
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds:", repr(e))

auto_cpufreq/bin/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)