VirtualMailManager/__init__.py
branchv0.6.x
changeset 216 0c8c053b451c
parent 215 33f727efa7c4
child 234 e88ba0fb1281
equal deleted inserted replaced
215:33f727efa7c4 216:0c8c053b451c
    18 from VirtualMailManager.constants.ERROR import \
    18 from VirtualMailManager.constants.ERROR import \
    19      DOMAIN_INVALID, DOMAIN_TOO_LONG, LOCALPART_INVALID, LOCALPART_TOO_LONG, \
    19      DOMAIN_INVALID, DOMAIN_TOO_LONG, LOCALPART_INVALID, LOCALPART_TOO_LONG, \
    20      NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY
    20      NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY
    21 from VirtualMailManager.constants.version import __author__, __date__, \
    21 from VirtualMailManager.constants.version import __author__, __date__, \
    22      __version__
    22      __version__
    23 from VirtualMailManager.Exceptions import VMMException
    23 from VirtualMailManager.errors import VMMError
    24 
    24 
    25 
    25 
    26 __all__ = [
    26 __all__ = [
    27     # version information from VERSION
    27     # version information from VERSION
    28     '__author__', '__date__', '__version__',
    28     '__author__', '__date__', '__version__',
    67 
    67 
    68 
    68 
    69 def is_dir(path):
    69 def is_dir(path):
    70     """Checks if `path` is a directory.
    70     """Checks if `path` is a directory.
    71 
    71 
    72     Throws a `VMMException` if `path` is not a directory.
    72     Throws a `VMMError` if `path` is not a directory.
    73 
    73 
    74     """
    74     """
    75     path = expand_path(path)
    75     path = expand_path(path)
    76     if not os.path.isdir(path):
    76     if not os.path.isdir(path):
    77         raise VMMException(_(u"'%s' is not a directory") %
    77         raise VMMError(_(u"'%s' is not a directory") % get_unicode(path),
    78                             get_unicode(path), NO_SUCH_DIRECTORY)
    78                        NO_SUCH_DIRECTORY)
    79     return path
    79     return path
    80 
    80 
    81 
    81 
    82 def exec_ok(binary):
    82 def exec_ok(binary):
    83     """Checks if the `binary` exists and if it is executable.
    83     """Checks if the `binary` exists and if it is executable.
    84 
    84 
    85     Throws a `VMMException` if the `binary` isn't a file or is not
    85     Throws a `VMMError` if the `binary` isn't a file or is not
    86     executable.
    86     executable.
    87 
    87 
    88     """
    88     """
    89     binary = expand_path(binary)
    89     binary = expand_path(binary)
    90     if not os.path.isfile(binary):
    90     if not os.path.isfile(binary):
    91         raise VMMException(_(u"'%s' is not a file") % get_unicode(binary),
    91         raise VMMError(_(u"'%s' is not a file") % get_unicode(binary),
    92                            NO_SUCH_BINARY)
    92                        NO_SUCH_BINARY)
    93     if not os.access(binary, os.X_OK):
    93     if not os.access(binary, os.X_OK):
    94         raise VMMException(_(u"File is not executable: '%s'") %
    94         raise VMMError(_(u"File is not executable: '%s'") % 
    95                            get_unicode(binary), NOT_EXECUTABLE)
    95                        get_unicode(binary), NOT_EXECUTABLE)
    96     return binary
    96     return binary
    97 
    97 
    98 
    98 
    99 def idn2ascii(domainname):
    99 def idn2ascii(domainname):
   100     """Converts the idn domain name `domainname` into punycode."""
   100     """Converts the idn domain name `domainname` into punycode."""
   110     """Returns the validated domain name `domainname`.
   110     """Returns the validated domain name `domainname`.
   111 
   111 
   112     It also converts the name of the domain from IDN to ASCII, if
   112     It also converts the name of the domain from IDN to ASCII, if
   113     necessary.
   113     necessary.
   114 
   114 
   115     Throws an `VMMException`, if the domain name is too long or doesn't
   115     Throws an `VMMError`, if the domain name is too long or doesn't
   116     look like a valid domain name (label.label.label).
   116     look like a valid domain name (label.label.label).
   117 
   117 
   118     """
   118     """
   119     if not RE_DOMAIN.match(domainname):
   119     if not RE_DOMAIN.match(domainname):
   120         domainname = idn2ascii(domainname)
   120         domainname = idn2ascii(domainname)
   121     if len(domainname) > 255:
   121     if len(domainname) > 255:
   122         raise VMMException(_(u'The domain name is too long'), DOMAIN_TOO_LONG)
   122         raise VMMError(_(u'The domain name is too long'), DOMAIN_TOO_LONG)
   123     if not RE_DOMAIN.match(domainname):
   123     if not RE_DOMAIN.match(domainname):
   124         raise VMMException(_(u'The domain name %r is invalid') % domainname,
   124         raise VMMError(_(u'The domain name %r is invalid') % domainname,
   125                            DOMAIN_INVALID)
   125                        DOMAIN_INVALID)
   126     return domainname
   126     return domainname
   127 
   127 
   128 
   128 
   129 def check_localpart(localpart):
   129 def check_localpart(localpart):
   130     """Returns the validated local-part `localpart`.
   130     """Returns the validated local-part `localpart`.
   131 
   131 
   132     Throws a `VMMException` if the local-part is too long or contains
   132     Throws a `VMMError` if the local-part is too long or contains
   133     invalid characters.
   133     invalid characters.
   134 
   134 
   135     """
   135     """
   136     if len(localpart) > 64:
   136     if len(localpart) > 64:
   137         raise VMMException(_(u'The local-part %r is too long') % localpart,
   137         raise VMMError(_(u'The local-part %r is too long') % localpart,
   138                            LOCALPART_TOO_LONG)
   138                        LOCALPART_TOO_LONG)
   139     invalid_chars = set(RE_LOCALPART.findall(localpart))
   139     invalid_chars = set(RE_LOCALPART.findall(localpart))
   140     if invalid_chars:
   140     if invalid_chars:
   141         i_chars = u''.join((u'"%s" ' % c for c in invalid_chars))
   141         i_chars = u''.join((u'"%s" ' % c for c in invalid_chars))
   142         raise VMMException(_(u"The local-part %(l_part)r contains invalid \
   142         raise VMMError(_(u"The local-part %(l_part)r contains invalid \
   143 characters: %(i_chars)s") %
   143 characters: %(i_chars)s") %
   144                            {'l_part': localpart, 'i_chars': i_chars},
   144                        {'l_part': localpart, 'i_chars': i_chars},
   145                            LOCALPART_INVALID)
   145                        LOCALPART_INVALID)
   146     return localpart
   146     return localpart
   147 
   147 
   148 
   148 
   149 del _
   149 del _