VirtualMailManager/Config.py
author Pascal Volk <neverseen@users.sourceforge.net>
Thu, 29 Apr 2010 03:38:19 +0000
branchv0.6.x
changeset 274 45ec5c3cfef4
parent 272 446483386914
child 280 db35d2eec518
permissions -rw-r--r--
VMM/password: added small output check on _dovecotpw().
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: 138
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.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
200
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
     5
"""
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
     6
    VirtualMailManager.Config
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
     7
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
     8
    VMM's configuration module for simplified configuration access.
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
"""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
264
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
    11
import re
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    12
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    13
from ConfigParser import \
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    14
     Error, MissingSectionHeaderError, NoOptionError, NoSectionError, \
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    15
     ParsingError, RawConfigParser
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
    16
from cStringIO import StringIO# TODO: move interactive stff to cli
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
    17
264
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
    18
from VirtualMailManager.common import exec_ok, get_unicode, is_dir, version_hex
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    19
from VirtualMailManager.constants.ERROR import CONF_ERROR
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    20
from VirtualMailManager.errors import ConfigError
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    22
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    23
_ = lambda msg: msg
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    24
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    25
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    26
class BadOptionError(Error):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    27
    """Raised when a option isn't in the format 'section.option'."""
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    28
    pass
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    29
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    30
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    31
class ConfigValueError(Error):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    32
    """Raised when creating or validating of new values fails."""
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    33
    pass
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    34
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    35
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    36
class NoDefaultError(Error):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    37
    """Raised when the requested option has no default value."""
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    38
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    39
    def __init__(self, section, option):
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 201
diff changeset
    40
        Error.__init__(self, 'Option %r in section %r has no default value' %
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 201
diff changeset
    41
                             (option, section))
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    42
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    43
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    44
class LazyConfig(RawConfigParser):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    45
    """The **lazy** derivate of the `RawConfigParser`.
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
    46
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    47
    There are two additional getters:
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
    48
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
    49
    `pget()`
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
    50
      The polymorphic getter, which returns a option's value with the
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
    51
      appropriate type.
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
    52
    `dget()`
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
    53
      Like `LazyConfig.pget()`, but returns the option's default, from
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
    54
      `LazyConfig._cfg['sectionname']['optionname'].default`, if the
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
    55
      option is not configured in a ini-like configuration file.
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    56
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    57
    `set()` differs from `RawConfigParser`'s `set()` method. `set()`
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    58
    takes the `section` and `option` arguments combined to a single
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    59
    string in the form "section.option".
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    60
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    61
    """
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 201
diff changeset
    62
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    63
    def __init__(self):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    64
        RawConfigParser.__init__(self)
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    65
        self._modified = False
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    66
        # sample _cfg dict.  Create your own in your derived class.
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    67
        self._cfg = {
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    68
            'sectionname': {
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    69
                'optionname': LazyConfigOption(int, 1, self.getint),
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    70
            }
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    71
        }
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    72
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    73
    def bool_new(self, value):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    74
        """Converts the string `value` into a `bool` and returns it.
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    75
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
    76
        | '1', 'on', 'yes' and 'true' will become `True`
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
    77
        | '0', 'off', 'no' and 'false' will become `False`
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    78
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
    79
        Throws a `ConfigValueError` for all other values, except bools.
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    80
        """
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    81
        if isinstance(value, bool):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    82
            return value
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    83
        if value.lower() in self._boolean_states:
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    84
            return self._boolean_states[value.lower()]
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    85
        else:
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 201
diff changeset
    86
            raise ConfigValueError(_(u"Not a boolean: '%s'") %
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    87
                                   get_unicode(value))
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
    88
206
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
    89
    def getboolean(self, section, option):
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    90
        """Returns the boolean value of the option, in the given
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    91
        section.
206
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
    92
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
    93
        For a boolean True, the value must be set to '1', 'on', 'yes',
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
    94
        'true' or True. For a boolean False, the value must set to '0',
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
    95
        'off', 'no', 'false' or False.
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    96
        If the option has another value assigned this method will raise
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    97
        a ValueError.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
    98
206
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
    99
        """
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
   100
        # if the setting was modified it may be still a boolean value lets see
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
   101
        tmp = self.get(section, option)
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
   102
        if isinstance(tmp, bool):
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 201
diff changeset
   103
            return tmp
211
0b129678cfe1 VMM/Config: LazyConfig.getboolean(), convert the value in our getboolean.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
   104
        if not tmp.lower() in self._boolean_states:
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   105
            raise ValueError('Not a boolean: %s' % tmp)
211
0b129678cfe1 VMM/Config: LazyConfig.getboolean(), convert the value in our getboolean.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 209
diff changeset
   106
        return self._boolean_states[tmp.lower()]
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   107
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   108
    def _get_section_option(self, section_option):
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   109
        """splits ``section_option`` (section.option) in two parts and
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   110
        returns them as list ``[section, option]``, if:
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   111
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   112
          * it likes the format of ``section_option``
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   113
          * the ``section`` is known
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   114
          * the ``option`` is known
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   115
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   116
        Else one of the following exceptions will be thrown:
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   117
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   118
          * `BadOptionError`
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   119
          * `NoSectionError`
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   120
          * `NoOptionError`
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   121
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
   122
        """
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   123
        sect_opt = section_option.lower().split('.')
200
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   124
        # TODO: cache it
264
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   125
        if len(sect_opt) != 2:  # do we need a regexp to check the format?
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   126
            raise BadOptionError(
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 201
diff changeset
   127
                        _(u"Bad format: '%s' - expected: section.option") %
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 201
diff changeset
   128
                                 get_unicode(section_option))
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   129
        if not sect_opt[0] in self._cfg:
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   130
            raise NoSectionError(sect_opt[0])
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   131
        if not sect_opt[1] in self._cfg[sect_opt[0]]:
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   132
            raise NoOptionError(sect_opt[1], sect_opt[0])
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   133
        return sect_opt
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   134
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   135
    def items(self, section):
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   136
        """returns an iterable that returns key, value ``tuples`` from
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   137
        the given ``section``.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   138
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   139
        """
264
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   140
        if section in self._sections:  # check if the section was parsed
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   141
            sect = self._sections[section]
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   142
        elif not section in self._cfg:
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   143
            raise NoSectionError(section)
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   144
        else:
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   145
            return ((k, self._cfg[section][k].default) \
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   146
                    for k in self._cfg[section].iterkeys())
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   147
        # still here? Get defaults and merge defaults with configured setting
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   148
        defaults = dict((k, self._cfg[section][k].default) \
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   149
                        for k in self._cfg[section].iterkeys())
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   150
        defaults.update(sect)
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   151
        if '__name__' in defaults:
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   152
            del defaults['__name__']
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   153
        return defaults.iteritems()
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   154
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   155
    def dget(self, option):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   156
        """Returns the value of the `option`.
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
   157
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   158
        If the option could not be found in the configuration file, the
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   159
        configured default value, from ``LazyConfig._cfg`` will be
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   160
        returned.
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
   161
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   162
        Arguments:
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
   163
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   164
        `option` : string
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   165
            the configuration option in the form "section.option"
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   166
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   167
        Throws a `NoDefaultError`, if no default value was passed to
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   168
        `LazyConfigOption.__init__()` for the `option`.
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   169
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   170
        """
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   171
        section, option = self._get_section_option(option)
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   172
        try:
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   173
            return self._cfg[section][option].getter(section, option)
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   174
        except (NoSectionError, NoOptionError):
264
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   175
            if not self._cfg[section][option].default is None:  # may be False
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   176
                return self._cfg[section][option].default
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   177
            else:
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   178
                raise NoDefaultError(section, option)
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   179
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   180
    def pget(self, option):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   181
        """Returns the value of the `option`."""
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   182
        section, option = self._get_section_option(option)
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   183
        return self._cfg[section][option].getter(section, option)
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   184
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   185
    def set(self, option, value):
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   186
        """Set the `value` of the `option`.
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
   187
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   188
        Throws a `ValueError` if `value` couldn't be converted using
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   189
        `LazyConfigOption.cls`.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   190
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   191
        """
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   192
        # pylint: disable-msg=W0221
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   193
        # @pylint: _L A Z Y_
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   194
        section, option = self._get_section_option(option)
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   195
        val = self._cfg[section][option].cls(value)
200
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   196
        if self._cfg[section][option].validate:
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   197
            val = self._cfg[section][option].validate(val)
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   198
        if not RawConfigParser.has_section(self, section):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   199
            self.add_section(section)
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   200
        RawConfigParser.set(self, section, option, val)
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   201
        self._modified = True
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   202
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   203
    def has_section(self, section):
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   204
        """Checks if `section` is a known configuration section."""
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
   205
        return section.lower() in self._cfg
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   206
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   207
    def has_option(self, option):
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   208
        """Checks if the option (section.option) is a known
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   209
        configuration option.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   210
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   211
        """
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   212
        # pylint: disable-msg=W0221
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   213
        # @pylint: _L A Z Y_
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   214
        try:
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   215
            self._get_section_option(option)
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   216
            return True
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   217
        except(BadOptionError, NoSectionError, NoOptionError):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   218
            return False
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   219
207
95be8f62bc0c VMM/Config: moved Config.sections() to class LazyConfig.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 206
diff changeset
   220
    def sections(self):
95be8f62bc0c VMM/Config: moved Config.sections() to class LazyConfig.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 206
diff changeset
   221
        """Returns an iterator object for all configuration sections."""
95be8f62bc0c VMM/Config: moved Config.sections() to class LazyConfig.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 206
diff changeset
   222
        return self._cfg.iterkeys()
95be8f62bc0c VMM/Config: moved Config.sections() to class LazyConfig.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 206
diff changeset
   223
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   224
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   225
class LazyConfigOption(object):
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   226
    """A simple container class for configuration settings.
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   227
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   228
    `LazyConfigOption` instances are required by `LazyConfig` instances,
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   229
    and instances of classes derived from `LazyConfig`, like the
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   230
    `Config` class.
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   231
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   232
    """
200
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   233
    __slots__ = ('__cls', '__default', '__getter', '__validate')
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   234
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   235
    def __init__(self, cls, default, getter, validate=None):
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   236
        """Creates a new `LazyConfigOption` instance.
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   237
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   238
        Arguments:
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   239
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   240
        `cls` : type
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   241
          The class/type of the option's value
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   242
        `default`
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   243
          Default value of the option. Use ``None`` if the option should
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   244
          not have a default value.
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   245
        `getter` : callable
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   246
          A method's name of `RawConfigParser` and derived classes, to
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   247
          get a option's value, e.g. `self.getint`.
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   248
        `validate` : NoneType or a callable
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   249
          None or any method, that takes one argument, in order to
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   250
          check the value, when `LazyConfig.set()` is called.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   251
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   252
        """
200
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   253
        self.__cls = cls
264
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   254
        if not default is None:  # enforce the type of the default value
201
dbb0f7ed7858 VMM/Config: LazyConfigOption.__init__() cast 'default' to 'cls'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 200
diff changeset
   255
            self.__default = self.__cls(default)
dbb0f7ed7858 VMM/Config: LazyConfigOption.__init__() cast 'default' to 'cls'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 200
diff changeset
   256
        else:
dbb0f7ed7858 VMM/Config: LazyConfigOption.__init__() cast 'default' to 'cls'.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 200
diff changeset
   257
            self.__default = default
200
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   258
        if not callable(getter):
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   259
            raise TypeError('getter has to be a callable, got a %r' %
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   260
                            getter.__class__.__name__)
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   261
        self.__getter = getter
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   262
        if validate and not callable(validate):
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   263
            raise TypeError('validate has to be callable or None, got a %r' %
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   264
                            validate.__class__.__name__)
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   265
        self.__validate = validate
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   266
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   267
    @property
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   268
    def cls(self):
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   269
        """The class of the option's value e.g. `str`, `unicode` or
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   270
        `bool`.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   271
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   272
        """
200
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   273
        return self.__cls
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   274
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   275
    @property
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   276
    def default(self):
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   277
        """The option's default value, may be `None`"""
