Adopted modules to new settings system

Btw. nothing has changed for the config files. You can pass into
__init__ a dict as before, or you can use keyword arguments.

Either way, a module defines the settings that can be specified
and those which are required. __init__ basically checks that all
required options are set and that no invalid options are used.
This commit is contained in:
enkore 2013-02-23 00:12:45 +01:00
parent c967cdecb2
commit 612faaaa4e
7 changed files with 41 additions and 46 deletions

View File

@ -42,8 +42,10 @@ class BatteryChecker(IntervalModule):
battery status battery status
""" """
def __init__(self, battery_ident="BAT0"): settings = ("battery_ident",)
self.battery_ident = battery_ident battery_ident = "BAT0"
def init(self):
self.base_path = "/sys/class/power_supply/{0}/uevent".format(self.battery_ident) self.base_path = "/sys/class/power_supply/{0}/uevent".format(self.battery_ident)
def run(self): def run(self):

View File

@ -10,22 +10,24 @@ class Clock(IntervalModule):
This class shows a clock This class shows a clock
""" """
def __init__(self, format_string=None): settings = ("format",)
format = None
def init(self):
lang = os.environ.get('LANG', None) lang = os.environ.get('LANG', None)
if lang: if lang:
locale.setlocale(locale.LC_ALL, lang) locale.setlocale(locale.LC_ALL, lang)
if not format_string: if self.format is not None:
lang = locale.getlocale()[0] lang = locale.getlocale()[0]
if lang == 'en_US': if lang == 'en_US':
# MDY format - United States of America # MDY format - United States of America
format_string = "%a %b %-d %X" self.format = "%a %b %-d %X"
else: else:
# DMY format - almost all other countries # DMY format - almost all other countries
format_string = "%a %-d %b %X" self.format = "%a %-d %b %X"
self.format_string = format_string
def run(self): def run(self):
full_text = datetime.datetime.now().strftime(self.format_string) full_text = datetime.datetime.now().strftime(self.format)
self.output = { self.output = {
"full_text": full_text, "full_text": full_text,
"name": "pyclock", "name": "pyclock",

View File

@ -15,25 +15,22 @@ class MailChecker(IntervalModule):
functionality is implemented in the subclass MailChecker.MailServer functionality is implemented in the subclass MailChecker.MailServer
""" """
settings = { settings = ("color", "servers")
"color": "#ff0000", required = ("servers",)
"servers": [] color = "#ff0000"
}
def __init__(self, settings = None): def init(self):
self.settings.update(settings) self.server_list = list(map(MailChecker.MailServer, self.servers))
self.servers = list(map(MailChecker.MailServer, settings["servers"]))
def run(self): def run(self):
unread = sum(map(lambda server: server.get_unread_count(), self.servers)) unread = sum(map(lambda server: server.get_unread_count(), self.server_list))
if unread: if unread:
self.output = { self.output = {
"full_text" : "%d new email%s" % (unread, ("s" if unread > 1 else "")), "full_text" : "%d new email%s" % (unread, ("s" if unread > 1 else "")),
"name" : "newmail", "name" : "newmail",
"urgent" : "true", "urgent" : "true",
"color" : self.settings["color"] "color" : self.color
} }
class MailServer: class MailServer:

View File

@ -17,20 +17,20 @@ class ModsDeChecker(IntervalModule):
unread posts in any bookmark in the mods.de forums. unread posts in any bookmark in the mods.de forums.
""" """
settings = ("color", "offset", "format", "username", "password")
required = ("username", "password")
color = "#7181fe"
offset = 0
format = "%d new posts in bookmarks"
login_url = "http://login.mods.de/" login_url = "http://login.mods.de/"
bookmark_url = "http://forum.mods.de/bb/xml/bookmarks.php" bookmark_url = "http://forum.mods.de/bb/xml/bookmarks.php"
opener = None opener = None
cj = None cj = None
logged_in = False logged_in = False
settings = { def init(self):
"color": "#7181fe",
"offset": 0,
"format": "%d new posts in bookmarks"
}
def __init__(self, settings = None):
self.settings.update(settings)
self.cj = http.cookiejar.CookieJar() self.cj = http.cookiejar.CookieJar()
self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj)) self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj))
@ -41,10 +41,10 @@ class ModsDeChecker(IntervalModule):
self.output = None self.output = None
else: else:
self.output = { self.output = {
"full_text" : self.settings["format"] % unread, "full_text" : self.format % unread,
"name" : "modsde", "name" : "modsde",
"urgent" : "true", "urgent" : "true",
"color" : self.settings["color"] "color" : self.color
} }
def get_unread_count(self): def get_unread_count(self):
@ -54,7 +54,7 @@ class ModsDeChecker(IntervalModule):
try: try:
f = self.opener.open(self.bookmark_url) f = self.opener.open(self.bookmark_url)
root = ET.fromstring(f.read()) root = ET.fromstring(f.read())
return int(root.attrib["newposts"]) - self.settings["offset"] return int(root.attrib["newposts"]) - self.offset
except Exception: except Exception:
self.cj.clear() self.cj.clear()
self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj)) self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj))
@ -62,8 +62,8 @@ class ModsDeChecker(IntervalModule):
def login(self): def login(self):
data = urllib.parse.urlencode({ data = urllib.parse.urlencode({
"login_username": self.settings["username"], "login_username": self.username,
"login_password": self.settings["password"], "login_password": self.password,
"login_lifetime": "31536000" "login_lifetime": "31536000"
}) })

View File

@ -15,10 +15,7 @@ class NotmuchMailChecker(IntervalModule):
and "unread" and "unread"
""" """
db_path = "" settings = required = ("db_path",)
def __init__(self, db_path):
self.db_path = db_path
def run(self): def run(self):
db = notmuch.Database(self.db_path) db = notmuch.Database(self.db_path)

View File

@ -15,10 +15,10 @@ class Regex(IntervalModule):
flags = 0 flags = 0
format = "{0}" format = "{0}"
settings = ("format", "regex", "file")
required = ("regex", "file")
def __init__(self, settings): def init(self):
self.__dict__.update(settings)
self.re = re.compile(self.regex, self.flags) self.re = re.compile(self.regex, self.flags)
def run(self): def run(self):

View File

@ -23,16 +23,13 @@ class ThunderbirdMailChecker(IntervalModule):
the dbus-sender extension for thunderbird. the dbus-sender extension for thunderbird.
""" """
settings = { settings = ("format",)
"format": "%d new email"
}
unread = set() unread = set()
interval = 1 interval = 1
format = "%d new mail"
def __init__(self, settings=None): def init(self):
if settings is not None:
self.settings.update(settings)
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus() bus = dbus.SessionBus()
bus.add_signal_receiver(self.new_msg, bus.add_signal_receiver(self.new_msg,