Skip to content

Commit

Permalink
Refactor charging function
Browse files Browse the repository at this point in the history
* Removed the `else` from the top level of the charging() function, the logic is the same but slightly easier to read now.

* Use os.path.exists() in the charging() function before opening the file instead of FileNotFoundError exceptions, makes the function a lot easier to read.

* Close the power_supply_type_path after reading the supply_type as it is not used later.

* Remove `else: continue` from the end of the charging() function for loop, this didn't actually do anything.

I tested the charging() function on my laptop plugged in and plugged out and it is still working as expected with these changes.
  • Loading branch information
dementive committed May 16, 2024
1 parent 1f911b5 commit 8568e49
Showing 1 changed file with 32 additions and 38 deletions.
70 changes: 32 additions & 38 deletions auto_cpufreq/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,47 +316,41 @@ def charging():
if len(power_supplies) == 0:
# nothing found found, so nothing to check
return True

# we found some power supplies, lets check their state
else:
for supply in power_supplies:
# Check if supply is in ignore list
ignore_supply = any(item in supply for item in POWER_SUPPLY_IGNORELIST)
# If found in ignore list, skip it.
if ignore_supply:
continue
for supply in power_supplies:
# Check if supply is in ignore list
ignore_supply = any(item in supply for item in POWER_SUPPLY_IGNORELIST)
# If found in ignore list, skip it.
if ignore_supply:
continue

try:
with open(Path(power_supply_path + supply + "/type")) as f:
supply_type = f.read()[:-1]
if supply_type == "Mains":
# we found an AC
try:
with open(Path(power_supply_path + supply + "/online")) as f:
val = int(f.read()[:-1])
if val == 1:
# we are definitely charging
return True
except FileNotFoundError:
# we could not find online, check next item
continue
elif supply_type == "Battery":
# we found a battery, check if its being discharged
try:
with open(Path(power_supply_path + supply + "/status")) as f:
val = str(f.read()[:-1])
if val == "Discharging":
# we found a discharging battery
return False
except FileNotFoundError:
# could not find status, check the next item
continue
else:
# continue to next item because current is not
# "Mains" or "Battery"
continue
except FileNotFoundError:
# could not find type, check the next item
power_supply_type_path = Path(power_supply_path + supply + "/type")
if not power_supply_type_path.exists():
continue
with open(power_supply_type_path) as f:
supply_type = f.read()[:-1]

if supply_type == "Mains":
# we found an AC
power_supply_online_path = Path(power_supply_path + supply + "/online")
if not power_supply_online_path.exists():
continue
with open(power_supply_online_path) as f:
val = int(f.read()[:-1])
if val == 1:
# we are definitely charging
return True
elif supply_type == "Battery":
# we found a battery, check if its being discharged
power_supply_status_path = Path(power_supply_path + supply + "/status")
if not power_supply_status_path.exists():
continue
with open(power_supply_status_path) as f:
val = str(f.read()[:-1])
if val == "Discharging":
# we found a discharging battery
return False

# we cannot determine discharging state, assume we are on powercable
return True
Expand Down

0 comments on commit 8568e49

Please sign in to comment.