Merge pull request #7 from enkore/master

Mailchecker, regex
This commit is contained in:
Jan Oliver Oelerich 2013-02-21 14:50:18 -08:00
commit e9b1976e1e
5 changed files with 66 additions and 39 deletions

View File

@ -60,7 +60,7 @@ class IOHandler:
Raises EOFError if the end of stream has been reached""" Raises EOFError if the end of stream has been reached"""
try: try:
line = self.inp.readline().decode("utf-8").strip() line = self.inp.readline().strip()
except KeyboardInterrupt: except KeyboardInterrupt:
raise EOFError() raise EOFError()

View File

@ -5,12 +5,22 @@ from i3pystatus import (
I3statusHandler, I3statusHandler,
mailchecker, mailchecker,
modsde, modsde,
notmuchmailchecker, notmuch,
thunderbird, thunderbird,
regex,
) )
status = I3statusHandler() 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 # The imap checker module
mailsettings = { mailsettings = {
"color": "#ff0000", "color": "#ff0000",
@ -46,8 +56,9 @@ status.register_module(mde)
# the notmuch mail checker module # the notmuch mail checker module
db_path = "path_to_your_notmuch_database" db_path = "path_to_your_notmuch_database"
notmuch = notmuchmailchecker.NotmuchMailChecker(db_path) nm = notmuch.NotmuchMailChecker(db_path)
status.register_module(notmuch) status.register_module(nm)
# the thunderbird dbus new mail checker module # the thunderbird dbus new mail checker module
tb = thunderbirdnewmail.ThunderbirdMailChecker() tb = thunderbirdnewmail.ThunderbirdMailChecker()

View File

@ -20,21 +20,15 @@ class MailChecker(IntervalModule):
"servers": [] "servers": []
} }
servers = []
def __init__(self, settings = None): def __init__(self, settings = None):
self.settings.update(settings) self.settings.update(settings)
for server in settings["servers"]: self.servers = list(map(MailChecker.MailServer, settings["servers"]))
srv = MailChecker.MailServer(server)
self.servers.append(srv)
def run(self): def run(self):
unread = sum([server.get_unread_count() for server in self.servers]) unread = sum(map(lambda server: server.get_unread_count(), self.servers))
if not unread:
return None
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",
@ -50,20 +44,13 @@ class MailChecker(IntervalModule):
tries to reconnect. It checks every "pause" seconds. tries to reconnect. It checks every "pause" seconds.
""" """
host = ""
port = ""
imap_class = imaplib.IMAP4 imap_class = imaplib.IMAP4
username = ""
password = ""
connection = None connection = None
def __init__(self, settings_dict): def __init__(self, settings_dict):
self.host = settings_dict["host"] self.__dict__.update(settings_dict)
self.port = settings_dict["port"]
self.username = settings_dict["username"]
self.password = settings_dict["password"]
if settings_dict["ssl"]: if self.ssl:
self.imap_class = imaplib.IMAP4_SSL self.imap_class = imaplib.IMAP4_SSL
def get_connection(self): def get_connection(self):

30
i3pystatus/regex.py Normal file
View File

@ -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",
}

View File

@ -7,15 +7,17 @@
# The plugin must be active and thunderbird running for the module to work # The plugin must be active and thunderbird running for the module to work
# properly. # properly.
import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop
import json import json
import threading import threading
import time import time
from functools import partial
from i3pystatus import AsyncModule import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop
class ThunderbirdMailChecker(AsyncModule): from i3pystatus import IntervalModule
class ThunderbirdMailChecker(IntervalModule):
""" """
This class listens for dbus signals emitted by This class listens for dbus signals emitted by
the dbus-sender extension for thunderbird. the dbus-sender extension for thunderbird.
@ -24,8 +26,8 @@ class ThunderbirdMailChecker(AsyncModule):
settings = { settings = {
"format": "%d new email" "format": "%d new email"
} }
unread = set() unread = set()
interval = 1
def __init__(self, settings=None): def __init__(self, settings=None):
if settings is not None: if settings is not None:
@ -43,10 +45,7 @@ class ThunderbirdMailChecker(AsyncModule):
dbus.mainloop.glib.threads_init() dbus.mainloop.glib.threads_init()
self.context = loop.get_context() self.context = loop.get_context()
def mainloop(self): self.run = partial(self.context.iteration, False)
while True:
self.context.iteration(False)
time.sleep(1)
def new_msg(self, id, author, subject): def new_msg(self, id, author, subject):
if id not in self.unread: if id not in self.unread:
@ -59,7 +58,7 @@ class ThunderbirdMailChecker(AsyncModule):
self._output() self._output()
def _output(self): def _output(self):
self.context.iteration(False) self.run()
unread = len(self.unread) unread = len(self.unread)
if unread: if unread: