VirtualMailManager/__init__.py
author Pascal Volk <neverseen@users.sourceforge.net>
Tue, 09 Feb 2010 04:59:40 +0000
branchv0.6.x
changeset 194 6c06edb5b2d2
parent 188 cf1b5f22dbd2
child 199 0684790fff7c
permissions -rw-r--r--
VMM/EmailAddress: reworked once more. - moved EmailAddress.__chkLocalpart() -> __module__.check_localpart() - renamed EmailAddress.__chkAddress() -> EmailAddress._chk_address() - attributes domainname and localpart are no longer protected - added missing doc strings.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
162
0ac9ef587769 Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 152
diff changeset
     2
# Copyright (c) 2007 - 2010, Pascal Volk
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
     4
# package initialization code
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
#
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
     6
188
cf1b5f22dbd2 VMM/Handler: __init__ accepts now a config_type ('default'||'cli').
Pascal Volk <neverseen@users.sourceforge.net>
parents: 187
diff changeset
     7
import gettext
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
     8
import os
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
     9
import re
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    10
import locale
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    11
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    12
from encodings.idna import ToASCII, ToUnicode
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    13
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    14
from VirtualMailManager.constants.ERROR import \
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    15
     DOMAIN_INVALID, DOMAIN_TOO_LONG, NOT_EXECUTABLE, NO_SUCH_BINARY, \
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    16
     NO_SUCH_DIRECTORY
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    17
from VirtualMailManager.constants.VERSION import *
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    18
from VirtualMailManager.Exceptions import VMMException
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    19
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    20
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    21
__all__ = [
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    22
    # imported modules
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    23
    'os', 're', 'locale',
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    24
    # version information from VERSION
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    25
    '__author__', '__date__', '__version__',
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    26
    # error codes
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    27
    'ENCODING', 'ace2idna', 'chk_domainname', 'exec_ok', 'expand_path',
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    28
    'get_unicode', 'idn2ascii', 'is_dir',
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    29
]
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    30
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    31
152
3dbee02711cd Ignore unsupported locale setting and silently fall back to 'C'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 141
diff changeset
    32
# Try to set all of the locales according to the current
3dbee02711cd Ignore unsupported locale setting and silently fall back to 'C'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 141
diff changeset
    33
# environment variables and get the character encoding.
3dbee02711cd Ignore unsupported locale setting and silently fall back to 'C'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 141
diff changeset
    34
try:
3dbee02711cd Ignore unsupported locale setting and silently fall back to 'C'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 141
diff changeset
    35
    locale.setlocale(locale.LC_ALL, '')
3dbee02711cd Ignore unsupported locale setting and silently fall back to 'C'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 141
diff changeset
    36
except locale.Error:
3dbee02711cd Ignore unsupported locale setting and silently fall back to 'C'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 141
diff changeset
    37
    locale.setlocale(locale.LC_ALL, 'C')
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    38
ENCODING = locale.nl_langinfo(locale.CODESET)
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    39
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    40
RE_ASCII_CHARS = """^[\x20-\x7E]*$"""
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    41
RE_DOMAIN = """^(?:[a-z0-9-]{1,63}\.){1,}[a-z]{2,6}$"""
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    42
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    43
188
cf1b5f22dbd2 VMM/Handler: __init__ accepts now a config_type ('default'||'cli').
Pascal Volk <neverseen@users.sourceforge.net>
parents: 187
diff changeset
    44
gettext.install('vmm', '/usr/local/share/locale', unicode=1)
cf1b5f22dbd2 VMM/Handler: __init__ accepts now a config_type ('default'||'cli').
Pascal Volk <neverseen@users.sourceforge.net>
parents: 187
diff changeset
    45
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    46
def get_unicode(string):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    47
    """Converts `string` to `unicode`, if necessary."""
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    48
    if isinstance(string, unicode):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    49
        return string
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    50
    return unicode(string, ENCODING, 'replace')
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    51
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    52
def expand_path(path):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    53
    """Expands paths, starting with ``.`` or ``~``, to an absolute path."""
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    54
    if path.startswith('.'):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    55
        return os.path.abspath(path)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    56
    if path.startswith('~'):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    57
        return os.path.expanduser(path)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    58
    return path
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    59
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    60
def is_dir(path):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    61
    """Checks if ``path`` is a directory.
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    62
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    63
    Throws a `VMMException` if ``path`` is not a directory.
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    64
    """
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    65
    path = expand_path(path)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    66
    if not os.path.isdir(path):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    67
        raise VMMException(_(u'“%s” is not a directory') % get_unicode(path),
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    68
                           NO_SUCH_DIRECTORY)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    69
    return path
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    70
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    71
def exec_ok(binary):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    72
    """Checks if the ``binary`` exists and if it is executable.
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    73
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    74
    Throws a `VMMException` if the ``binary`` isn't a file or is not
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    75
    executable.
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    76
    """
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    77
    binary = expand_path(binary)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    78
    if not os.path.isfile(binary):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    79
        raise VMMException(_(u'“%s” is not a file') % get_unicode(binary),
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    80
                           NO_SUCH_BINARY)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    81
    if not os.access(binary, os.X_OK):
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    82
        raise VMMException(_(u'File is not executable: “%s”') %
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    83
                           get_unicode(binary), NOT_EXECUTABLE)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    84
    return binary
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    85
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    86
def idn2ascii(domainname):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    87
    """Converts the idn domain name `domainname` into punycode."""
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    88
    return '.'.join([ToASCII(lbl) for lbl in domainname.split('.') if lbl])
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    89
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    90
def ace2idna(domainname):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    91
    """Converts the domain name `domainname` from ACE according to IDNA."""
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    92
    return u'.'.join([ToUnicode(lbl) for lbl in domainname.split('.') if lbl])
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    93
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    94
def chk_domainname(domainname):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    95
    """Returns the validated domain name `domainname`.
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    96
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    97
    It also converts the name of the domain from IDN to ASCII, if necessary.
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    98
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    99
    Throws an VMMException, if the domain name is too long or doesn't look
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   100
    like a valid domain name (label.label.label).
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   101
    """
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   102
    if not re.match(RE_ASCII_CHARS, domainname):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   103
        domainname = idn2ascii(domainname)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   104
    if len(domainname) > 255:
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   105
        raise VMMException(_(u'The domain name is too long.'), DOMAIN_TOO_LONG)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   106
    if not re.match(RE_DOMAIN, domainname):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   107
        raise VMMException(_(u'The domain name “%s” is invalid.') % domainname,
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   108
                           DOMAIN_INVALID)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   109
    return domainname