author | Pascal Volk <neverseen@users.sourceforge.net> |
Sat, 13 Feb 2010 03:20:42 +0000 | |
branch | v0.6.x |
changeset 204 | 83938336c518 |
parent 201 | dbb0f7ed7858 |
child 206 | da07dd944ad1 |
permissions | -rw-r--r-- |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
162
0ac9ef587769
Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
138
diff
changeset
|
2 |
# Copyright (c) 2007 - 2010, Pascal Volk |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
3 |
# See COPYING for distribution information. |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
4 |
|
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
5 |
""" |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
6 |
VirtualMailManager.Config |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
7 |
|
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
8 |
VMM's configuration module for simplified configuration access. |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
9 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
10 |
This module defines a few classes: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
11 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
12 |
``LazyConfig`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
13 |
This class provides the following additonal methods |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
14 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
15 |
* `LazyConfig.pget()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
16 |
polymorphic getter which returns the value with the appropriate |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
17 |
type. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
18 |
* `LazyConfig.dget()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
19 |
like *pget()*, but checks additonal for default values in |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
20 |
`LazyConfig._cfg`. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
21 |
* `LazyConfig.set()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
22 |
like `RawConfigParser.set()`, but converts the new value to the |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
23 |
appropriate type/class and optional validates the new value. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
24 |
* `LazyConfig.bool_new()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
25 |
converts data from raw_input into boolean values. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
26 |
* `LazyConfig.get_boolean()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
27 |
like `RawConfigParser.getboolean()`, but doesn't fail on real |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
28 |
`bool` values. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
29 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
30 |
``Config`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
31 |
The Config class used by vmm. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
32 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
33 |
``LazyConfigOption`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
34 |
The class for the configuration objects in the ``Config`` class' |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
35 |
``_cfg`` dictionary. |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
36 |
""" |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
37 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
38 |
|
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
39 |
from ConfigParser import \ |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
40 |
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
|
41 |
ParsingError, RawConfigParser |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
42 |
from cStringIO import StringIO# TODO: move interactive stff to cli |
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
43 |
|
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
44 |
from VirtualMailManager import ENCODING, exec_ok, get_unicode, is_dir |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
45 |
from VirtualMailManager.constants.ERROR import CONF_ERROR |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
46 |
from VirtualMailManager.Exceptions import VMMConfigException |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
47 |
|
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 |
class BadOptionError(Error): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
50 |
"""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
|
51 |
pass |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
52 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
53 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
54 |
class ConfigValueError(Error): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
55 |
"""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
|
56 |
pass |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
57 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
58 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
59 |
class NoDefaultError(Error): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
60 |
"""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
|
61 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
62 |
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
|
63 |
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
|
64 |
(option, section)) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
65 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
66 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
67 |
class LazyConfig(RawConfigParser): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
68 |
"""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
|
69 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
70 |
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
|
71 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
72 |
`LazyConfig.pget()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
73 |
The polymorphic getter, which returns a option's value with the |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
74 |
appropriate type. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
75 |
`LazyConfig.dget()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
76 |
Like `LazyConfig.pget()`, but returns the option's default, from |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
77 |
`LazyConfig._cfg['sectionname']['optionname'].default`, if the |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
78 |
option is not configured in a ini-like configuration file. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
79 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
80 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
81 |
`LazyConfig.set()` differs from ``RawConfigParser``'s ``set()`` method. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
82 |
``LazyConfig.set()`` takes the ``section`` and ``option`` arguments |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
83 |
combined to a single string in the form |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
84 |
"``section``\ **.**\ ``option``". |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
85 |
""" |
204
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
201
diff
changeset
|
86 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
87 |
def __init__(self): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
88 |
RawConfigParser.__init__(self) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
89 |
self._modified = False |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
90 |
self._cfg = { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
91 |
'sectionname': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
92 |
'optionname': LazyConfigOption(int, 1, self.getint) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
93 |
} |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
94 |
} |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
95 |
"""sample _cfg dictionary. Create your own in your derived class.""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
96 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
97 |
def bool_new(self, value): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
98 |
"""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
|
99 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
100 |
| '1', 'on', 'yes' and 'true' will become ``True`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
101 |
| '0', 'off', 'no' and 'false' will become ``False`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
102 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
103 |
Throws a `ConfigValueError` for all other values, except ``bool``\ s. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
104 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
105 |
if isinstance(value, bool): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
106 |
return value |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
107 |
if value.lower() in self._boolean_states: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
108 |
return self._boolean_states[value.lower()] |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
109 |
else: |
204
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
201
diff
changeset
|
110 |
raise ConfigValueError(_(u"Not a boolean: '%s'") % |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
111 |
get_unicode(value)) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
112 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
113 |
def get_boolean(self, section, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
114 |
# if the setting was not written to the configuration file, it may |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
115 |
# be still a boolean value - lets see |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
116 |
if self._modified: |
204
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
201
diff
changeset
|
117 |
tmp = self.get(section, option) |
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
201
diff
changeset
|
118 |
assert isinstance(tmp, bool), 'Oops, not a boolean: %r' % tmp |
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
201
diff
changeset
|
119 |
return tmp |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
120 |
return self.getboolean(section, option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
121 |
|
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
122 |
def _get_section_option(self, section_option): |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
123 |
"""splits ``section_option`` (section\ **.**\ option) in two parts |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
124 |
and returns them as list ``[section, option]``, if: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
125 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
126 |
* it likes the format of ``section_option`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
127 |
* the ``section`` is known |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
128 |
* the ``option`` is known |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
129 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
130 |
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
|
131 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
132 |
* `BadOptionError` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
133 |
* `NoSectionError` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
134 |
* `NoOptionError` |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
135 |
""" |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
136 |
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
|
137 |
# TODO: cache it |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
138 |
if len(sect_opt) != 2:# do we need a regexp to check the format? |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
139 |
raise BadOptionError( |
204
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
201
diff
changeset
|
140 |
_(u"Bad format: '%s' - expected: section.option") % |
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
201
diff
changeset
|
141 |
get_unicode(section_option)) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
142 |
if not sect_opt[0] in self._cfg: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
143 |
raise NoSectionError(sect_opt[0]) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
144 |
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
|
145 |
raise NoOptionError(sect_opt[1], sect_opt[0]) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
146 |
return sect_opt |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
147 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
148 |
def items(self, section): |
178
9480f2b15129
po: Quick refresh - for documentation purpose.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
175
diff
changeset
|
149 |
"""returns an iterable that returns key, value ``tuples`` from the |
9480f2b15129
po: Quick refresh - for documentation purpose.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
175
diff
changeset
|
150 |
given ``section``.""" |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
151 |
if section in self._sections:# check if the section was parsed |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
152 |
d2 = self._sections[section] |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
153 |
elif not section in self._cfg: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
154 |
raise NoSectionError(section) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
155 |
else: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
156 |
return ((k, self._cfg[section][k].default) \ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
157 |
for k in self._cfg[section].iterkeys()) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
158 |
# still here? Get defaults and merge defaults with configured setting |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
159 |
d = dict((k, self._cfg[section][k].default) \ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
160 |
for k in self._cfg[section].iterkeys()) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
161 |
d.update(d2) |
204
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
201
diff
changeset
|
162 |
if '__name__' in d: |
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
201
diff
changeset
|
163 |
del d['__name__'] |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
164 |
return d.iteritems() |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
165 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
166 |
def dget(self, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
167 |
"""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
|
168 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
169 |
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
|
170 |
configured default value, from ``LazyConfig._cfg`` will be |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
171 |
returned. |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
172 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
173 |
Arguments: |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
174 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
175 |
`option` : string |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
176 |
the configuration option in the form |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
177 |
"``section``\ **.**\ ``option``" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
178 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
179 |
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
|
180 |
`LazyConfigOption.__init__()` for the `option`. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
181 |
""" |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
182 |
section, option = self._get_section_option(option) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
183 |
try: |
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 |
except (NoSectionError, NoOptionError): |
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
186 |
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
|
187 |
return self._cfg[section][option].default |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
188 |
else: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
189 |
raise NoDefaultError(section, option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
190 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
191 |
def pget(self, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
192 |
"""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
|
193 |
section, option = self._get_section_option(option) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
194 |
return self._cfg[section][option].getter(section, option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
195 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
196 |
def set(self, option, value): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
197 |
"""Set the value of an option. |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
198 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
199 |
Throws a ``ValueError`` if `value` couldn't be converted to |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
200 |
``LazyConfigOption.cls``""" |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
201 |
section, option = self._get_section_option(option) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
202 |
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
|
203 |
if self._cfg[section][option].validate: |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
204 |
val = self._cfg[section][option].validate(val) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
205 |
if not RawConfigParser.has_section(self, section): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
206 |
self.add_section(section) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
207 |
RawConfigParser.set(self, section, option, val) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
208 |
self._modified = True |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
209 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
210 |
def has_section(self, section): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
211 |
"""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
|
212 |
return section.lower() in self._cfg |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
213 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
214 |
def has_option(self, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
215 |
"""Checks if the option (section\ **.**\ option) is a known |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
216 |
configuration option.""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
217 |
try: |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
218 |
self._get_section_option(option) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
219 |
return True |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
220 |
except(BadOptionError, NoSectionError, NoOptionError): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
221 |
return False |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
222 |
|
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 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
227 |
``LazyConfigOption`` instances are required by `LazyConfig` instances, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
228 |
and instances of classes derived from ``LazyConfig``, like the |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
229 |
`Config` class. |
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): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
234 |
"""Creates a new ``LazyConfigOption`` instance. |
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 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
238 |
``cls`` : type |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
239 |
The class/type of the option's value |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
240 |
``default`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
241 |
Default value of the option. Use ``None`` if the option should |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
242 |
not have a default value. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
243 |
``getter`` : callable |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
244 |
A method's name of `RawConfigParser` and derived classes, to |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
245 |
get a option's value, e.g. `self.getint`. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
246 |
``validate`` : NoneType or a callable |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
247 |
None or any method, that takes one argument, in order to check |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
248 |
the value, when `LazyConfig.set()` is called. |
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 |
201
dbb0f7ed7858
VMM/Config: LazyConfigOption.__init__() cast 'default' to 'cls'.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
200
diff
changeset
|
251 |
if not default is None:# enforce the type of the default value |
dbb0f7ed7858
VMM/Config: LazyConfigOption.__init__() cast 'default' to 'cls'.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
200
diff
changeset
|
252 |
self.__default = self.__cls(default) |
dbb0f7ed7858
VMM/Config: LazyConfigOption.__init__() cast 'default' to 'cls'.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
200
diff
changeset
|
253 |
else: |
dbb0f7ed7858
VMM/Config: LazyConfigOption.__init__() cast 'default' to 'cls'.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
200
diff
changeset
|
254 |
self.__default = default |
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
255 |
if not callable(getter): |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
256 |
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
|
257 |
getter.__class__.__name__) |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
258 |
self.__getter = getter |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
259 |
if validate and not callable(validate): |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
260 |
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
|
261 |
validate.__class__.__name__) |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
262 |
self.__validate = validate |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
263 |
|
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
264 |
@property |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
265 |
def cls(self): |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
266 |
"""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
|
267 |
return self.__cls |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
268 |
|
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
269 |
@property |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
270 |
def default(self): |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
271 |
"""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
|
272 |
return self.__default |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
273 |
|
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
274 |
@property |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
275 |
def getter(self): |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
276 |
"""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
|
277 |
return self.__getter |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
278 |
|
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
279 |
@property |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
280 |
def validate(self): |
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
281 |
"""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
|
282 |
return self.__validate |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
283 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
284 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
285 |
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
|
286 |
"""This class is for reading vmm's configuration file.""" |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
287 |
|
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
288 |
def __init__(self, filename): |
49
9bd033177377
* 'VirtualMailManager/Config.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
48
diff
changeset
|
289 |
"""Creates a new Config instance |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
290 |
|
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
291 |
Arguments: |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
292 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
293 |
``filename`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
294 |
path to the configuration file |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
295 |
""" |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
296 |
LazyConfig.__init__(self) |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
297 |
self._cfgFileName = filename |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
298 |
self._cfgFile = None |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
299 |
self.__missing = {} |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
300 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
301 |
LCO = LazyConfigOption |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
302 |
bool_t = self.bool_new |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
303 |
self._cfg = { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
304 |
'account': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
305 |
'delete_directory': LCO(bool_t, False, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
306 |
'directory_mode': LCO(int, 448, self.getint), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
307 |
'disk_usage': LCO(bool_t, False, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
308 |
'password_length': LCO(int, 8, self.getint), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
309 |
'random_password': LCO(bool_t, False, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
310 |
'imap' : LCO(bool_t, True, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
311 |
'pop3' : LCO(bool_t, True, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
312 |
'sieve': LCO(bool_t, True, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
313 |
'smtp' : LCO(bool_t, True, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
314 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
315 |
'bin': { |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
316 |
'dovecotpw': LCO(str, '/usr/sbin/dovecotpw', self.get, exec_ok), |
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
317 |
'du': LCO(str, '/usr/bin/du', self.get, exec_ok), |
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
318 |
'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
|
319 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
320 |
'database': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
321 |
'host': LCO(str, 'localhost', self.get), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
322 |
'name': LCO(str, 'mailsys', self.get), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
323 |
'pass': LCO(str, None, self.get), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
324 |
'user': LCO(str, None, self.get), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
325 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
326 |
'domain': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
327 |
'auto_postmaster': LCO(bool_t, True, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
328 |
'delete_directory': LCO(bool_t, False, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
329 |
'directory_mode': LCO(int, 504, self.getint), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
330 |
'force_deletion': LCO(bool_t, False, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
331 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
332 |
'maildir': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
333 |
'folders': LCO(str, 'Drafts:Sent:Templates:Trash', self.get), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
334 |
'name': LCO(str, 'Maildir', self.get), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
335 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
336 |
'misc': { |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
337 |
'base_directory': LCO(str, '/srv/mail', self.get, is_dir), |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
338 |
'dovecot_version': LCO(int, 12, self.getint), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
339 |
'gid_mail': LCO(int, 8, self.getint), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
340 |
'password_scheme': LCO(str, 'CRAM-MD5', self.get, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
341 |
self.known_scheme), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
342 |
'transport': LCO(str, 'dovecot:', self.get), |
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 |
} |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
345 |
|
188
cf1b5f22dbd2
VMM/Handler: __init__ accepts now a config_type ('default'||'cli').
Pascal Volk <neverseen@users.sourceforge.net>
parents:
187
diff
changeset
|
346 |
def configure(self, sections): |
cf1b5f22dbd2
VMM/Handler: __init__ accepts now a config_type ('default'||'cli').
Pascal Volk <neverseen@users.sourceforge.net>
parents:
187
diff
changeset
|
347 |
raise NotImplementedError |
cf1b5f22dbd2
VMM/Handler: __init__ accepts now a config_type ('default'||'cli').
Pascal Volk <neverseen@users.sourceforge.net>
parents:
187
diff
changeset
|
348 |
|
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
349 |
def load(self): |
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
350 |
"""Loads the configuration, read only. |
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
351 |
|
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
352 |
Raises a VMMConfigException if the configuration syntax is invalid. |
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
353 |
""" |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
354 |
try: |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
355 |
self._cfgFile = open(self._cfgFileName, 'r') |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
356 |
self.readfp(self._cfgFile) |
48
0d5f58f8b8f5
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
47
diff
changeset
|
357 |
except (MissingSectionHeaderError, ParsingError), e: |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
358 |
raise VMMConfigException(str(e), CONF_ERROR) |
175
b241272eb1bd
VMM/VMM: allow configure subcommand also with missing settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
174
diff
changeset
|
359 |
finally: |
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
360 |
if self._cfgFile and not self._cfgFile.closed: |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
361 |
self._cfgFile.close() |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
362 |
|
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
363 |
def check(self): |
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
364 |
"""Performs a configuration check. |
128
cf8116625866
Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
120
diff
changeset
|
365 |
|
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
366 |
Raises a VMMConfigException if the check fails. |
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
367 |
""" |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
368 |
# TODO: There are only two settings w/o defaults. |
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
369 |
# So there is no need for cStringIO |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
370 |
if not self.__chkCfg(): |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
371 |
errmsg = StringIO() |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
372 |
errmsg.write(_(u'Missing options, which have no default value.\n')) |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
373 |
errmsg.write(_(u'Using configuration file: %s\n') % |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
374 |
self._cfgFileName) |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
375 |
for section, options in self.__missing.iteritems(): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
376 |
errmsg.write(_(u'* Section: %s\n') % section) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
377 |
for option in options: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
378 |
errmsg.write((u' %s\n') % option) |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
379 |
raise VMMConfigException(errmsg.getvalue(), CONF_ERROR) |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
380 |
|
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
381 |
def sections(self): |
180
f8279c90e99c
VMM/Config: dropped option config.done
Pascal Volk <neverseen@users.sourceforge.net>
parents:
178
diff
changeset
|
382 |
"""Returns an iterator object for all configuration sections.""" |
f8279c90e99c
VMM/Config: dropped option config.done
Pascal Volk <neverseen@users.sourceforge.net>
parents:
178
diff
changeset
|
383 |
return self._cfg.iterkeys() |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
384 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
385 |
def known_scheme(self, scheme): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
386 |
"""Converts ``scheme`` to upper case and checks if is known by |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
387 |
Dovecot (listed in VirtualMailManager.SCHEMES). |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
388 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
389 |
Throws a `ConfigValueError` if the scheme is not listed in |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
390 |
VirtualMailManager.SCHEMES. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
391 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
392 |
scheme = scheme.upper() |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
393 |
# TODO: VMM.SCHEMES |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
394 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
395 |
def unicode(self, section, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
396 |
"""Returns the value of the ``option`` from ``section``, converted |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
397 |
to Unicode. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
398 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
399 |
return get_unicode(self.get(section, option)) |
69
0c124160a991
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
68
diff
changeset
|
400 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
401 |
def __chkCfg(self): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
402 |
"""Checks all section's options for settings w/o default values. |
185
6e1ef32fbd82
VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
180
diff
changeset
|
403 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
404 |
Returns ``True`` if everything is fine, else ``False``.""" |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
405 |
errors = False |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
406 |
for section in self._cfg.iterkeys(): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
407 |
missing = [] |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
408 |
for option, value in self._cfg[section].iteritems(): |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
409 |
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
|
410 |
not RawConfigParser.has_option(self, section, option)): |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
411 |
missing.append(option) |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
185
diff
changeset
|
412 |
errors = True |
200
983cf98d5881
VMM/Config: attributes of class LazyConfigOption are read-only now.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
188
diff
changeset
|
413 |
if missing: |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
414 |
self.__missing[section] = missing |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
415 |
return not errors |