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:
parent
c967cdecb2
commit
612faaaa4e
@ -42,8 +42,10 @@ class BatteryChecker(IntervalModule):
|
||||
battery status
|
||||
"""
|
||||
|
||||
def __init__(self, battery_ident="BAT0"):
|
||||
self.battery_ident = battery_ident
|
||||
settings = ("battery_ident",)
|
||||
battery_ident = "BAT0"
|
||||
|
||||
def init(self):
|
||||
self.base_path = "/sys/class/power_supply/{0}/uevent".format(self.battery_ident)
|
||||
|
||||
def run(self):
|
||||
|
@ -9,23 +9,25 @@ class Clock(IntervalModule):
|
||||
"""
|
||||
This class shows a clock
|
||||
"""
|
||||
|
||||
def __init__(self, format_string=None):
|
||||
|
||||
settings = ("format",)
|
||||
format = None
|
||||
|
||||
def init(self):
|
||||
lang = os.environ.get('LANG', None)
|
||||
if lang:
|
||||
locale.setlocale(locale.LC_ALL, lang)
|
||||
if not format_string:
|
||||
if self.format is not None:
|
||||
lang = locale.getlocale()[0]
|
||||
if lang == 'en_US':
|
||||
# MDY format - United States of America
|
||||
format_string = "%a %b %-d %X"
|
||||
self.format = "%a %b %-d %X"
|
||||
else:
|
||||
# DMY format - almost all other countries
|
||||
format_string = "%a %-d %b %X"
|
||||
self.format_string = format_string
|
||||
self.format = "%a %-d %b %X"
|
||||
|
||||
def run(self):
|
||||
full_text = datetime.datetime.now().strftime(self.format_string)
|
||||
full_text = datetime.datetime.now().strftime(self.format)
|
||||
self.output = {
|
||||
"full_text": full_text,
|
||||
"name": "pyclock",
|
||||
|
@ -15,25 +15,22 @@ class MailChecker(IntervalModule):
|
||||
functionality is implemented in the subclass MailChecker.MailServer
|
||||
"""
|
||||
|
||||
settings = {
|
||||
"color": "#ff0000",
|
||||
"servers": []
|
||||
}
|
||||
settings = ("color", "servers")
|
||||
required = ("servers",)
|
||||
color = "#ff0000"
|
||||
|
||||
def __init__(self, settings = None):
|
||||
self.settings.update(settings)
|
||||
|
||||
self.servers = list(map(MailChecker.MailServer, settings["servers"]))
|
||||
def init(self):
|
||||
self.server_list = list(map(MailChecker.MailServer, self.servers))
|
||||
|
||||
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:
|
||||
self.output = {
|
||||
"full_text" : "%d new email%s" % (unread, ("s" if unread > 1 else "")),
|
||||
"name" : "newmail",
|
||||
"urgent" : "true",
|
||||
"color" : self.settings["color"]
|
||||
"color" : self.color
|
||||
}
|
||||
|
||||
class MailServer:
|
||||
|
@ -17,20 +17,20 @@ class ModsDeChecker(IntervalModule):
|
||||
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/"
|
||||
bookmark_url = "http://forum.mods.de/bb/xml/bookmarks.php"
|
||||
opener = None
|
||||
cj = None
|
||||
logged_in = False
|
||||
|
||||
settings = {
|
||||
"color": "#7181fe",
|
||||
"offset": 0,
|
||||
"format": "%d new posts in bookmarks"
|
||||
}
|
||||
|
||||
def __init__(self, settings = None):
|
||||
self.settings.update(settings)
|
||||
def init(self):
|
||||
self.cj = http.cookiejar.CookieJar()
|
||||
self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj))
|
||||
|
||||
@ -41,10 +41,10 @@ class ModsDeChecker(IntervalModule):
|
||||
self.output = None
|
||||
else:
|
||||
self.output = {
|
||||
"full_text" : self.settings["format"] % unread,
|
||||
"full_text" : self.format % unread,
|
||||
"name" : "modsde",
|
||||
"urgent" : "true",
|
||||
"color" : self.settings["color"]
|
||||
"color" : self.color
|
||||
}
|
||||
|
||||
def get_unread_count(self):
|
||||
@ -54,7 +54,7 @@ class ModsDeChecker(IntervalModule):
|
||||
try:
|
||||
f = self.opener.open(self.bookmark_url)
|
||||
root = ET.fromstring(f.read())
|
||||
return int(root.attrib["newposts"]) - self.settings["offset"]
|
||||
return int(root.attrib["newposts"]) - self.offset
|
||||
except Exception:
|
||||
self.cj.clear()
|
||||
self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cj))
|
||||
@ -62,8 +62,8 @@ class ModsDeChecker(IntervalModule):
|
||||
|
||||
def login(self):
|
||||
data = urllib.parse.urlencode({
|
||||
"login_username": self.settings["username"],
|
||||
"login_password": self.settings["password"],
|
||||
"login_username": self.username,
|
||||
"login_password": self.password,
|
||||
"login_lifetime": "31536000"
|
||||
})
|
||||
|
||||
|
@ -15,10 +15,7 @@ class NotmuchMailChecker(IntervalModule):
|
||||
and "unread"
|
||||
"""
|
||||
|
||||
db_path = ""
|
||||
|
||||
def __init__(self, db_path):
|
||||
self.db_path = db_path
|
||||
settings = required = ("db_path",)
|
||||
|
||||
def run(self):
|
||||
db = notmuch.Database(self.db_path)
|
||||
|
@ -15,10 +15,10 @@ class Regex(IntervalModule):
|
||||
|
||||
flags = 0
|
||||
format = "{0}"
|
||||
settings = ("format", "regex", "file")
|
||||
required = ("regex", "file")
|
||||
|
||||
def __init__(self, settings):
|
||||
self.__dict__.update(settings)
|
||||
|
||||
def init(self):
|
||||
self.re = re.compile(self.regex, self.flags)
|
||||
|
||||
def run(self):
|
||||
|
@ -23,16 +23,13 @@ class ThunderbirdMailChecker(IntervalModule):
|
||||
the dbus-sender extension for thunderbird.
|
||||
"""
|
||||
|
||||
settings = {
|
||||
"format": "%d new email"
|
||||
}
|
||||
settings = ("format",)
|
||||
|
||||
unread = set()
|
||||
interval = 1
|
||||
format = "%d new mail"
|
||||
|
||||
def __init__(self, settings=None):
|
||||
if settings is not None:
|
||||
self.settings.update(settings)
|
||||
|
||||
def init(self):
|
||||
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
||||
bus = dbus.SessionBus()
|
||||
bus.add_signal_receiver(self.new_msg,
|
||||
|
Loading…
Reference in New Issue
Block a user