VirtualMailManager/__init__.py
author Pascal Volk <neverseen@users.sourceforge.net>
Fri, 16 Apr 2010 03:58:07 +0000
branchv0.6.x
changeset 254 8aecc83a0d32
parent 253 58d1b6f41664
child 256 ae80282301a3
permissions -rw-r--r--
VMM: moved functions ace2idna(), check_domainname(), idn2ascii() and relevant parts to the Domain module. Adjusted imports in modules AliasDomain, EmailAddress and Handler.
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.
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 208
diff changeset
     4
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 208
diff changeset
     5
"""
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 208
diff changeset
     6
    VirtualMailManager
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 208
diff changeset
     7
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 208
diff changeset
     8
    VirtualMailManager package initialization code
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 208
diff changeset
     9
"""
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    10
188
cf1b5f22dbd2 VMM/Handler: __init__ accepts now a config_type ('default'||'cli').
Pascal Volk <neverseen@users.sourceforge.net>
parents: 187
diff changeset
    11
import gettext
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    12
import os
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    13
import locale
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    14
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    15
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    16
from VirtualMailManager.constants.ERROR import \
254
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 253
diff changeset
    17
     NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
    18
from VirtualMailManager.constants.version import __author__, __date__, \
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
    19
     __version__
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    20
from VirtualMailManager.errors import VMMError
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    21
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    22
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    23
__all__ = [
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__',
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 208
diff changeset
    26
    # defined stuff
254
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 253
diff changeset
    27
    'ENCODING', 'exec_ok', 'expand_path', 'get_unicode', 'is_dir',
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    28
]
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    29
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    30
152
3dbee02711cd Ignore unsupported locale setting and silently fall back to 'C'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 141
diff changeset
    31
# 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
    32
# 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
    33
try:
3dbee02711cd Ignore unsupported locale setting and silently fall back to 'C'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 141
diff changeset
    34
    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
    35
except locale.Error:
3dbee02711cd Ignore unsupported locale setting and silently fall back to 'C'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 141
diff changeset
    36
    locale.setlocale(locale.LC_ALL, 'C')
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    37
ENCODING = locale.nl_langinfo(locale.CODESET)
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    38
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
    39
gettext.install('vmm', '/usr/local/share/locale', unicode=1)
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
    40
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
    41
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
    42
_ = lambda msg: msg
188
cf1b5f22dbd2 VMM/Handler: __init__ accepts now a config_type ('default'||'cli').
Pascal Volk <neverseen@users.sourceforge.net>
parents: 187
diff changeset
    43
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
    44
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    45
def get_unicode(string):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    46
    """Converts `string` to `unicode`, if necessary."""
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    47
    if isinstance(string, unicode):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    48
        return string
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    49
    return unicode(string, ENCODING, 'replace')
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    50
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
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
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
    60
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    61
def is_dir(path):
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 208
diff changeset
    62
    """Checks if `path` is a directory.
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    63
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    64
    Throws a `VMMError` if `path` is not a directory.
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
    65
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    66
    """
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    67
    path = expand_path(path)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    68
    if not os.path.isdir(path):
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    69
        raise VMMError(_(u"'%s' is not a directory") % get_unicode(path),
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    70
                       NO_SUCH_DIRECTORY)
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    71
    return path
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    72
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
    73
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    74
def exec_ok(binary):
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 208
diff changeset
    75
    """Checks if the `binary` exists and if it is executable.
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    76
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    77
    Throws a `VMMError` if the `binary` isn't a file or is not
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    78
    executable.
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
    79
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    80
    """
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    81
    binary = expand_path(binary)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    82
    if not os.path.isfile(binary):
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    83
        raise VMMError(_(u"'%s' is not a file") % get_unicode(binary),
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    84
                       NO_SUCH_BINARY)
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    85
    if not os.access(binary, os.X_OK):
254
8aecc83a0d32 VMM: moved functions ace2idna(), check_domainname(), idn2ascii()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 253
diff changeset
    86
        raise VMMError(_(u"File is not executable: '%s'") %
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    87
                       get_unicode(binary), NOT_EXECUTABLE)
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    88
    return binary
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    89
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
    90
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
    91
del _