Code cleanup
This commit is contained in:
parent
2625de19dc
commit
bf3b77ddbd
@ -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):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user