200
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   278
        return self.__default
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   279
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   280
    @property
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   281
    def getter(self):
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   282
        """The getter method or function to get the option's value"""
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   283
        return self.__getter
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   284
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   285
    @property
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   286
    def validate(self):
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   287
        """A method or function to validate the value"""
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   288
        return self.__validate
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   289
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   290
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   291
class Config(LazyConfig):
188
cf1b5f22dbd2 VMM/Handler: __init__ accepts now a config_type ('default'||'cli').
Pascal Volk <neverseen@users.sourceforge.net>
parents: 187
diff changeset
   292
    """This class is for reading vmm's configuration file."""
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   293
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   294
    def __init__(self, filename):
49
9bd033177377 * 'VirtualMailManager/Config.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   295
        """Creates a new Config instance
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   296
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   297
        Arguments:
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
   298
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   299
        `filename` : str
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   300
          path to the configuration file
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   301
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   302
        """
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   303
        LazyConfig.__init__(self)
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   304
        self._cfg_filename = filename
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   305
        self._cfg_file = None
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   306
        self.__missing = {}
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   307
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   308
        LCO = LazyConfigOption
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   309
        bool_t = self.bool_new
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   310
        self._cfg = {
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   311
            'account': {
206
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
   312
                'delete_directory': LCO(bool_t, False, self.getboolean),
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   313
                'directory_mode': LCO(int, 448, self.getint),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   314
                'disk_usage': LCO(bool_t, False, self.getboolean),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   315
                'password_length': LCO(int, 8, self.getint),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   316
                'random_password': LCO(bool_t, False, self.getboolean),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   317
                'imap': LCO(bool_t, True, self.getboolean),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   318
                'pop3': LCO(bool_t, True, self.getboolean),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   319
                'sieve': LCO(bool_t, True, self.getboolean),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   320
                'smtp': LCO(bool_t, True, self.getboolean),
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   321
            },
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   322
            'bin': {
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   323
                'dovecotpw': LCO(str, '/usr/sbin/dovecotpw', self.get,
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   324
                                 exec_ok),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   325
                'du': LCO(str, '/usr/bin/du', self.get, exec_ok),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   326
                'postconf': LCO(str, '/usr/sbin/postconf', self.get, exec_ok),
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   327
            },
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   328
            'database': {
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   329
                'host': LCO(str, 'localhost', self.get),
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   330
                'name': LCO(str, 'mailsys', self.get),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   331
                'pass': LCO(str, None, self.get),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   332
                'user': LCO(str, None, self.get),
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   333
            },
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   334
            'domain': {
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   335
                'auto_postmaster': LCO(bool_t, True, self.getboolean),
206
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
   336
                'delete_directory': LCO(bool_t, False, self.getboolean),
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   337
                'directory_mode': LCO(int, 504, self.getint),
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   338
                'force_deletion': LCO(bool_t, False, self.getboolean),
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   339
            },
229
0fb2f12648a7 vmm.cfg: renamed maildir.folders to mailbox.folders. maildir.name
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   340
            'mailbox': {
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   341
                'folders': LCO(str, 'Drafts:Sent:Templates:Trash', self.get),
229
0fb2f12648a7 vmm.cfg: renamed maildir.folders to mailbox.folders. maildir.name
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   342
                'format': LCO(str, 'maildir', self.get),
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   343
            },
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   344
            'misc': {
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   345
                'base_directory': LCO(str, '/srv/mail', self.get, is_dir),
264
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   346
                'dovecot_version': LCO(str, '1.2.11', self.hexversion,
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   347
                                       check_version_format),
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   348
                'password_scheme': LCO(str, 'CRAM-MD5', self.get,
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   349
                                       self.known_scheme),
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   350
                'transport': LCO(str, 'dovecot:', self.get),
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   351
            },
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   352
        }
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   353
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   354
    def load(self):
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   355
        """Loads the configuration, read only.
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   356
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   357
        Raises a ConfigError if the configuration syntax is
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   358
        invalid.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   359
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   360
        """
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   361
        try:
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   362
            self._cfg_file = open(self._cfg_filename, 'r')
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   363
            self.readfp(self._cfg_file)
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   364
        except (MissingSectionHeaderError, ParsingError), err:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   365
            raise ConfigError(str(err), CONF_ERROR)
