author | Pascal Volk <user@localhost.localdomain.org> |
Sat, 15 Feb 2014 19:00:40 +0000 | |
branch | v0.7.x |
changeset 728 | 69eb7a65d8a4 |
parent 711 | 2a75058fc064 |
permissions | -rw-r--r-- |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
1 |
# -*- coding: UTF-8 -*- |
703
58815c004a61
Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents:
675
diff
changeset
|
2 |
# Copyright (c) 2010 - 2014, Pascal Volk |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
3 |
# See COPYING for distribution information. |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
4 |
""" |
320
011066435e6f
VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
316
diff
changeset
|
5 |
VirtualMailManager.cli.config |
011066435e6f
VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
316
diff
changeset
|
6 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
7 |
|
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
8 |
Adds some interactive stuff to the Config class. |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
9 |
""" |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
10 |
|
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
11 |
from configparser import RawConfigParser |
204
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
189
diff
changeset
|
12 |
from shutil import copy2 |
659
a6b6b0937cae
VMM/cli/config: … encode/decode …
Pascal Volk <user@localhost.localdomain.org>
parents:
658
diff
changeset
|
13 |
from string import Template |
204
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
189
diff
changeset
|
14 |
|
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
15 |
from VirtualMailManager import ENCODING |
320
011066435e6f
VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
316
diff
changeset
|
16 |
from VirtualMailManager.config import Config, ConfigValueError, LazyConfig |
349
d60ffbc0124b
VMM/cli/config: Except the VMMError from exec_ok in configure().
Pascal Volk <neverseen@users.sourceforge.net>
parents:
344
diff
changeset
|
17 |
from VirtualMailManager.errors import ConfigError, VMMError |
313
c17c46d9e440
VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
18 |
from VirtualMailManager.cli import w_err, w_std |
344
0d2430dc6ef8
VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents:
341
diff
changeset
|
19 |
from VirtualMailManager.constants import CONF_ERROR, VMM_TOO_MANY_FAILURES |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
20 |
|
313
c17c46d9e440
VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
21 |
_ = lambda msg: msg |
c17c46d9e440
VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
22 |
|
204
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
189
diff
changeset
|
23 |
|
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
24 |
class CliConfig(Config): |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
25 |
"""Adds the interactive ``configure`` method to the `Config` class |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
26 |
and overwrites `LazyConfig.set(), in order to update a single option |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
27 |
in the configuration file with a single command line command. |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
28 |
""" |
204
83938336c518
VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
189
diff
changeset
|
29 |
|
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
30 |
def configure(self, sections): |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
31 |
"""Interactive method for configuring all options of the given |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
32 |
iterable ``sections`` object.""" |
659
a6b6b0937cae
VMM/cli/config: … encode/decode …
Pascal Volk <user@localhost.localdomain.org>
parents:
658
diff
changeset
|
33 |
input_tpl = Template(_('Enter new value for option $option ' |
a6b6b0937cae
VMM/cli/config: … encode/decode …
Pascal Volk <user@localhost.localdomain.org>
parents:
658
diff
changeset
|
34 |
'[$current_value]: ')) |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
35 |
failures = 0 |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
36 |
|
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
37 |
w_std(_('Using configuration file: %s\n') % self._cfg_filename) |
313
c17c46d9e440
VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
38 |
for section in sections: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
39 |
w_std(_("* Configuration section: '%s'") % section) |
313
c17c46d9e440
VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
40 |
for opt, val in self.items(section): |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
41 |
failures = 0 |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
42 |
while True: |
659
a6b6b0937cae
VMM/cli/config: … encode/decode …
Pascal Volk <user@localhost.localdomain.org>
parents:
658
diff
changeset
|
43 |
if isinstance(val, str): |
a6b6b0937cae
VMM/cli/config: … encode/decode …
Pascal Volk <user@localhost.localdomain.org>
parents:
658
diff
changeset
|
44 |
val = val.encode(ENCODING, 'replace').decode(ENCODING) |
a6b6b0937cae
VMM/cli/config: … encode/decode …
Pascal Volk <user@localhost.localdomain.org>
parents:
658
diff
changeset
|
45 |
newval = input(input_tpl.substitute(option=opt, |
a6b6b0937cae
VMM/cli/config: … encode/decode …
Pascal Volk <user@localhost.localdomain.org>
parents:
658
diff
changeset
|
46 |
current_value=val)) |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
47 |
if newval and newval != val: |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
48 |
try: |
313
c17c46d9e440
VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
49 |
LazyConfig.set(self, '%s.%s' % (section, opt), |
c17c46d9e440
VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
50 |
newval) |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
51 |
break |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
52 |
except (ValueError, ConfigValueError, VMMError) as err: |
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
53 |
w_err(0, _('Warning: %s') % err) |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
54 |
failures += 1 |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
55 |
if failures > 2: |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
56 |
raise ConfigError(_('Too many failures - try ' |
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
57 |
'again later.'), |
216
0c8c053b451c
Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
215
diff
changeset
|
58 |
VMM_TOO_MANY_FAILURES) |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
59 |
else: |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
60 |
break |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
61 |
print() |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
62 |
if self._modified: |
322
94bd10e237e5
VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
63 |
self._save_changes() |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
64 |
|
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
65 |
def set(self, option, value): |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
66 |
"""Set the value of an option. |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
67 |
|
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
68 |
If the new `value` has been set, the configuration file will be |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
69 |
immediately updated. |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
70 |
|
344
0d2430dc6ef8
VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents:
341
diff
changeset
|
71 |
Throws a ``ConfigError`` if `value` couldn't be converted to |
0d2430dc6ef8
VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents:
341
diff
changeset
|
72 |
``LazyConfigOption.cls`` or ``LazyConfigOption.validate`` fails.""" |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
73 |
section, option_ = self._get_section_option(option) |
344
0d2430dc6ef8
VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents:
341
diff
changeset
|
74 |
try: |
0d2430dc6ef8
VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents:
341
diff
changeset
|
75 |
val = self._cfg[section][option_].cls(value) |
0d2430dc6ef8
VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents:
341
diff
changeset
|
76 |
if self._cfg[section][option_].validate: |
0d2430dc6ef8
VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents:
341
diff
changeset
|
77 |
val = self._cfg[section][option_].validate(val) |
643
df1e3b67882a
Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents:
642
diff
changeset
|
78 |
except (ValueError, ConfigValueError) as err: |
344
0d2430dc6ef8
VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents:
341
diff
changeset
|
79 |
raise ConfigError(str(err), CONF_ERROR) |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
80 |
# Do not write default values also skip identical values |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
81 |
if not self._cfg[section][option_].default is None: |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
82 |
old_val = self.dget(option) |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
83 |
else: |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
84 |
old_val = self.pget(option) |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
85 |
if val == old_val: |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
86 |
return |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
87 |
if not RawConfigParser.has_section(self, section): |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
88 |
self.add_section(section) |
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
89 |
RawConfigParser.set(self, section, option_, val) |
322
94bd10e237e5
VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
90 |
self._save_changes() |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
91 |
|
322
94bd10e237e5
VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
320
diff
changeset
|
92 |
def _save_changes(self): |
187
38b9a9859749
VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff
changeset
|
93 |
"""Writes changes to the configuration file.""" |
215
33f727efa7c4
PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
206
diff
changeset
|
94 |
copy2(self._cfg_filename, self._cfg_filename + '.bak') |
658
ad967c4b7fe5
VMM{/cli}/config: Explicitly pass utf-8 as the encoding to open().
Pascal Volk <user@localhost.localdomain.org>
parents:
643
diff
changeset
|
95 |
with open(self._cfg_filename, 'w', encoding='utf-8') as self._cfg_file: |
642
4cd9d0a9f42f
Use the with statement for file operations.
Pascal Volk <user@localhost.localdomain.org>
parents:
568
diff
changeset
|
96 |
self.write(self._cfg_file) |
313
c17c46d9e440
VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
97 |
|
c17c46d9e440
VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
290
diff
changeset
|
98 |
del _ |