diff --git a/auto_cpufreq/core.py b/auto_cpufreq/core.py index 2098a1b..9bfee80 100755 --- a/auto_cpufreq/core.py +++ b/auto_cpufreq/core.py @@ -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