From 426f57d97dff8eddc980fe9893a3e8527bdf4426 Mon Sep 17 00:00:00 2001 From: enkore Date: Thu, 21 Feb 2013 04:56:34 +0100 Subject: [PATCH] Regex module. Batteries... eh.. example included. *gotta get some sleep. maybe. --- i3pystatus/__main__.py.dist | 17 ++++++++++++++--- i3pystatus/regex.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 i3pystatus/regex.py diff --git a/i3pystatus/__main__.py.dist b/i3pystatus/__main__.py.dist index 2b4f391..8561d0f 100755 --- a/i3pystatus/__main__.py.dist +++ b/i3pystatus/__main__.py.dist @@ -5,12 +5,22 @@ from i3pystatus import ( I3statusHandler, mailchecker, modsde, - notmuchmailchecker, + notmuch, thunderbird, + regex, ) status = I3statusHandler() +# Regular expression file watcher +# If you're using a thinkpad, chances are that this displays your fan speed and level +regexsettings = { + "regex": "speed:\s+([0-9]+)\nlevel:\s+([a-zA-Z0-9]+)", + "file": "/proc/acpi/ibm/fan", + "format": "{0} [{1}]", +} +status.register(regex.Regex(regexsettings)) + # The imap checker module mailsettings = { "color": "#ff0000", @@ -46,8 +56,9 @@ status.register_module(mde) # the notmuch mail checker module db_path = "path_to_your_notmuch_database" -notmuch = notmuchmailchecker.NotmuchMailChecker(db_path) -status.register_module(notmuch) +nm = notmuch.NotmuchMailChecker(db_path) +status.register_module(nm) + # the thunderbird dbus new mail checker module tb = thunderbirdnewmail.ThunderbirdMailChecker() diff --git a/i3pystatus/regex.py b/i3pystatus/regex.py new file mode 100644 index 0000000..dd3e872 --- /dev/null +++ b/i3pystatus/regex.py @@ -0,0 +1,30 @@ +import re + +from i3pystatus import IntervalModule + +class Regex(IntervalModule): + """ + Simple regex file watcher + + Settings: + * flags — Python.re flags + * regex — regular expression + * file — file to search for regex matches + * format — new-style format string used for output, default is "{0}" + """ + + flags = 0 + format = "{0}" + + def __init__(self, settings): + self.__dict__.update(settings) + + self.re = re.compile(self.regex, self.flags) + + def run(self): + with open(self.file, "r") as f: + match = self.re.search(f.read()) + self.output = self.output = { + "full_text" : self.format.format(*match.groups()), + "name" : "regex", + } \ No newline at end of file