Skip to content

Commit

Permalink
Code refactoring, more readable and easier to modify (#736)
Browse files Browse the repository at this point in the history
* Add global variables

* Add global variables

* Code refactoring

* Refactoring
  • Loading branch information
Angel-Karasu authored Jul 16, 2024
1 parent 34ebd04 commit 6f7e469
Show file tree
Hide file tree
Showing 20 changed files with 456 additions and 1,002 deletions.
Empty file removed auto_cpufreq/__init__.py
Empty file.
26 changes: 13 additions & 13 deletions auto_cpufreq/battery_scripts/battery.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#!/usr/bin/env python3
import subprocess
from subprocess import PIPE, run

from auto_cpufreq.battery_scripts.thinkpad import thinkpad_setup, thinkpad_print_thresholds
from auto_cpufreq.battery_scripts.ideapad_acpi import ideapad_acpi_setup, ideapad_acpi_print_thresholds
from auto_cpufreq.battery_scripts.ideapad_laptop import ideapad_laptop_setup, ideapad_laptop_print_thresholds
from auto_cpufreq.battery_scripts.ideapad_acpi import ideapad_acpi_print_thresholds, ideapad_acpi_setup
from auto_cpufreq.battery_scripts.ideapad_laptop import ideapad_laptop_print_thresholds, ideapad_laptop_setup
from auto_cpufreq.battery_scripts.thinkpad import thinkpad_print_thresholds, thinkpad_setup

def lsmod(module): return module in subprocess.run(['lsmod'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True).stdout

def battery_setup():
if lsmod("thinkpad_acpi"): thinkpad_setup()
elif lsmod("ideapad_acpi"): ideapad_acpi_setup()
elif lsmod("ideapad_laptop"): ideapad_laptop_setup()
else: return
def lsmod(module): return module in run(['lsmod'], stdout=PIPE, stderr=PIPE, text=True).stdout

def battery_get_thresholds():
if lsmod("thinkpad_acpi"): thinkpad_print_thresholds()
elif lsmod("ideapad_acpi"): ideapad_acpi_print_thresholds()
if lsmod("ideapad_acpi"): ideapad_acpi_print_thresholds()
elif lsmod("ideapad_laptop"): ideapad_laptop_print_thresholds()
elif lsmod("thinkpad_acpi"): thinkpad_print_thresholds()
else: return

def battery_setup():
if lsmod("ideapad_acpi"): ideapad_acpi_setup()
elif lsmod("ideapad_laptop"): ideapad_laptop_setup()
elif lsmod("thinkpad_acpi"): thinkpad_setup()
else: return
11 changes: 5 additions & 6 deletions auto_cpufreq/battery_scripts/ideapad_acpi.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env python3
import os
import subprocess
from auto_cpufreq.utils.config import config
import os, subprocess

POWER_SUPPLY_DIR = "/sys/class/power_supply/"
from auto_cpufreq.config.config import config
from auto_cpufreq.globals import POWER_SUPPLY_DIR

def set_battery(value, mode, bat):
path = f"{POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold"
Expand All @@ -25,7 +24,7 @@ def ideapad_acpi_setup():
for bat in batteries:
set_battery(get_threshold_value("start"), "start", bat)
set_battery(get_threshold_value("stop"), "stop", bat)
else: print(f"WARNING: could NOT access {POWER_SUPPLY_DIR}")
else: print("WARNING: could NOT access", POWER_SUPPLY_DIR)

def ideapad_acpi_print_thresholds():
batteries = [name for name in os.listdir(POWER_SUPPLY_DIR) if name.startswith('BAT')]
Expand All @@ -35,4 +34,4 @@ def ideapad_acpi_print_thresholds():
try:
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_start_threshold")}')
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_stop_threshold")}')
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds: ", repr(e))
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds:", repr(e))
13 changes: 6 additions & 7 deletions auto_cpufreq/battery_scripts/ideapad_laptop.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env python3
import os
import subprocess
from auto_cpufreq.utils.config import config
import os, subprocess

POWER_SUPPLY_DIR = "/sys/class/power_supply/"
from auto_cpufreq.config.config import config
from auto_cpufreq.globals import CONSERVATION_MODE_FILE, POWER_SUPPLY_DIR

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

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

def check_conservation_mode():
try:
value = subprocess.check_output("cat /sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode", shell=True, text=True)
value = subprocess.check_output(["cat", CONSERVATION_MODE_FILE], text=True)
if value == "1": return True
elif value == "0": return False
else:
Expand Down Expand Up @@ -66,4 +65,4 @@ def ideapad_laptop_print_thresholds():
try:
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_start_threshold")}')
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_stop_threshold")}')
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds: ", repr(e))
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds:", repr(e))
9 changes: 4 additions & 5 deletions auto_cpufreq/battery_scripts/thinkpad.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#!/usr/bin/env python3
import os
import subprocess
from auto_cpufreq.utils.config import config
import os, subprocess

POWER_SUPPLY_DIR = "/sys/class/power_supply/"
from auto_cpufreq.config.config import config
from auto_cpufreq.globals import POWER_SUPPLY_DIR

def set_battery(value, mode, bat):
path = f"{POWER_SUPPLY_DIR}{bat}/charge_{mode}_threshold"
Expand Down Expand Up @@ -36,4 +35,4 @@ def thinkpad_print_thresholds():
try:
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_start_threshold")}')
print(f'{bat} start threshold = {subprocess.getoutput(f"cat {POWER_SUPPLY_DIR}{bat}/charge_stop_threshold")}')
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds: ", repr(e))
except Exception as e: print(f"ERROR: failed to read battery {bat} thresholds:", repr(e))
Empty file removed auto_cpufreq/bin/__init__.py
Empty file.
Loading

0 comments on commit 6f7e469

Please sign in to comment.