VirtualMailManager/__init__.py
author Pascal Volk <neverseen@users.sourceforge.net>
Fri, 23 Apr 2010 03:18:44 +0000
branchv0.6.x
changeset 261 1c2241dde942
parent 256 ae80282301a3
child 262 6eea85d8b91d
permissions -rw-r--r--
setup.py: Added sub-package pycompat to the packages list.
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
256
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    27
    'ENCODING', 'Configuration', 'exec_ok', 'expand_path', 'get_unicode',
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    28
    'is_dir', 'set_configuration',
185
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
256
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    40
Configuration = None
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
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
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
    43
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
    44
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
    45
_ = 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
    46
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
    47
256
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    48
def set_configuration(cfg_obj):
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    49
    """Assigns the *cfg_obj* to the global `Configuration`.
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    50
    *cfg_obj* has to be a `VirtualMailManager.Config.Config` instance."""
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    51
    from VirtualMailManager.Config import Config
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    52
    assert isinstance(cfg_obj, Config)
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    53
    global Configuration
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    54
    Configuration = cfg_obj
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    55
ae80282301a3 VMM: added 'Configuration' variable and set_configuration().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 254
diff changeset
    56
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    57
def get_unicode(string):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    58
    """Converts `string` to `unicode`, if necessary."""
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    59
    if isinstance(string, unicode):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    60
        return string
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    61
    return unicode(string, ENCODING, 'replace')
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    62
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
    63
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    64
def expand_path(path):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    65
    """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
    66
    if path.startswith('.'):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    67
        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
    68
    if path.startswith('~'):
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    69
        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
    70
    return path
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    71
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
    72
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    73
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
    74
    """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
    75
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    76
    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
    77
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    78
    """
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    79
    path = expand_path(path)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    80
    if not os.path.isdir(path):
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    81
        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
    82
                       NO_SUCH_DIRECTORY)
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    83
    return path
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    84
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
    85
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    86
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
    87
    """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
    88
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    89
    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
    90
    executable.
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
    91
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    92
    """
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    93
    binary = expand_path(binary)
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    94
    if not os.path.isfile(binary):
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    95
        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
    96
                       NO_SUCH_BINARY)
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
    97
    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
    98
        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
    99
                       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
   100
    return binary
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   101
199
0684790fff7c VMM: renamed function chk_domainname() -> check_domainname().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   102
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
   103
del _