VirtualMailManager/maillocation.py
branchv0.6.x
changeset 228 a7b000ca4ac9
parent 227 87db9f1f95ea
child 264 04fea4d8b900
equal deleted inserted replaced
227:87db9f1f95ea 228:a7b000ca4ac9
    11 """
    11 """
    12 
    12 
    13 from VirtualMailManager.pycompat import any
    13 from VirtualMailManager.pycompat import any
    14 
    14 
    15 
    15 
    16 __all__ = ('MailLocation',
    16 __all__ = ('MailLocation', 'known_format',
    17            'MAILDIR_ID', 'MBOX_ID', 'MDBOX_ID', 'SDBOX_ID',
    17            'MAILDIR_ID', 'MBOX_ID', 'MDBOX_ID', 'SDBOX_ID')
    18            'MAILDIR_NAME', 'MBOX_NAME', 'MDBOX_NAME', 'SDBOX_NAME')
       
    19 
    18 
    20 MAILDIR_ID = 0x1
    19 MAILDIR_ID = 0x1
    21 MBOX_ID = 0x2
    20 MBOX_ID = 0x2
    22 MDBOX_ID = 0x3
    21 MDBOX_ID = 0x3
    23 SDBOX_ID = 0x4
    22 SDBOX_ID = 0x4
    26 MDBOX_NAME = 'mdbox'
    25 MDBOX_NAME = 'mdbox'
    27 SDBOX_NAME = 'dbox'
    26 SDBOX_NAME = 'dbox'
    28 
    27 
    29 _storage = {
    28 _storage = {
    30     MAILDIR_ID: dict(dovecot_version=10, postfix=True, prefix='maildir:',
    29     MAILDIR_ID: dict(dovecot_version=10, postfix=True, prefix='maildir:',
    31                      directory=MAILDIR_NAME),
    30                      directory=MAILDIR_NAME, mid=MAILDIR_ID),
    32     MBOX_ID: dict(dovecot_version=10, postfix=True, prefix='mbox:',
    31     MBOX_ID: dict(dovecot_version=10, postfix=True, prefix='mbox:',
    33                   directory=MBOX_NAME),
    32                   directory=MBOX_NAME, mid=MBOX_ID),
    34     MDBOX_ID: dict(dovecot_version=20, postfix=False, prefix='mdbox:',
    33     MDBOX_ID: dict(dovecot_version=20, postfix=False, prefix='mdbox:',
    35                    directory=MDBOX_NAME),
    34                    directory=MDBOX_NAME, mid=MDBOX_ID),
    36     SDBOX_ID: dict(dovecot_version=12, postfix=False, prefix='dbox:',
    35     SDBOX_ID: dict(dovecot_version=12, postfix=False, prefix='dbox:',
    37                    directory=SDBOX_NAME),
    36                    directory=SDBOX_NAME, mid=SDBOX_ID),
    38 }
    37 }
    39 
    38 
    40 _type_id = {
    39 _format_id = {
    41     'maildir': MAILDIR_ID,
    40     'maildir': MAILDIR_ID,
    42     MBOX_NAME: MBOX_ID,
    41     'mbox': MBOX_ID,
    43     MDBOX_NAME: MDBOX_ID,
    42     'mdbox': MDBOX_ID,
    44     SDBOX_NAME: SDBOX_ID,
    43     'dbox': SDBOX_ID,
    45 }
    44 }
    46 
    45 
    47 
    46 
    48 class MailLocation(object):
    47 class MailLocation(object):
    49     """A small class for mail_location relevant information."""
    48     """A small class for mail_location relevant information."""
    50     __slots__ = ('_info')
    49     __slots__ = ('_info')
    51 
    50 
    52     def __init__(self, mid=None, type_=None):
    51     def __init__(self, mid=None, format=None):
    53         """Creates a new MailLocation instance.
    52         """Creates a new MailLocation instance.
    54 
    53 
    55         Either mid or type_ must be specified.
    54         Either a mid or the format must be specified.
    56 
    55 
    57         Keyword arguments:
    56         Keyword arguments:
    58         mid -- the id of a mail_location (int)
    57         mid -- the id of a mail_location (int)
    59           one of the maillocation constants: `MAILDIR_ID`, `MBOX_ID`,
    58           one of the maillocation constants: `MAILDIR_ID`, `MBOX_ID`,
    60           `MDBOX_ID` and `SDBOX_ID`
    59           `MDBOX_ID` and `SDBOX_ID`
    61         type_ -- the type/mailbox format of the mail_location (str)
    60         format -- the mailbox format of the mail_location. One out of:
    62           one of the maillocation constants: `MAILDIR_NAME`, `MBOX_NAME`,
    61         ``maildir``, ``mbox``, ``dbox`` and ``mdbox``.
    63           `MDBOX_NAME` and `SDBOX_NAME`
       
    64         """
    62         """
    65         assert any((mid, type_))
    63         assert any((mid, format))
    66         if mid:
    64         if mid:
    67             assert isinstance(mid, (int, long)) and mid in _storage
    65             assert isinstance(mid, (int, long)) and mid in _storage
    68             self._info = _storage[mid]
    66             self._info = _storage[mid]
    69         else:
    67         else:
    70             assert isinstance(type_, basestring) and type_.lower() in _type_id
    68             assert isinstance(format, basestring) and \
    71             self._info = _storage[_type_id[type_.lower()]]
    69                     format.lower() in _format_id
       
    70             self._info = _storage[_format_id[format.lower()]]
    72 
    71 
    73     def __str__(self):
    72     def __str__(self):
    74         return '%(prefix)s~/%(directory)s' % self._info
    73         return '%(prefix)s~/%(directory)s' % self._info
    75 
    74 
    76     @property
    75     @property
   100         return self.__str__()
    99         return self.__str__()
   101 
   100 
   102     @property
   101     @property
   103     def mid(self):
   102     def mid(self):
   104         """The mail_location's unique ID."""
   103         """The mail_location's unique ID."""
   105         return _type_id[self._info['directory'].lower()]
   104         return self._info['mid']
       
   105 
       
   106 
       
   107 def known_format(format):
       
   108     """Checks if the mailbox *format* is known, returns bool."""
       
   109     return format.lower() in _format_id