VirtualMailManager/common.py
changeset 614 1ec3497a6733
parent 611 8e9b0046bc8f
child 643 df1e3b67882a
child 675 d24f094d1cb5
--- 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.