author | Pascal Volk <user@localhost.localdomain.org> |
Sun, 20 Jan 2013 14:42:52 +0000 | |
branch | v0.7.x |
changeset 689 | dab0630947a2 |
parent 676 | 2bc11dada296 |
child 694 | b1bfd4d1d9c0 |
permissions | -rw-r--r-- |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
675
d24f094d1cb5
Updated copyright notices to include the year 2013.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
2 |
# Copyright (c) 2007 - 2013, Pascal Volk |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
3 |
# See COPYING for distribution information. |
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
4 |
""" |
320
011066435e6f
VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
316
diff
changeset
|
5 |
VirtualMailManager.config |
301
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
6 |
~~~~~~~~~~~~~~~~~~~~~~~~~ |
200
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 |
|
674
995203a101e1
VMM/config: Post-2to3 fix. PEP8-ified imports.
Pascal Volk <user@localhost.localdomain.org>
parents:
670
diff
changeset
|
11 |
import collections |
995203a101e1
VMM/config: Post-2to3 fix. PEP8-ified imports.
Pascal Volk <user@localhost.localdomain.org>
parents:
670
diff
changeset
|
12 |
|
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
13 |
from configparser import \ |
187
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 |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
16 |
from io import StringIO |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
17 |
|
348
ca7575401549
VMM/config: Use common.VERSION_RE, instead of defining the regexp two times.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
342
diff
changeset
|
18 |
from VirtualMailManager.common import VERSION_RE, \ |
384
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
19 |
exec_ok, expand_path, get_unicode, lisdir, size_in_bytes, version_hex |
316
31d8931dc535
VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
304
diff
changeset
|
20 |
from VirtualMailManager.constants import CONF_ERROR |
287
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
21 |
from VirtualMailManager.errors import ConfigError, VMMError |
301
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
22 |
from VirtualMailManager.maillocation import known_format |
287
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
23 |
from VirtualMailManager.password import verify_scheme as _verify_scheme |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
24 |
|
496
17f2c5b5098e
VMM/config: Fixed a typo: s/DB_MUDULES/DB_MODULES/g.
Pascal Volk <user@localhost.localdomain.org>
parents:
481
diff
changeset
|
25 |
DB_MODULES = ('psycopg2', 'pypgsql') |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
26 |
DB_SSL_MODES = ('allow', 'disabled', 'prefer', 'require', 'verify-ca', |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
27 |
'verify-full') |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
28 |
|
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
29 |
_ = lambda msg: msg |
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
30 |
|
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
31 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
32 |
class BadOptionError(Error): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
33 |
"""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
|
34 |
pass |
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 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
37 |
class ConfigValueError(Error): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
38 |
"""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
|
39 |
pass |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
40 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
41 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
42 |
class NoDefaultError(Error): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
43 |
"""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
|
44 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
45 |
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
|
46 |
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
|
47 |
(option, section)) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
48 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
49 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
50 |
class LazyConfig(RawConfigParser): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
51 |
"""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
|
52 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
53 |
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
|
54 |
|
209
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
55 |
`pget()` |
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
56 |
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
|
57 |
appropriate type. |
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
58 |
`dget()` |
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
59 |
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
|
60 |
`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
|
61 |
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
|
62 |
|
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
63 |
`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
|
64 |
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
|
65 |
string in the form "section.option". |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
66 |
""" |
204
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
201
diff
changeset
|
67 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
68 |
def __init__(self): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
69 |
RawConfigParser.__init__(self) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
70 |
self._modified = False |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
71 |
# 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
|
72 |
self._cfg = { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
73 |
'sectionname': { |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
74 |
'optionname': LazyConfigOption(int, 1, self.getint), |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
75 |
} |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
76 |
} |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
77 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
78 |
def bool_new(self, value): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
79 |
"""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
|
80 |
|
209
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
81 |
| '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
|
82 |
| '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
|
83 |
|
209
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
84 |
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
|
85 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
86 |
if isinstance(value, bool): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
87 |
return value |
650
429ba58bc302
VMM/config: s/_boolean_states/BOOLEAN_STATES/g
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
88 |
if value.lower() in self.BOOLEAN_STATES: |
429ba58bc302
VMM/config: s/_boolean_states/BOOLEAN_STATES/g
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
89 |
return self.BOOLEAN_STATES[value.lower()] |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
90 |
else: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
91 |
raise ConfigValueError(_("Not a boolean: '%s'") % |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
92 |
get_unicode(value)) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
93 |
|
206
da07dd944ad1
VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
204
diff
changeset
|
94 |
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
|
95 |
"""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
|
96 |
section. |
206
da07dd944ad1
VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
204
diff
changeset
|
97 |
|
da07dd944ad1
VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
204
diff
changeset
|
98 |
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
|
99 |
'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
|
100 |
'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
|
101 |
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
|
102 |
a ValueError. |
206
da07dd944ad1
VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
204
diff
changeset
|
103 |
""" |
da07dd944ad1
VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
204
diff
changeset
|
104 |
# 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
|
105 |
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
|
106 |
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
|
107 |
return tmp |
650
429ba58bc302
VMM/config: s/_boolean_states/BOOLEAN_STATES/g
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
108 |
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
|
109 |
raise ValueError('Not a boolean: %s' % tmp) |
650
429ba58bc302
VMM/config: s/_boolean_states/BOOLEAN_STATES/g
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
110 |
return self.BOOLEAN_STATES[tmp.lower()] |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
111 |
|
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
112 |
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
|
113 |
"""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
|
114 |
returns them as list ``[section, option]``, if: |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
115 |
|
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
116 |
* 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
|
117 |
* the ``section`` is known |
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
118 |
* the ``option`` is known |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
119 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
120 |
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
|
121 |
|
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
122 |
* `BadOptionError` |
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
123 |
* `NoSectionError` |
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
124 |
* `NoOptionError` |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
125 |
""" |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
126 |
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
|
127 |
# TODO: cache it |
342
b0c971f943dc
VMM/config: LazyConfig._get_section_option check for empty section/option names.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
326
diff
changeset
|
128 |
if len(sect_opt) != 2 or not sect_opt[0] or not sect_opt[1]: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
129 |
raise BadOptionError(_("Bad format: '%s' - expected: " |
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
130 |
"section.option") % |
204
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
201
diff
changeset
|
131 |
get_unicode(section_option)) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
132 |
if not sect_opt[0] in self._cfg: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
133 |
raise NoSectionError(sect_opt[0]) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
134 |
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
|
135 |
raise NoOptionError(sect_opt[1], sect_opt[0]) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
136 |
return sect_opt |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
137 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
138 |
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
|
139 |
"""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
|
140 |
the given ``section``. |
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
141 |
""" |
264
04fea4d8b900
Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents:
262
diff
changeset
|
142 |
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
|
143 |
sect = self._sections[section] |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
144 |
elif not section in self._cfg: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
145 |
raise NoSectionError(section) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
146 |
else: |
611
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
568
diff
changeset
|
147 |
return ((k, self._cfg[section][k].default) |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
148 |
for k in self._cfg[section].keys()) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
149 |
# still here? Get defaults and merge defaults with configured setting |
611
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
568
diff
changeset
|
150 |
defaults = dict((k, self._cfg[section][k].default) |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
151 |
for k in self._cfg[section].keys()) |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
152 |
defaults.update(sect) |
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
153 |
if '__name__' in defaults: |
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
154 |
del defaults['__name__'] |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
155 |
return iter(defaults.items()) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
156 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
157 |
def dget(self, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
158 |
"""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
|
159 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
160 |
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
|
161 |
configured default value, from ``LazyConfig._cfg`` will be |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
162 |
returned. |
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 |
Arguments: |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
165 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
166 |
`option` : string |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
167 |
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
|
168 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
169 |
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
|
170 |
`LazyConfigOption.__init__()` for the `option`. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
171 |
""" |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
172 |
section, option = self._get_section_option(option) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
173 |
try: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
174 |
return self._cfg[section][option].getter(section, option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
175 |
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
|
176 |
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
|
177 |
return self._cfg[section][option].default |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
178 |
else: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
179 |
raise NoDefaultError(section, option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
180 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
181 |
def pget(self, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
182 |
"""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
|
183 |
section, option = self._get_section_option(option) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
184 |
return self._cfg[section][option].getter(section, option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
185 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
186 |
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
|
187 |
"""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
|
188 |
|
209
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
189 |
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
|
190 |
`LazyConfigOption.cls`. |
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
191 |
""" |
303
8dd3a107fd92
VMM/Config: Return mailbox.{folders,root} settings as Unicode.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
301
diff
changeset
|
192 |
# pylint: disable=W0221 |
216
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 |
""" |
303
8dd3a107fd92
VMM/Config: Return mailbox.{folders,root} settings as Unicode.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
301
diff
changeset
|
211 |
# pylint: disable=W0221 |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
215
diff
changeset
|
212 |
# @pylint: _L A Z Y_ |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
213 |
try: |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
214 |
self._get_section_option(option) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
215 |
return True |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
216 |
except(BadOptionError, NoSectionError, NoOptionError): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
217 |
return False |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
218 |
|
207
95be8f62bc0c
VMM/Config: moved Config.sections() to class LazyConfig.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
206
diff
changeset
|
219 |
def sections(self): |
95be8f62bc0c
VMM/Config: moved Config.sections() to class LazyConfig.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
206
diff
changeset
|
220 |
"""Returns an iterator object for all configuration sections.""" |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
221 |
return iter(self._cfg.keys()) |
207
95be8f62bc0c
VMM/Config: moved Config.sections() to class LazyConfig.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
206
diff
changeset
|
222 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
223 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
224 |
class LazyConfigOption(object): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
225 |
"""A simple container class for configuration settings. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
226 |
|
209
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
227 |
`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
|
228 |
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
|
229 |
`Config` class. |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
230 |
""" |
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
231 |
__slots__ = ('__cls', '__default', '__getter', '__validate') |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
232 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
233 |
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
|
234 |
"""Creates a new `LazyConfigOption` instance. |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
235 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
236 |
Arguments: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
237 |
|
209
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
238 |
`cls` : type |
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
239 |
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
|
240 |
`default` |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
241 |
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
|
242 |
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
|
243 |
`getter` : callable |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
244 |
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
|
245 |
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
|
246 |
`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
|
247 |
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
|
248 |
check the value, when `LazyConfig.set()` is called. |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
249 |
""" |
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
250 |
self.__cls = cls |
638
0de0b9e75c9f
VMM: Partial PEP-308-ification.
Pascal Volk <user@localhost.localdomain.org>
parents:
611
diff
changeset
|
251 |
self.__default = default if default is None else self.__cls(default) |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
252 |
if not isinstance(getter, collections.Callable): |
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
253 |
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
|
254 |
getter.__class__.__name__) |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
255 |
self.__getter = getter |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
256 |
if validate and not isinstance(validate, collections.Callable): |
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
257 |
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
|
258 |
validate.__class__.__name__) |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
259 |
self.__validate = validate |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
260 |
|
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
261 |
@property |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
262 |
def cls(self): |
303
8dd3a107fd92
VMM/Config: Return mailbox.{folders,root} settings as Unicode.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
301
diff
changeset
|
263 |
"""The class of the option's value e.g. `str`, `unicode` or `bool`.""" |
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
264 |
return self.__cls |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
265 |
|
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
266 |
@property |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
267 |
def default(self): |
209
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
268 |
"""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
|
269 |
return self.__default |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
270 |
|
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
271 |
@property |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
272 |
def getter(self): |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
273 |
"""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
|
274 |
return self.__getter |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
275 |
|
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
276 |
@property |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
277 |
def validate(self): |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
278 |
"""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
|
279 |
return self.__validate |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
280 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
281 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
282 |
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
|
283 |
"""This class is for reading vmm's configuration file.""" |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
284 |
|
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
285 |
def __init__(self, filename): |
49
9bd033177377
* 'VirtualMailManager/Config.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
48
diff
changeset
|
286 |
"""Creates a new Config instance |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
287 |
|
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
288 |
Arguments: |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
289 |
|
209
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
290 |
`filename` : str |
c705a9e38962
VMM{/Config}: reduced docstrings. Added doc to the repository.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
207
diff
changeset
|
291 |
path to the configuration file |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
292 |
""" |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
293 |
LazyConfig.__init__(self) |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
294 |
self._cfg_filename = filename |
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
295 |
self._cfg_file = None |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
296 |
self._missing = {} |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
297 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
298 |
LCO = LazyConfigOption |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
299 |
bool_t = self.bool_new |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
300 |
self._cfg = { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
301 |
'account': { |
206
da07dd944ad1
VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
204
diff
changeset
|
302 |
'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
|
303 |
'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
|
304 |
'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
|
305 |
'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
|
306 |
'random_password': LCO(bool_t, False, self.getboolean), |
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 |
'bin': { |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
309 |
'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
|
310 |
exec_ok), |
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
311 |
'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
|
312 |
'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
|
313 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
314 |
'database': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
315 |
'host': LCO(str, 'localhost', self.get), |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
316 |
'module': LCO(str, 'psycopg2', self.get, check_db_module), |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
317 |
'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
|
318 |
'pass': LCO(str, None, self.get), |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
319 |
'port': LCO(int, 5432, self.getint), |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
320 |
'sslmode': LCO(str, 'prefer', self.get, check_db_ssl_mode), |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
321 |
'user': LCO(str, None, self.get), |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
322 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
323 |
'domain': { |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
324 |
'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
|
325 |
'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
|
326 |
'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
|
327 |
'force_deletion': LCO(bool_t, False, self.getboolean), |
458
4ff0fa3ba0fa
VMM/config: Moved some options to section `domain':
Pascal Volk <neverseen@users.sourceforge.net>
parents:
384
diff
changeset
|
328 |
'imap': LCO(bool_t, True, self.getboolean), |
4ff0fa3ba0fa
VMM/config: Moved some options to section `domain':
Pascal Volk <neverseen@users.sourceforge.net>
parents:
384
diff
changeset
|
329 |
'pop3': LCO(bool_t, True, self.getboolean), |
4ff0fa3ba0fa
VMM/config: Moved some options to section `domain':
Pascal Volk <neverseen@users.sourceforge.net>
parents:
384
diff
changeset
|
330 |
'sieve': LCO(bool_t, True, self.getboolean), |
4ff0fa3ba0fa
VMM/config: Moved some options to section `domain':
Pascal Volk <neverseen@users.sourceforge.net>
parents:
384
diff
changeset
|
331 |
'smtp': LCO(bool_t, True, self.getboolean), |
4ff0fa3ba0fa
VMM/config: Moved some options to section `domain':
Pascal Volk <neverseen@users.sourceforge.net>
parents:
384
diff
changeset
|
332 |
'quota_bytes': LCO(str, '0', self.get_in_bytes, |
4ff0fa3ba0fa
VMM/config: Moved some options to section `domain':
Pascal Volk <neverseen@users.sourceforge.net>
parents:
384
diff
changeset
|
333 |
check_size_value), |
4ff0fa3ba0fa
VMM/config: Moved some options to section `domain':
Pascal Volk <neverseen@users.sourceforge.net>
parents:
384
diff
changeset
|
334 |
'quota_messages': LCO(int, 0, self.getint), |
4ff0fa3ba0fa
VMM/config: Moved some options to section `domain':
Pascal Volk <neverseen@users.sourceforge.net>
parents:
384
diff
changeset
|
335 |
'transport': LCO(str, 'dovecot:', self.get), |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
336 |
}, |
229
0fb2f12648a7
vmm.cfg: renamed maildir.folders to mailbox.folders. maildir.name
Pascal Volk <neverseen@users.sourceforge.net>
parents:
216
diff
changeset
|
337 |
'mailbox': { |
303
8dd3a107fd92
VMM/Config: Return mailbox.{folders,root} settings as Unicode.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
301
diff
changeset
|
338 |
'folders': LCO(str, 'Drafts:Sent:Templates:Trash', |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
339 |
self.str), |
301
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
340 |
'format': LCO(str, 'maildir', self.get, check_mailbox_format), |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
341 |
'root': LCO(str, 'Maildir', self.str), |
304
df0f7b22540c
VMM/Config: Added boolean option mailbox.subscribe.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
303
diff
changeset
|
342 |
'subscribe': LCO(bool_t, True, self.getboolean), |
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), |
291
7ef3f117a230
VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
346 |
'crypt_blowfish_rounds': LCO(int, 5, self.getint), |
7ef3f117a230
VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
347 |
'crypt_sha256_rounds': LCO(int, 5000, self.getint), |
7ef3f117a230
VMM/password: adapted Blowfish/SHA-256/SHA-512 crypt() to recent
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
348 |
'crypt_sha512_rounds': LCO(int, 5000, self.getint), |
286
e2046d47688b
VMM/Config: removed the default value of misc.dovecot_version.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
284
diff
changeset
|
349 |
'dovecot_version': LCO(str, None, self.hexversion, |
264
04fea4d8b900
Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents:
262
diff
changeset
|
350 |
check_version_format), |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
351 |
'password_scheme': LCO(str, 'CRAM-MD5', self.get, |
287
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
352 |
verify_scheme), |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
353 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
354 |
} |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
355 |
|
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
356 |
def load(self): |
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
357 |
"""Loads the configuration, read only. |
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
358 |
|
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
215
diff
changeset
|
359 |
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
|
360 |
invalid. |
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
361 |
""" |
658
ad967c4b7fe5
VMM{/cli}/config: Explicitly pass utf-8 as the encoding to open().
Pascal Volk <user@localhost.localdomain.org>
parents:
650
diff
changeset
|
362 |
with open(self._cfg_filename, 'r', encoding='utf-8') as self._cfg_file: |
642
4cd9d0a9f42f
Use the with statement for file operations.
Pascal Volk <user@localhost.localdomain.org>
parents:
641
diff
changeset
|
363 |
try: |
4cd9d0a9f42f
Use the with statement for file operations.
Pascal Volk <user@localhost.localdomain.org>
parents:
641
diff
changeset
|
364 |
self.readfp(self._cfg_file) |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
365 |
except (MissingSectionHeaderError, ParsingError) as err: |
642
4cd9d0a9f42f
Use the with statement for file operations.
Pascal Volk <user@localhost.localdomain.org>
parents:
641
diff
changeset
|
366 |
raise ConfigError(str(err), CONF_ERROR) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
367 |
|
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
368 |
def check(self): |
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
369 |
"""Performs a configuration check. |
128
cf8116625866
Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
120
diff
changeset
|
370 |
|
264
04fea4d8b900
Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents:
262
diff
changeset
|
371 |
Raises a ConfigError if settings w/o a default value are missed. |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
372 |
Or some settings have a invalid value. |
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
373 |
""" |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
374 |
def iter_dict(): |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
375 |
for section, options in self._missing.items(): |
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
376 |
errmsg.write(_('* Section: %s\n') % section) |
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
377 |
errmsg.writelines(' %s\n' % option for option in options) |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
378 |
self._missing.clear() |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
379 |
|
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
380 |
errmsg = None |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
381 |
self._chk_non_default() |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
382 |
miss_vers = 'misc' in self._missing and \ |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
383 |
'dovecot_version' in self._missing['misc'] |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
384 |
if self._missing: |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
385 |
errmsg = StringIO() |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
386 |
errmsg.write(_('Check of configuration file %s failed.\n') % |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
387 |
self._cfg_filename) |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
388 |
errmsg.write(_('Missing options, which have no default value.\n')) |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
389 |
iter_dict() |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
390 |
self._chk_possible_values(miss_vers) |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
391 |
if self._missing: |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
392 |
if not errmsg: |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
393 |
errmsg = StringIO() |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
394 |
errmsg.write(_('Check of configuration file %s failed.\n') % |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
395 |
self._cfg_filename) |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
396 |
errmsg.write(_('Invalid configuration values.\n')) |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
397 |
else: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
398 |
errmsg.write('\n' + _('Invalid configuration values.\n')) |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
399 |
iter_dict() |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
400 |
if errmsg: |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
215
diff
changeset
|
401 |
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
|
402 |
|
04fea4d8b900
Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents:
262
diff
changeset
|
403 |
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
|
404 |
"""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
|
405 |
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
|
406 |
return version_hex(self.get(section, option)) |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
407 |
|
384
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
408 |
def get_in_bytes(self, section, option): |
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
409 |
"""Converts the size value (e.g.: 1024k) from the *option*'s |
670
f374ef062c94
VMM/*: Post-2to3 fix. Updated some error messages/comments.
Pascal Volk <user@localhost.localdomain.org>
parents:
658
diff
changeset
|
410 |
value to a integer""" |
384
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
411 |
return size_in_bytes(self.get(section, option)) |
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
412 |
|
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
413 |
def str(self, section, option): |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
414 |
"""Returns the value of the `option` from `section`, converted |
287
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
415 |
to Unicode.""" |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
416 |
return get_unicode(self.get(section, option)) |
69
0c124160a991
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
68
diff
changeset
|
417 |
|
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
418 |
def _chk_non_default(self): |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
419 |
"""Checks all section's options for settings w/o a default |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
420 |
value. Missing items will be stored in _missing. |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
421 |
""" |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
422 |
for section in self._cfg.keys(): |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
423 |
missing = [] |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
424 |
for option, value in self._cfg[section].items(): |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
425 |
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
|
426 |
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
|
427 |
missing.append(option) |
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
428 |
if missing: |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
429 |
self._missing[section] = missing |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
430 |
|
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
431 |
def _chk_possible_values(self, miss_vers): |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
432 |
"""Check settings for which the possible values are known.""" |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
433 |
if not miss_vers: |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
434 |
value = self.get('misc', 'dovecot_version') |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
435 |
if not VERSION_RE.match(value): |
611
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
568
diff
changeset
|
436 |
self._missing['misc'] = ['version: ' + |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
437 |
_("Not a valid Dovecot version: '%s'") % value] |
359
7fa919dab42c
VMM/config: Added option mailbox.format to the configuration check.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
353
diff
changeset
|
438 |
# section database |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
439 |
db_err = [] |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
440 |
value = self.dget('database.module').lower() |
496
17f2c5b5098e
VMM/config: Fixed a typo: s/DB_MUDULES/DB_MODULES/g.
Pascal Volk <user@localhost.localdomain.org>
parents:
481
diff
changeset
|
441 |
if value not in DB_MODULES: |
611
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
568
diff
changeset
|
442 |
db_err.append('module: ' + |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
443 |
_("Unsupported database module: '%s'") % value) |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
444 |
if value == 'psycopg2': |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
445 |
value = self.dget('database.sslmode') |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
446 |
if value not in DB_SSL_MODES: |
611
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
568
diff
changeset
|
447 |
db_err.append('sslmode: ' + |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
448 |
_("Unknown pgsql SSL mode: '%s'") % value) |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
449 |
if db_err: |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
450 |
self._missing['database'] = db_err |
359
7fa919dab42c
VMM/config: Added option mailbox.format to the configuration check.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
353
diff
changeset
|
451 |
# section mailbox |
7fa919dab42c
VMM/config: Added option mailbox.format to the configuration check.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
353
diff
changeset
|
452 |
value = self.dget('mailbox.format') |
7fa919dab42c
VMM/config: Added option mailbox.format to the configuration check.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
353
diff
changeset
|
453 |
if not known_format(value): |
611
8e9b0046bc8f
PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents:
568
diff
changeset
|
454 |
self._missing['mailbox'] = ['format: ' + |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
455 |
_("Unsupported mailbox format: '%s'") % value] |
458
4ff0fa3ba0fa
VMM/config: Moved some options to section `domain':
Pascal Volk <neverseen@users.sourceforge.net>
parents:
384
diff
changeset
|
456 |
# section domain |
384
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
457 |
try: |
458
4ff0fa3ba0fa
VMM/config: Moved some options to section `domain':
Pascal Volk <neverseen@users.sourceforge.net>
parents:
384
diff
changeset
|
458 |
value = self.dget('domain.quota_bytes') |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
459 |
except (ValueError, TypeError) as err: |
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
460 |
self._missing['domain'] = ['quota_bytes: ' + str(err)] |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
461 |
|
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
462 |
|
326
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
322
diff
changeset
|
463 |
def is_dir(path): |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
322
diff
changeset
|
464 |
"""Check if the expanded path is a directory. When the expanded path |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
322
diff
changeset
|
465 |
is a directory the expanded path will be returned. Otherwise a |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
322
diff
changeset
|
466 |
ConfigValueError will be raised. |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
322
diff
changeset
|
467 |
""" |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
322
diff
changeset
|
468 |
path = expand_path(path) |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
322
diff
changeset
|
469 |
if lisdir(path): |
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
322
diff
changeset
|
470 |
return path |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
471 |
raise ConfigValueError(_("No such directory: %s") % get_unicode(path)) |
326
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
322
diff
changeset
|
472 |
|
8f8d9c4c8332
VMM/common: Replaced function is_dir() by lisdir().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
322
diff
changeset
|
473 |
|
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
474 |
def check_db_module(module): |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
475 |
"""Check if the *module* is a supported pgsql module.""" |
496
17f2c5b5098e
VMM/config: Fixed a typo: s/DB_MUDULES/DB_MODULES/g.
Pascal Volk <user@localhost.localdomain.org>
parents:
481
diff
changeset
|
476 |
if module.lower() in DB_MODULES: |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
477 |
return module |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
478 |
raise ConfigValueError(_("Unsupported database module: '%s'") % |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
479 |
get_unicode(module)) |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
480 |
|
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
481 |
|
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
482 |
def check_db_ssl_mode(ssl_mode): |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
483 |
"""Check if the *ssl_mode* is one of the SSL modes, known by pgsql.""" |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
484 |
if ssl_mode in DB_SSL_MODES: |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
485 |
return ssl_mode |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
486 |
raise ConfigValueError(_("Unknown pgsql SSL mode: '%s'") % |
353
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
487 |
get_unicode(ssl_mode)) |
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
488 |
|
2ae40cd0d213
VMM/config: Extended configuration check and raise only a ConfigError.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
348
diff
changeset
|
489 |
|
301
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
490 |
def check_mailbox_format(format): |
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
491 |
""" |
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
492 |
Check if the mailbox format *format* is supported. When the *format* |
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
493 |
is supported it will be returned, otherwise a `ConfigValueError` will |
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
494 |
be raised. |
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
495 |
""" |
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
496 |
format = format.lower() |
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
497 |
if known_format(format): |
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
498 |
return format |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
499 |
raise ConfigValueError(_("Unsupported mailbox format: '%s'") % |
301
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
500 |
get_unicode(format)) |
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
501 |
|
e1d3f027dd64
VMM/Config: Added function check_mailbox_format().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
298
diff
changeset
|
502 |
|
384
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
503 |
def check_size_value(value): |
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
504 |
"""Check if the size value *value* has the proper format, e.g.: 1024k. |
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
505 |
Returns the validated value string if it has the expected format. |
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
506 |
Otherwise a `ConfigValueError` will be raised.""" |
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
507 |
try: |
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
508 |
tmp = size_in_bytes(value) |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
509 |
except (TypeError, ValueError) as err: |
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
510 |
raise ConfigValueError(_("Not a valid size value: '%s'") % |
384
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
511 |
get_unicode(value)) |
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
512 |
return value |
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
513 |
|
d3a97f7fb98a
VMM/config: Added quota_bytes and quota_messages settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
366
diff
changeset
|
514 |
|
264
04fea4d8b900
Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents:
262
diff
changeset
|
515 |
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
|
516 |
"""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
|
517 |
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
|
518 |
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
|
519 |
""" |
348
ca7575401549
VMM/config: Use common.VERSION_RE, instead of defining the regexp two times.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
342
diff
changeset
|
520 |
if not VERSION_RE.match(version_string): |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
521 |
raise ConfigValueError(_("Not a valid Dovecot version: '%s'") % |
264
04fea4d8b900
Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents:
262
diff
changeset
|
522 |
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
|
523 |
return version_string |
04fea4d8b900
Use the complete Dovecot version, not only the concatenated major
Pascal Volk <neverseen@users.sourceforge.net>
parents:
262
diff
changeset
|
524 |
|
287
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
525 |
|
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
526 |
def verify_scheme(scheme): |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
527 |
"""Checks if the password scheme *scheme* can be accepted and returns |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
528 |
the verified scheme. |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
529 |
""" |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
530 |
try: |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
531 |
scheme, encoding = _verify_scheme(scheme) |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
532 |
except VMMError as err: # 'cast' it |
287
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
533 |
raise ConfigValueError(err.msg) |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
534 |
if not encoding: |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
535 |
return scheme |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
536 |
return '%s.%s' % (scheme, encoding) |
1e77dd639fa3
VMM/password: moved the 'scheme check' code from pwhash() to the
Pascal Volk <neverseen@users.sourceforge.net>
parents:
286
diff
changeset
|
537 |
|
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
211
diff
changeset
|
538 |
del _ |