Skip to content

Commit

Permalink
Fix type checking in Linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kentzo committed Nov 14, 2012
1 parent 0c768e1 commit 9630adf
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions power/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def power_source_type(supply_path):
"""
@param supply_path: Path to power supply
@return: One of common.POWER_TYPE_*
@raise: Runtime error if type of power source is not supported.
@raise: Runtime error if type of power source is not supported
"""
with open(os.path.join(supply_path, 'type'), 'r') as type_file:
type = type_file.readline().strip()
Expand Down Expand Up @@ -88,12 +88,14 @@ def get_providing_power_source_type(self):
supply_path = os.path.join(POWER_SUPPLY_PATH, supply)
try:
type = self.power_source_type(supply_path)
if type == common.POWER_TYPE_AC and self.is_ac_online(supply_path):
return common.POWER_TYPE_AC
elif type == common.POWER_TYPE_BATTERY and self.is_battery_present(supply_path) and self.is_battery_discharging(supply_path):
return common.POWER_TYPE_BATTERY
if type == common.POWER_TYPE_AC:
if self.is_ac_online(supply_path):
return common.POWER_TYPE_AC
elif type == common.POWER_TYPE_BATTERY:
if self.is_battery_present(supply_path) and self.is_battery_discharging(supply_path):
return common.POWER_TYPE_BATTERY
else:
warnings.warn("%s is not supported.".format(type))
warnings.warn("UPS is not supported.")
except (RuntimeError, IOError) as e:
warnings.warn("Unable to read properties of {path}: {error}".format(path=supply_path, error=str(e)))

Expand All @@ -112,13 +114,15 @@ def get_low_battery_warning_level(self):
supply_path = os.path.join(POWER_SUPPLY_PATH, supply)
try:
type = self.power_source_type(supply_path)
if type == common.POWER_TYPE_AC and self.is_ac_online(supply_path):
return common.LOW_BATTERY_WARNING_NONE
elif type == common.POWER_TYPE_BATTERY and self.is_battery_present(supply_path) and self.is_battery_discharging(supply_path):
energy_full, energy_now, power_now = self.get_battery_state(supply_path)
all_energy_full.append(energy_full)
all_energy_now.append(energy_now)
all_power_now.append(power_now)
if type == common.POWER_TYPE_AC:
if self.is_ac_online(supply_path):
return common.LOW_BATTERY_WARNING_NONE
elif type == common.POWER_TYPE_BATTERY:
if self.is_battery_present(supply_path) and self.is_battery_discharging(supply_path):
energy_full, energy_now, power_now = self.get_battery_state(supply_path)
all_energy_full.append(energy_full)
all_energy_now.append(energy_now)
all_power_now.append(power_now)
else:
warnings.warn("UPS is not supported.")
except (RuntimeError, IOError) as e:
Expand Down Expand Up @@ -148,18 +152,18 @@ def get_time_remaining_estimate(self):
supply_path = os.path.join(POWER_SUPPLY_PATH, supply)
try:
type = self.power_source_type(supply_path)
if type == common.POWER_TYPE_AC and self.is_ac_online(supply_path):
return common.TIME_REMAINING_UNLIMITED
elif type == common.POWER_TYPE_BATTERY and self.is_battery_present(supply_path) and self.is_battery_discharging(supply_path):
energy_full, energy_now, power_now = self.get_battery_state(supply_path)
all_energy_now.append(energy_now)
all_power_now.append(power_now)
if type == common.POWER_TYPE_AC:
if self.is_ac_online(supply_path):
return common.TIME_REMAINING_UNLIMITED
elif type == common.POWER_TYPE_BATTERY:
if self.is_battery_present(supply_path) and self.is_battery_discharging(supply_path):
energy_full, energy_now, power_now = self.get_battery_state(supply_path)
all_energy_now.append(energy_now)
all_power_now.append(power_now)
else:
warnings.warn("%s is not supported.".format(type))
except IOError as e:
print "Unable to read properties of %s: %s".format(supply_path, str(e))
except RuntimeError as e:
warnings.warn(str(e))
warnings.warn("UPS is not supported.")
except (RuntimeError, IOError) as e:
warnings.warn("Unable to read properties of {path}: {error}".format(path=supply_path, error=str(e)))

if len(all_energy_now) > 0:
try:
Expand Down

0 comments on commit 9630adf

Please sign in to comment.