175
b241272eb1bd VMM/VMM: allow configure subcommand also with missing settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 174
diff changeset
   366
        finally:
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   367
            if self._cfg_file and not self._cfg_file.closed:
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   368
                self._cfg_file.close()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   369
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   370
    def check(self):
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   371
        """Performs a configuration check.
128
cf8116625866 Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 120
diff changeset
   372
264
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   373
        Raises a ConfigError if settings w/o a default value are missed.
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   374
        Or a ConfigValueError if 'misc.dovecot_version' has the wrong
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   375
        format.
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   376
        """
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
   377
        # TODO: There are only two settings w/o defaults.
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
   378
        #       So there is no need for cStringIO
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   379
        if not self.__chk_cfg():
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   380
            errmsg = StringIO()
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   381
            errmsg.write(_(u'Missing options, which have no default value.\n'))
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   382
            errmsg.write(_(u'Using configuration file: %s\n') %
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   383
                         self._cfg_filename)
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   384
            for section, options in self.__missing.iteritems():
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   385
                errmsg.write(_(u'* Section: %s\n') % section)
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   386
                for option in options:
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   387
                    errmsg.write((u'    %s\n') % option)
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   388
            raise ConfigError(errmsg.getvalue(), CONF_ERROR)
264
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   389
        check_version_format(self.get('misc', 'dovecot_version'))
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   390
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   391
    def hexversion(self, section, option):
272
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 270
diff changeset
   392
        """Converts the version number (e.g.: 1.2.3) from the *option*'s
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 270
diff changeset
   393
        value to an int."""
264
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   394
        return version_hex(self.get(section, option))
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   395
272
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 270
diff changeset
   396
    def install(self):
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 270
diff changeset
   397
        """Installs the dget() method as ``cfg_dget`` in the built-in
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 270
diff changeset
   398
        namespace."""
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 270
diff changeset
   399
        import __builtin__
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 270
diff changeset
   400
        assert 'cfg_dget' not in __builtin__.__dict__
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 270
diff changeset
   401
        __builtin__.__dict__['cfg_dget'] = self.dget
446483386914 VMM/Config: Added method Config.install() -> global cfg_dget().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 270
diff changeset
   402
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   403
    def known_scheme(self, scheme):
209
c705a9e38962 VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 207
diff changeset
   404
        """Converts `scheme` to upper case and checks if is known by
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   405
        Dovecot (listed in VirtualMailManager.SCHEMES).
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
   406
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   407
        Throws a `ConfigValueError` if the scheme is not listed in
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   408
        VirtualMailManager.SCHEMES.
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   409
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   410
        """
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   411
        scheme = scheme.upper()
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   412
        # TODO: VMM.SCHEMES
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   413
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   414
    def unicode(self, section, option):
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   415
        """Returns the value of the `option` from `section`, converted
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   416
        to Unicode.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   417
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   418
        """
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   419
        return get_unicode(self.get(section, option))
69
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 68
diff changeset
   420
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   421
    def __chk_cfg(self):
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   422
        """Checks all section's options for settings w/o a default
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   423
        value.
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
   424
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   425
        Returns `True` if everything is fine, else `False`.
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   426
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   427
        """
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   428
        errors = False
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   429
        for section in self._cfg.iterkeys():
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   430
            missing = []
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   431
            for option, value in self._cfg[section].iteritems():
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   432
                if (value.default is None and
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   433
                    not RawConfigParser.has_option(self, section, option)):
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   434
                    missing.append(option)
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   435
                    errors = True
200
983cf98d5881 VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 188
diff changeset
   436
            if missing:
174
974bafa59330 VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 173
diff changeset
   437
                self.__missing[section] = missing
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   438
        return not errors
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   439
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   440
264
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   441
def check_version_format(version_string):
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   442
    """Check if the *version_string* has the proper format, e.g.: '1.2.3'.
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   443
    Returns the validated version string if it has the expected format.
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   444
    Otherwise a `ConfigValueError` will be raised.
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   445
    """
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   446
    version_re = r'^\d+\.\d+\.(?:\d+|(?:alpha|beta|rc)\d+)$'
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   447
    if not re.match(version_re, version_string):
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   448
        raise ConfigValueError(_(u"Not a valid Dovecot version: '%s'") %
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   449
                               get_unicode(version_string))
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   450
    return version_string
04fea4d8b900 Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents: 262
diff changeset
   451
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 211
diff changeset
   452
del _