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.
This commit is contained in:
enkore 2013-02-24 22:54:32 +01:00
parent 582a35e412
commit 76d86807db
4 changed files with 47 additions and 75 deletions

View File

@ -75,7 +75,7 @@ battery status
This class shows a clock 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) * `format` — (required)
* `components` — (required) * `components` — (required)
* `transforms` * `transforms` — (default: `{}`)
* `base_path` — (default: `/`) * `base_path` — (default: `/`)
* `color` — (default: `#FFFFFF`) * `color` — (default: `#FFFFFF`)
* `interval` — (default: `5`) * `interval` — (default: `5`)
@ -139,19 +139,14 @@ The `backends` setting determines the backends to use. Currently available are:
> ### imap > ### imap
> >
> >
> This class handles IMAP mailservers. The mail server > Checks for mail on a IMAP 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)
> >
> >
> * `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`) * `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`) * `color` — (default: `#7181fe`)
* `username` — (required) * `username` — (required)
* `password` — (required) * `password` — (required)
@ -207,7 +202,7 @@ Simple regex file watcher
* `format` — format string used for output (default: `{0}`) * `format` — format string used for output (default: `{0}`)
* `regex` — (required) * `regex` — (required)
* `file` — file to search for regex matches * `file` — file to search for regex matches
* `flags` — Python.re flags * `flags` — Python.re flags (default: `0`)

View File

@ -29,7 +29,7 @@ class File(IntervalModule):
) )
required = ("format", "components") required = ("format", "components")
base_path = "/" base_path = "/"
transforms = tuple() transforms = {}
color = "#FFFFFF" color = "#FFFFFF"
def run(self): def run(self):

View File

@ -10,67 +10,44 @@ from i3pystatus.mail import Backend
class IMAP(Backend): class IMAP(Backend):
""" """
This class handles IMAP mailservers. The mail server Checks for mail on a IMAP 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)
""" """
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): 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 @property
def unread(self): def unread(self):
return sum(map(lambda server: server.get_unread_count(), self.server_list)) conn = self.get_connection()
if conn:
class MailServer: return len(conn.search(None,"UnSeen")[1][0].split())
"""
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

View File

@ -68,7 +68,7 @@ class Setting:
name = "" name = ""
doc = "" doc = ""
required = False required = False
default = None default = sentinel = object()
def __init__(self, mod, setting): def __init__(self, mod, setting):
if isinstance(setting, tuple): if isinstance(setting, tuple):
@ -86,7 +86,7 @@ class Setting:
attrs = [] attrs = []
if self.required: if self.required:
attrs.append("required") attrs.append("required")
if self.default: if self.default is not self.sentinel:
attrs.append("default: `{default}`".format(default=self.default)) attrs.append("default: `{default}`".format(default=self.default))
formatted = "* `{name}` ".format(name=self.name) formatted = "* `{name}` ".format(name=self.name)