From 76d86807db8917b7c0879c3dacbf9c6117966b04 Mon Sep 17 00:00:00 2001 From: enkore Date: Sun, 24 Feb 2013 22:54:32 +0100 Subject: [PATCH] As we can have multiple backends for mail already, we don't need the multiple-servers functionality in imap. Just add multiple instances of that backend. --- README.md | 25 +++++------ i3pystatus/file.py | 2 +- i3pystatus/mail/imap.py | 91 +++++++++++++++-------------------------- i3pystatus/mkdocs.py | 4 +- 4 files changed, 47 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index cbf208d..19ea46e 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ battery status This class shows a clock -* `format` — stftime format string +* `format` — stftime format string (default: `None`) @@ -100,7 +100,7 @@ of all components). The return value is bound to the key. * `format` — (required) * `components` — (required) -* `transforms` +* `transforms` — (default: `{}`) * `base_path` — (default: `/`) * `color` — (default: `#FFFFFF`) * `interval` — (default: `5`) @@ -139,19 +139,14 @@ The `backends` setting determines the backends to use. Currently available are: > ### imap > > -> This class handles IMAP mailservers. The mail server -> functionality is implemented in the subclass IMAP.MailServer -> -> The servers parameter should be a list of dicts containing the following -> items: -> * host -> * port (optional, defaults to 143) -> * username -> * password -> * ssl (optional, defaults to False) +> Checks for mail on a IMAP server > > -> * `servers` — (required) +> * `host` — (required) +> * `port` — (default: `143`) +> * `username` — (required) +> * `password` — (required) +> * `ssl` — (default: `False`) > > > @@ -191,7 +186,7 @@ unread posts in any bookmark in the mods.de forums. * `format` — Use {unread} as the formatter for number of unread posts (default: `{unread} new posts in bookmarks`) -* `offset` — subtract number of posts before output +* `offset` — subtract number of posts before output (default: `0`) * `color` — (default: `#7181fe`) * `username` — (required) * `password` — (required) @@ -207,7 +202,7 @@ Simple regex file watcher * `format` — format string used for output (default: `{0}`) * `regex` — (required) * `file` — file to search for regex matches -* `flags` — Python.re flags +* `flags` — Python.re flags (default: `0`) diff --git a/i3pystatus/file.py b/i3pystatus/file.py index 4c15f42..0f610f6 100644 --- a/i3pystatus/file.py +++ b/i3pystatus/file.py @@ -29,7 +29,7 @@ class File(IntervalModule): ) required = ("format", "components") base_path = "/" - transforms = tuple() + transforms = {} color = "#FFFFFF" def run(self): diff --git a/i3pystatus/mail/imap.py b/i3pystatus/mail/imap.py index baef9d7..db15433 100644 --- a/i3pystatus/mail/imap.py +++ b/i3pystatus/mail/imap.py @@ -10,67 +10,44 @@ from i3pystatus.mail import Backend class IMAP(Backend): """ - This class handles IMAP mailservers. The mail server - functionality is implemented in the subclass IMAP.MailServer - - The servers parameter should be a list of dicts containing the following - items: - * host - * port (optional, defaults to 143) - * username - * password - * ssl (optional, defaults to False) + Checks for mail on a IMAP server """ - settings = required = ("servers",) + settings = ( + "host", "port", + "username", "password", + "ssl" + ) + required = ("host", "username", "password") + + port = 143 + ssl = False + + imap_class = imaplib.IMAP4 + connection = None def init(self): - self.server_list = list(map(IMAP.MailServer, self.servers)) + if self.ssl: + self.imap_class = imaplib.IMAP4_SSL + + def get_connection(self): + if not self.connection: + try: + self.connection = self.imap_class(self.host, self.port) + self.connection.login(self.username, self.password) + self.connection.select() + except Exception: + self.connection = None + + try: + self.connection.select() + except Exception as e: + self.connection = None + + return self.connection @property def unread(self): - return sum(map(lambda server: server.get_unread_count(), self.server_list)) - - class MailServer: - """ - This class provides the functionality to connect - to a mail server and fetch the count of unread emails. - When the server connection is lost, it returns 0 and - tries to reconnect. It checks every "pause" seconds. - """ - - imap_class = imaplib.IMAP4 - connection = None - - ssl = False - port = 143 - - def __init__(self, settings_dict): - self.__dict__.update(settings_dict) - - if self.ssl: - self.imap_class = imaplib.IMAP4_SSL - - def get_connection(self): - if not self.connection: - try: - self.connection = self.imap_class(self.host, self.port) - self.connection.login(self.username, self.password) - self.connection.select() - except Exception: - self.connection = None - - try: - self.connection.select() - except Exception as e: - self.connection = None - - return self.connection - - def get_unread_count(self): - unread = 0 - conn = self.get_connection() - if conn: - unread += len(conn.search(None,"UnSeen")[1][0].split()) - - return unread + conn = self.get_connection() + if conn: + return len(conn.search(None,"UnSeen")[1][0].split()) diff --git a/i3pystatus/mkdocs.py b/i3pystatus/mkdocs.py index 5d0fd04..76f725c 100755 --- a/i3pystatus/mkdocs.py +++ b/i3pystatus/mkdocs.py @@ -68,7 +68,7 @@ class Setting: name = "" doc = "" required = False - default = None + default = sentinel = object() def __init__(self, mod, setting): if isinstance(setting, tuple): @@ -86,7 +86,7 @@ class Setting: attrs = [] if self.required: attrs.append("required") - if self.default: + if self.default is not self.sentinel: attrs.append("default: `{default}`".format(default=self.default)) formatted = "* `{name}` ".format(name=self.name)