author | Pascal Volk <neverseen@users.sourceforge.net> |
Sat, 30 Jan 2010 05:15:21 +0000 | |
branch | v0.6.x |
changeset 179 | 3d09c657e9e5 |
parent 178 | 9480f2b15129 |
child 180 | f8279c90e99c |
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 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
5 |
"""vmm's configuration module for simplified configuration access. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
6 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
7 |
This module defines a few classes: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
8 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
9 |
``LazyConfig`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
10 |
This class provides the following additonal methods |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
11 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
12 |
* `LazyConfig.pget()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
13 |
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
|
14 |
type. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
15 |
* `LazyConfig.dget()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
16 |
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
|
17 |
`LazyConfig._cfg`. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
18 |
* `LazyConfig.set()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
19 |
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
|
20 |
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
|
21 |
* `LazyConfig.bool_new()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
22 |
converts data from raw_input into boolean values. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
23 |
* `LazyConfig.get_boolean()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
24 |
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
|
25 |
`bool` values. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
26 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
27 |
``Config`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
28 |
The Config class used by vmm. |
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 |
``LazyConfigOption`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
31 |
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
|
32 |
``_cfg`` dictionary. |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
33 |
""" |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
34 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
35 |
|
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
36 |
from shutil import copy2 |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
37 |
from ConfigParser import (Error, MissingSectionHeaderError, NoOptionError, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
38 |
NoSectionError, ParsingError, RawConfigParser) |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
39 |
from cStringIO import StringIO |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
40 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
41 |
from __main__ import os, ENCODING, ERR, get_unicode, w_std |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
42 |
from Exceptions import VMMConfigException |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
43 |
|
174
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 |
class BadOptionError(Error): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
46 |
"""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
|
47 |
pass |
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 ConfigValueError(Error): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
51 |
"""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
|
52 |
pass |
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 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
55 |
class NoDefaultError(Error): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
56 |
"""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
|
57 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
58 |
def __init__(self, section, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
59 |
Error.__init__(self, 'Option %r in section %r has no default value' %( |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
60 |
option, section)) |
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 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
63 |
class LazyConfig(RawConfigParser): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
64 |
"""The **lazy** derivate of the `RawConfigParser`. |
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 |
There are two additional getters: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
67 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
68 |
`LazyConfig.pget()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
69 |
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
|
70 |
appropriate type. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
71 |
`LazyConfig.dget()` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
72 |
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
|
73 |
`LazyConfig._cfg['sectionname']['optionname'].default`, if the |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
74 |
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
|
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 |
`LazyConfig.set()` differs from ``RawConfigParser``'s ``set()`` method. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
78 |
``LazyConfig.set()`` takes the ``section`` and ``option`` arguments |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
79 |
combined to a single string in the form |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
80 |
"``section``\ **.**\ ``option``". |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
81 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
82 |
def __init__(self): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
83 |
RawConfigParser.__init__(self) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
84 |
self._modified = False |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
85 |
self._cfg = { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
86 |
'sectionname': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
87 |
'optionname': LazyConfigOption(int, 1, self.getint) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
88 |
} |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
89 |
} |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
90 |
"""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
|
91 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
92 |
def bool_new(self, value): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
93 |
"""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
|
94 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
95 |
| '1', 'on', 'yes' and 'true' will become ``True`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
96 |
| '0', 'off', 'no' and 'false' will become ``False`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
97 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
98 |
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
|
99 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
100 |
if isinstance(value, bool): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
101 |
return value |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
102 |
if value.lower() in self._boolean_states: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
103 |
return self._boolean_states[value.lower()] |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
104 |
else: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
105 |
raise ConfigValueError(_(u'Not a boolean: “%s”') % \ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
106 |
get_unicode(value)) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
107 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
108 |
def get_boolean(self, section, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
109 |
# 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
|
110 |
# be still a boolean value - lets see |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
111 |
if self._modified: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
112 |
tmp = self.get(section, option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
113 |
if isinstance(tmp, bool): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
114 |
return tmp |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
115 |
return self.getboolean(section, option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
116 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
117 |
def __get_section_option(self, section_option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
118 |
"""splits ``section_option`` (section\ **.**\ option) in two parts |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
119 |
and returns them as list ``[section, option]``, if: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
120 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
121 |
* it likes the format of ``section_option`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
122 |
* the ``section`` is known |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
123 |
* the ``option`` is known |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
124 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
125 |
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
|
126 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
127 |
* `BadOptionError` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
128 |
* `NoSectionError` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
129 |
* `NoOptionError` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
130 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
131 |
sect_opt = section_option.lower().split('.') |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
132 |
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
|
133 |
raise BadOptionError( |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
134 |
_(u'Bad format: “%s” - expected: section.option') % \ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
135 |
get_unicode(section_option)) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
136 |
if not sect_opt[0] in self._cfg: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
137 |
raise NoSectionError(sect_opt[0]) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
138 |
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
|
139 |
raise NoOptionError(sect_opt[1], sect_opt[0]) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
140 |
return sect_opt |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
141 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
142 |
def items(self, section): |
178
9480f2b15129
po: Quick refresh - for documentation purpose.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
175
diff
changeset
|
143 |
"""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
|
144 |
given ``section``.""" |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
145 |
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
|
146 |
d2 = self._sections[section] |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
147 |
elif not section in self._cfg: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
148 |
raise NoSectionError(section) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
149 |
else: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
150 |
return ((k, self._cfg[section][k].default) \ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
151 |
for k in self._cfg[section].iterkeys()) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
152 |
# 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
|
153 |
d = dict((k, self._cfg[section][k].default) \ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
154 |
for k in self._cfg[section].iterkeys()) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
155 |
d.update(d2) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
156 |
if '__name__' in d: del d['__name__'] |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
157 |
return d.iteritems() |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
158 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
159 |
def dget(self, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
160 |
"""Returns the value of the `option`. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
161 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
162 |
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
|
163 |
configured default value, from ``LazyConfig._cfg`` will be |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
164 |
returned. |
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 |
Arguments: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
167 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
168 |
`option` : string |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
169 |
the configuration option in the form |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
170 |
"``section``\ **.**\ ``option``" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
171 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
172 |
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
|
173 |
`LazyConfigOption.__init__()` for the `option`. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
174 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
175 |
section, option = self.__get_section_option(option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
176 |
try: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
177 |
return self._cfg[section][option].getter(section, option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
178 |
except (NoSectionError, NoOptionError): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
179 |
if not self._cfg[section][option].default is None: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
180 |
return self._cfg[section][option].default |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
181 |
else: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
182 |
raise NoDefaultError(section, option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
183 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
184 |
def pget(self, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
185 |
"""Returns the value of the `option`.""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
186 |
section, option = self.__get_section_option(option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
187 |
return self._cfg[section][option].getter(section, option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
188 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
189 |
def set(self, option, value): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
190 |
"""Set the value of an option. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
191 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
192 |
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
|
193 |
``LazyConfigOption.cls``""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
194 |
section, option = self.__get_section_option(option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
195 |
val = self._cfg[section][option].cls(value) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
196 |
if not self._cfg[section][option].validate is None: |
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): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
204 |
"""Checks if ``section`` is a known configuration section.""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
205 |
return section.lower() in self._cfg |
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): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
208 |
"""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
|
209 |
configuration option.""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
210 |
try: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
211 |
self.__get_section_option(option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
212 |
return True |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
213 |
except(BadOptionError, NoSectionError, NoOptionError): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
214 |
return False |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
215 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
216 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
217 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
218 |
class LazyConfigOption(object): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
219 |
"""A simple container class for configuration settings. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
220 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
221 |
``LazyConfigOption`` instances are required by `LazyConfig` instances, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
222 |
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
|
223 |
`Config` class. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
224 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
225 |
__slots__ = ('cls', 'default', 'getter', 'validate') |
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 |
def __init__(self, cls, default, getter, validate=None): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
228 |
"""Creates a new ``LazyConfigOption`` instance. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
229 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
230 |
Arguments: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
231 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
232 |
``cls`` : type |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
233 |
The class/type of the option's value |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
234 |
``default`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
235 |
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
|
236 |
not have a default value. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
237 |
``getter`` : callable |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
238 |
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
|
239 |
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
|
240 |
``validate`` : NoneType or a callable |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
241 |
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
|
242 |
the value, when `LazyConfig.set()` is called. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
243 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
244 |
self.cls = cls |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
245 |
"""The class of the option's value e.g. `str`, `unicode` or `bool`""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
246 |
self.default = default |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
247 |
"""The option's default value, may be ``None``""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
248 |
if callable(getter): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
249 |
self.getter = getter |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
250 |
"""The getter method to get the option's value""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
251 |
else: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
252 |
raise TypeError('getter has to be a callable, got a %r'\ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
253 |
% getter.__class__.__name__) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
254 |
if validate is None or callable(validate): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
255 |
self.validate = validate |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
256 |
"""A method to validate the value""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
257 |
else: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
258 |
raise TypeError('validate has to be callable or None, got a %r'\ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
259 |
% validate.__class__.__name__) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
260 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
261 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
262 |
class Config(LazyConfig): |
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
263 |
"""This class is for reading and modifying vmm's configuration file.""" |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
264 |
|
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
265 |
def __init__(self, filename): |
49
9bd033177377
* 'VirtualMailManager/Config.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
48
diff
changeset
|
266 |
"""Creates a new Config instance |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
267 |
|
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
268 |
Arguments: |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
269 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
270 |
``filename`` |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
271 |
path to the configuration file |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
272 |
""" |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
273 |
LazyConfig.__init__(self) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
274 |
self.__cfgFileName = filename |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
275 |
self.__cfgFile = None |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
276 |
self.__missing = {} |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
277 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
278 |
LCO = LazyConfigOption |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
279 |
bool_t = self.bool_new |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
280 |
self._cfg = { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
281 |
'account': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
282 |
'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
|
283 |
'directory_mode': LCO(int, 448, self.getint), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
284 |
'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
|
285 |
'password_length': LCO(int, 8, self.getint), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
286 |
'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
|
287 |
'imap' : LCO(bool_t, True, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
288 |
'pop3' : LCO(bool_t, True, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
289 |
'sieve': LCO(bool_t, True, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
290 |
'smtp' : LCO(bool_t, True, self.get_boolean), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
291 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
292 |
'bin': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
293 |
'dovecotpw': LCO(str, '/usr/sbin/dovecotpw', self.get, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
294 |
self.exec_ok), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
295 |
'du': LCO(str, '/usr/bin/du', self.get, self.exec_ok), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
296 |
'postconf': LCO(str, '/usr/sbin/postconf', self.get, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
297 |
self.exec_ok), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
298 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
299 |
'database': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
300 |
'host': LCO(str, 'localhost', self.get), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
301 |
'name': LCO(str, 'mailsys', self.get), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
302 |
'pass': LCO(str, None, self.get), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
303 |
'user': LCO(str, None, self.get), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
304 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
305 |
'domain': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
306 |
'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
|
307 |
'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
|
308 |
'directory_mode': LCO(int, 504, self.getint), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
309 |
'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
|
310 |
}, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
311 |
'maildir': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
312 |
'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
|
313 |
'name': LCO(str, 'Maildir', self.get), |
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 |
'misc': { |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
316 |
'base_directory': LCO(str, '/srv/mail', self.get, self.is_dir), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
317 |
'dovecot_version': LCO(int, 12, self.getint), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
318 |
'gid_mail': LCO(int, 8, self.getint), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
319 |
'password_scheme': LCO(str, 'CRAM-MD5', self.get, |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
320 |
self.known_scheme), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
321 |
'transport': LCO(str, 'dovecot:', self.get), |
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 |
'config': {'done': LCO(bool_t, False, self.get_boolean)} |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
324 |
} |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
325 |
|
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
326 |
def load(self): |
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
327 |
"""Loads the configuration, read only. |
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
328 |
|
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
329 |
Raises a VMMConfigException if the configuration syntax is invalid. |
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
330 |
""" |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
331 |
try: |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
332 |
self.__cfgFile = open(self.__cfgFileName, 'r') |
48
0d5f58f8b8f5
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
47
diff
changeset
|
333 |
self.readfp(self.__cfgFile) |
0d5f58f8b8f5
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
47
diff
changeset
|
334 |
except (MissingSectionHeaderError, ParsingError), e: |
0d5f58f8b8f5
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
47
diff
changeset
|
335 |
raise VMMConfigException(str(e), ERR.CONF_ERROR) |
175
b241272eb1bd
VMM/VMM: allow configure subcommand also with missing settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
174
diff
changeset
|
336 |
finally: |
b241272eb1bd
VMM/VMM: allow configure subcommand also with missing settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
174
diff
changeset
|
337 |
if not self.__cfgFile is None and not self.__cfgFile.closed: |
b241272eb1bd
VMM/VMM: allow configure subcommand also with missing settings.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
174
diff
changeset
|
338 |
self.__cfgFile.close() |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
339 |
|
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
340 |
def check(self): |
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
341 |
"""Performs a configuration check. |
128
cf8116625866
Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
120
diff
changeset
|
342 |
|
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
343 |
Raises a VMMConfigException if the check fails. |
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
344 |
""" |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
345 |
if not self.__chkCfg(): |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
346 |
errmsg = StringIO() |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
347 |
errmsg.write(_(u'Missing options, which have no default value.\n')) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
348 |
errmsg.write(_(u'Using configuration file: %s\n') %\ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
349 |
self.__cfgFileName) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
350 |
for section, options in self.__missing.iteritems(): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
351 |
errmsg.write(_(u'* Section: %s\n') % section) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
352 |
for option in options: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
353 |
errmsg.write((u' %s\n') % option) |
48
0d5f58f8b8f5
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
47
diff
changeset
|
354 |
raise VMMConfigException(errmsg.getvalue(), ERR.CONF_ERROR) |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
355 |
|
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
356 |
def getsections(self): |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
357 |
"""Returns a generator object for all configurable sections.""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
358 |
return (s for s in self._cfg.iterkeys() if s != 'config') |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
359 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
360 |
def is_dir(self, path): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
361 |
"""Checks if ``path`` is a directory. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
362 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
363 |
Throws a `ConfigValueError` if ``path`` is not a directory. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
364 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
365 |
path = self.__expand_path(path) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
366 |
if not os.path.isdir(path): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
367 |
raise ConfigValueError(_(u'“%s” is not a directory') % \ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
368 |
get_unicode(path)) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
369 |
return path |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
370 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
371 |
def exec_ok(self, binary): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
372 |
"""Checks if the ``binary`` exists and if it is executable. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
373 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
374 |
Throws a `ConfigValueError` if the ``binary`` isn't a file or is |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
375 |
not executable. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
376 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
377 |
binary = self.__expand_path(binary) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
378 |
if not os.path.isfile(binary): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
379 |
raise ConfigValueError(_(u'“%s” is not a file') % \ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
380 |
get_unicode(binary)) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
381 |
if not os.access(binary, os.X_OK): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
382 |
raise ConfigValueError(_(u'File is not executable: “%s”') % \ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
383 |
get_unicode(binary)) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
384 |
return binary |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
385 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
386 |
def known_scheme(self, scheme): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
387 |
"""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
|
388 |
Dovecot (listed in VirtualMailManager.SCHEMES). |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
389 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
390 |
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
|
391 |
VirtualMailManager.SCHEMES. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
392 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
393 |
scheme = scheme.upper() |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
394 |
# TODO: VMM.SCHEMES |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
395 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
396 |
def unicode(self, section, option): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
397 |
"""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
|
398 |
to Unicode. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
399 |
""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
400 |
return get_unicode(self.get(section, option)) |
69
0c124160a991
* 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
68
diff
changeset
|
401 |
|
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
402 |
def configure(self, sections): |
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
403 |
"""Interactive method for configuring all options in the given sections |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
404 |
|
120
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
405 |
Arguments: |
928659c8ee9f
Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
113
diff
changeset
|
406 |
sections -- list of strings with section names |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
407 |
""" |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
408 |
input_fmt = _(u'Enter new value for option %(option)s \ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
409 |
[%(current_value)s]: ') |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
410 |
failures = 0 |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
411 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
412 |
# if config.done == false (default at 1st run), |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
413 |
# then set changes true |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
414 |
if not self.dget('config.done'): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
415 |
self._modified = True |
112
d1f345f91e1c
Hopefully the final UnicodeDecodeError fix for the Config class
Pascal Volk <neverseen@users.sourceforge.net>
parents:
103
diff
changeset
|
416 |
w_std(_(u'Using configuration file: %s\n') % self.__cfgFileName) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
417 |
for s in sections: |
178
9480f2b15129
po: Quick refresh - for documentation purpose.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
175
diff
changeset
|
418 |
w_std(_(u'* Configuration section: “%s”') % s ) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
419 |
for opt, val in self.items(s): |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
420 |
failures = 0 |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
421 |
while True: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
422 |
newval = raw_input(input_fmt.encode(ENCODING,'replace') %{ |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
423 |
'option': opt, 'current_value': val}) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
424 |
if newval and newval != val: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
425 |
try: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
426 |
self.set('%s.%s' % (s, opt), newval) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
427 |
break |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
428 |
except (ValueError, ConfigValueError), e: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
429 |
w_std(_(u'Warning: %s') % e) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
430 |
failures += 1 |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
431 |
if failures > 2: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
432 |
raise VMMConfigException( |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
433 |
_(u'Too many failures - try again later.'), |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
434 |
ERR.VMM_TOO_MANY_FAILURES) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
435 |
else: |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
436 |
break |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
437 |
print |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
438 |
if self._modified: |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
439 |
self.__saveChanges() |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
440 |
|
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
441 |
def __saveChanges(self): |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
442 |
"""Writes changes to the configuration file.""" |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
443 |
self.set('config.done', True) |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
444 |
copy2(self.__cfgFileName, self.__cfgFileName+'.bak') |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
445 |
self.__cfgFile = open(self.__cfgFileName, 'w') |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
446 |
self.write(self.__cfgFile) |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
447 |
self.__cfgFile.close() |
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
448 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
449 |
def __chkCfg(self): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
450 |
"""Checks all section's options for settings w/o default values. |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
451 |
|
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
452 |
Returns ``True`` if everything is fine, else ``False``.""" |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
453 |
errors = False |
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
454 |
for section in self._cfg.iterkeys(): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
455 |
missing = [] |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
456 |
for option, value in self._cfg[section].iteritems(): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
457 |
if (value.default is None |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
458 |
and not RawConfigParser.has_option(self, section, option)): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
459 |
missing.append(option) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
460 |
errors = True |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
461 |
if len(missing): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
462 |
self.__missing[section] = missing |
2
9b39f828aa8a
* 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
0
diff
changeset
|
463 |
return not errors |
0
bb0aa2102206
Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
464 |
|
174
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
465 |
def __expand_path(self, path): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
466 |
"""Expands paths, starting with ``.`` or ``~``, to an absolute path.""" |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
467 |
if path.startswith('.'): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
468 |
return os.path.abspath(path) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
469 |
if path.startswith('~'): |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
470 |
return os.path.expanduser(path) |
974bafa59330
VMM/Config: reworked configuration handling.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
173
diff
changeset
|
471 |
return path |