Code cleanup

This commit is contained in:
enkore 2013-02-22 23:21:53 +01:00
parent 2625de19dc
commit bf3b77ddbd

View File

@ -10,21 +10,18 @@ class Battery:
The data from uevent is transformed into attributes of this class, stripping The data from uevent is transformed into attributes of this class, stripping
their prefix. (i.e. POWER_SUPPLY_NAME becomes the NAME attribute). their prefix. (i.e. POWER_SUPPLY_NAME becomes the NAME attribute).
Numbers are automatically converted to floats. Numbers are automatically converted to floats. Strings are stripped.
Strings are stripped.
(configparser would be a bit overkill for this)
""" """
prefix = "POWER_SUPPLY_" @staticmethod
def lchop(string, prefix="POWER_SUPPLY_"):
if string.startswith(prefix):
return string[len(prefix):]
return string
@staticmethod @staticmethod
def lchop(string, substring): def convert(value):
if string.startswith(substring): return float(value) if value.isdecimal() else value.strip()
return string[len(substring):]
return string
def __init__(self, file): def __init__(self, file):
self.parse(file) self.parse(file)
@ -36,13 +33,8 @@ class Battery:
def parse_line(self, line): def parse_line(self, line):
key, value = line.split("=", 2) key, value = line.split("=", 2)
key = self.lchop(key, self.prefix)
value = value.strip()
if value.isdecimal(): setattr(self, self.lchop(key), self.convert(value))
value = float(value)
setattr(self, key, value)
class BatteryChecker(IntervalModule): class BatteryChecker(IntervalModule):
""" """