VMM: Moved transport-validation to common.
authorPascal Volk <user@localhost.localdomain.org>
Sun, 16 Sep 2012 17:05:33 +0000
changeset 614 1ec3497a6733
parent 613 7b3494f1a2ef
child 615 5882bfdf83e8
VMM: Moved transport-validation to common.
VirtualMailManager/account.py
VirtualMailManager/common.py
--- a/VirtualMailManager/account.py	Sat Sep 15 17:31:34 2012 +0000
+++ b/VirtualMailManager/account.py	Sun Sep 16 17:05:33 2012 +0000
@@ -14,6 +14,7 @@
      ACCOUNT_EXISTS, ACCOUNT_MISSING_PASSWORD, ALIAS_PRESENT, \
      INVALID_ARGUMENT, INVALID_MAIL_LOCATION, NO_SUCH_ACCOUNT, \
      NO_SUCH_DOMAIN, VMM_ERROR
+from VirtualMailManager.common import validate_transport
 from VirtualMailManager.domain import Domain
 from VirtualMailManager.emailaddress import EmailAddress
 from VirtualMailManager.errors import VMMError, AccountError as AErr
@@ -125,12 +126,7 @@
                        'version': version_str(maillocation.dovecot_version)},
                        INVALID_MAIL_LOCATION)
         transport = self._transport or self._domain.transport
-        if not maillocation.postfix and \
-          transport.transport.lower() in ('virtual:', 'virtual'):
-            raise AErr(_(u"Invalid transport '%(transport)s' for mailbox "
-                         u"format '%(mbfmt)s'.") %
-                       {'transport': transport,
-                        'mbfmt': maillocation.mbformat}, INVALID_MAIL_LOCATION)
+        validate_transport(transport, maillocation)
         self._mail = maillocation
         self._set_uid()
 
@@ -350,13 +346,7 @@
         self._transport = transport
         if transport is not None:
             assert isinstance(transport, Transport)
-            if transport.transport.lower() in ('virtual', 'virtual:') and \
-                not self._mail.postfix:
-                raise AErr(_(u"Invalid transport '%(transport)s' for mailbox "
-                             u"format '%(mbfmt)s'.") %
-                           {'transport': transport,
-                            'mbfmt': self._mail.mbformat},
-                           INVALID_MAIL_LOCATION)
+            validate_transport(transport, self._mail)
             transport = transport.tid
         self._update_tables('tid', transport)
 
--- a/VirtualMailManager/common.py	Sat Sep 15 17:31:34 2012 +0000
+++ b/VirtualMailManager/common.py	Sun Sep 16 17:05:33 2012 +0000
@@ -14,8 +14,8 @@
 import stat
 
 from VirtualMailManager import ENCODING
-from VirtualMailManager.constants import NOT_EXECUTABLE, NO_SUCH_BINARY, \
-     TYPE_ACCOUNT, TYPE_ALIAS, TYPE_RELOCATED
+from VirtualMailManager.constants import INVALID_MAIL_LOCATION, \
+     NOT_EXECUTABLE, NO_SUCH_BINARY, TYPE_ACCOUNT, TYPE_ALIAS, TYPE_RELOCATED
 from VirtualMailManager.errors import VMMError
 
 VERSION_RE = re.compile(r'^(\d+)\.(\d+)\.(?:(\d+)|(alpha|beta|rc)(\d+))$')
@@ -121,6 +121,27 @@
         return num
 
 
+def validate_transport(transport, maillocation):
+    """Checks if the `transport` is usable for the given `maillocation`.
+
+    Throws a `VMMError` if the chosen `transport` is unable to write
+    messages in the `maillocation`'s mailbox format.
+
+    Arguments:
+
+    `transport` : VirtualMailManager.transport.Transport
+      a Transport object
+    `maillocation` : VirtualMailManager.maillocation.MailLocation
+      a MailLocation object
+    """
+    if transport.transport in ('virtual', 'virtual:') and \
+      not maillocation.postfix:
+        raise VMMError(_(u"Invalid transport '%(transport)s' for mailbox "
+                         u"format '%(mbfmt)s'.") %
+                       {'transport': transport.transport,
+                        'mbfmt': maillocation.mbformat}, INVALID_MAIL_LOCATION)
+
+
 def version_hex(version_string):
     """Converts a Dovecot version, e.g.: '1.2.3' or '2.0.beta4', to an int.
     Raises a `ValueError` if the *version_string* has the wrong™ format.