Package ProcImap :: Package Utils :: Module MailboxFactory :: Class MailboxFactory
[hide private]
[frames] | no frames]

Class MailboxFactory

source code

MailboxFactory is a factory class for Mailbox objects. You can define
mailboxes of different types in an INI-style config file (the file
has to parsable by ConfigParser.ConfigParser; the exceptions defined
in ConfigParser may be thrown if the config file is not well-formed.)
Each section in the config file describes one mailbox.

An example of a valid config file 'mailboxes.cfg' is the following:

    [Standard]
    type = IMAP
    mailbox = INBOX
    server = mail.physik.fu-berlin.de
    username = goerz
    password = secret
    ssl = True
    port = 933

    [Sent]
    type = IMAP
    mailbox = Sent
    server = mail.physik.fu-berlin.de
    username = goerz
    password = secret

    [Backup]
    type = mbox
    path = /home/goerz/Mail/backup.mbox

The type of the mailbox is described by the 'type' parameters. The
types known by default are 'imap', 'mbox', 'maildir', 'MH', 'Babyl',
and 'MMDF', all of which have corresponding subclasses of
mailbox.Mailbox (all except ImapMailbox are defined in the standard
library). The type specification is not case sensitive.

The remaining parameters in a specific section depend on the type. The
Mailbox classes from the standard library need only a path; IMAP needs
type, mailbox, server, username, and password. The ssl and port
parameters are optional. ssl is enabled by default; the port, if
unspecified, is the standard port (933 for ssl, 433 otherwise).

MailboxFactory has capabilities to extend the set of known types by
using the set_type method.

The MailboxFactory partly supports a read-only dictionary interface.

Instance Methods [hide private]
 
__init__(self, configfilename)
Initialize MailboxFactory files.
source code
 
get(self, name)
Create the Mailbox object that is described in section 'name' in the config file.
source code
 
__getitem__(self, name)
Shorthand for the get method.
source code
 
get_server(self, name)
Return an ImapServer instance from the server data that is described in section 'name'.
source code
 
__contains__(self, name)
Return True if there is a mailbox with the given name, False otherwise
source code
 
list(self)
List all mailboxes defined in the config file
source code
 
data(self, name)
List all the data associated with the mailbox name
source code
 
set_type(self, typename, factory, pathgenerator)
Make a new typename of Mailbox known.
source code
Method Details [hide private]

__init__(self, configfilename)
(Constructor)

source code 

Initialize MailboxFactory files. The mailbox objects that can be generated must be described in configfilename.

get(self, name)

source code 
Create the Mailbox object that is described in section 'name'
in the config file. For example,
    >>> mailboxes = MailboxFactory("mailboxes.cfg")
    >>> mb = mailboxes.get('Standard')
mb would now be an object of type ImapMailbox if mailboxes.cfg
contained the data as the example in the class docstring.

__getitem__(self, name)
(Indexing operator)

source code 

Shorthand for the get method. For example,

>>> mailboxes = MailboxFactory("mailboxes.cfg")
>>> mb = mailboxes['Standard']

get_server(self, name)

source code 

Return an ImapServer instance from the server data that is described in section 'name'. The section must have the form of an imap mailbox (as described above). A TypeError will be raised if the section is not of type IMAP. The 'mailbox' key is ignored.

For example, you could create an ImapServer like this:

>>> mailboxes = MailboxFactory("mailboxes.cfg")
>>> server = mailboxes.get_server('StandardServer')

set_type(self, typename, factory, pathgenerator)

source code 

Make a new typename of Mailbox known. This allows you to handle new types of Mailbox objects beyond IMAP and the mailboxes of the standard library.

factory is the class that generates the Mailbox object and must be a subclass of mailbox.Mailbox

pathgenerator is a callable that receives a dict of options set in a section of the config file, and returns the 'path' that is passed as the first argument to the factory. For the standard mailboxes of the standard library, the 'path' is just a string, the path of the mailbox in the filesystem. For IMAP, the path is a tuple (server, name). For new types, this may be anything.

For example the constructor of this class makes the 'mbox' type known as: self.set_type('mbox', mailbox.mbox, standard_pathgenerator)

In combination, factory(pathgenerator(dict_of_options_in_configfile_section)) should create a Mailbox object of the appropriate type.