VMM/maillocation: MailLocation.__init__(): take a 'format' name, v0.6.x
authorPascal Volk <neverseen@users.sourceforge.net>
Wed, 03 Mar 2010 14:32:30 +0000
branchv0.6.x
changeset 228 a7b000ca4ac9
parent 227 87db9f1f95ea
child 229 0fb2f12648a7
VMM/maillocation: MailLocation.__init__(): take a 'format' name, instead of a 'directory' name. - added function known_format() to the module. VMM/Account: Adjusted to above changes.
VirtualMailManager/Account.py
VirtualMailManager/maillocation.py
--- a/VirtualMailManager/Account.py	Tue Mar 02 19:24:36 2010 +0000
+++ b/VirtualMailManager/Account.py	Wed Mar 03 14:32:30 2010 +0000
@@ -8,8 +8,7 @@
 from VirtualMailManager.Domain import Domain
 from VirtualMailManager.EmailAddress import EmailAddress
 from VirtualMailManager.errors import AccountError as AccE
-from VirtualMailManager.maillocation import MailLocation, MAILDIR_NAME, \
-     MBOX_NAME, MDBOX_NAME, SDBOX_NAME
+from VirtualMailManager.maillocation import MailLocation, known_format
 from VirtualMailManager.Transport import Transport
 
 
@@ -76,12 +75,11 @@
         dbc.close()
 
     def _prepare(self, maillocation):
-        if not maillocation.lower() in map(lambda x: x.lower(), (MAILDIR_NAME,
-                                           MBOX_NAME, MDBOX_NAME, SDBOX_NAME)):
-            raise AccE(_(u'Unknown mail_location directory name: %r') %
+        if not known_format(maillocation):                                  
+            raise AccE(_(u'Unknown mail_location mailbox format: %r') %
                        maillocation, ERR.UNKNOWN_MAILLOCATION_NAME)
         self._setID()
-        self._mid = MailLocation(type_=maillocation).mid
+        self._mid = MailLocation(format=maillocation).mid
 
     def _switchState(self, state, dcvers, service):
         if not isinstance(state, bool):
--- a/VirtualMailManager/maillocation.py	Tue Mar 02 19:24:36 2010 +0000
+++ b/VirtualMailManager/maillocation.py	Wed Mar 03 14:32:30 2010 +0000
@@ -13,9 +13,8 @@
 from VirtualMailManager.pycompat import any
 
 
-__all__ = ('MailLocation',
-           'MAILDIR_ID', 'MBOX_ID', 'MDBOX_ID', 'SDBOX_ID',
-           'MAILDIR_NAME', 'MBOX_NAME', 'MDBOX_NAME', 'SDBOX_NAME')
+__all__ = ('MailLocation', 'known_format',
+           'MAILDIR_ID', 'MBOX_ID', 'MDBOX_ID', 'SDBOX_ID')
 
 MAILDIR_ID = 0x1
 MBOX_ID = 0x2
@@ -28,20 +27,20 @@
 
 _storage = {
     MAILDIR_ID: dict(dovecot_version=10, postfix=True, prefix='maildir:',
-                     directory=MAILDIR_NAME),
+                     directory=MAILDIR_NAME, mid=MAILDIR_ID),
     MBOX_ID: dict(dovecot_version=10, postfix=True, prefix='mbox:',
-                  directory=MBOX_NAME),
+                  directory=MBOX_NAME, mid=MBOX_ID),
     MDBOX_ID: dict(dovecot_version=20, postfix=False, prefix='mdbox:',
-                   directory=MDBOX_NAME),
+                   directory=MDBOX_NAME, mid=MDBOX_ID),
     SDBOX_ID: dict(dovecot_version=12, postfix=False, prefix='dbox:',
-                   directory=SDBOX_NAME),
+                   directory=SDBOX_NAME, mid=SDBOX_ID),
 }
 
-_type_id = {
+_format_id = {
     'maildir': MAILDIR_ID,
-    MBOX_NAME: MBOX_ID,
-    MDBOX_NAME: MDBOX_ID,
-    SDBOX_NAME: SDBOX_ID,
+    'mbox': MBOX_ID,
+    'mdbox': MDBOX_ID,
+    'dbox': SDBOX_ID,
 }
 
 
@@ -49,26 +48,26 @@
     """A small class for mail_location relevant information."""
     __slots__ = ('_info')
 
-    def __init__(self, mid=None, type_=None):
+    def __init__(self, mid=None, format=None):
         """Creates a new MailLocation instance.
 
-        Either mid or type_ must be specified.
+        Either a mid or the format must be specified.
 
         Keyword arguments:
         mid -- the id of a mail_location (int)
           one of the maillocation constants: `MAILDIR_ID`, `MBOX_ID`,
           `MDBOX_ID` and `SDBOX_ID`
-        type_ -- the type/mailbox format of the mail_location (str)
-          one of the maillocation constants: `MAILDIR_NAME`, `MBOX_NAME`,
-          `MDBOX_NAME` and `SDBOX_NAME`
+        format -- the mailbox format of the mail_location. One out of:
+        ``maildir``, ``mbox``, ``dbox`` and ``mdbox``.
         """
-        assert any((mid, type_))
+        assert any((mid, format))
         if mid:
             assert isinstance(mid, (int, long)) and mid in _storage
             self._info = _storage[mid]
         else:
-            assert isinstance(type_, basestring) and type_.lower() in _type_id
-            self._info = _storage[_type_id[type_.lower()]]
+            assert isinstance(format, basestring) and \
+                    format.lower() in _format_id
+            self._info = _storage[_format_id[format.lower()]]
 
     def __str__(self):
         return '%(prefix)s~/%(directory)s' % self._info
@@ -102,4 +101,9 @@
     @property
     def mid(self):
         """The mail_location's unique ID."""
-        return _type_id[self._info['directory'].lower()]
+        return self._info['mid']
+
+
+def known_format(format):
+    """Checks if the mailbox *format* is known, returns bool."""
+    return format.lower() in _format_id