Refactor charging function (#702)

* 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.
This commit is contained in:
dementive 2024-05-25 11:09:54 -04:00 committed by GitHub
parent 487c344da3
commit 3f6d7a3e77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 33 additions and 39 deletions

View File

@ -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
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
# we found some power supplies, lets check their state
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
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