6 VirtualMailManager.cli.CliConfig |
6 VirtualMailManager.cli.CliConfig |
7 |
7 |
8 Adds some interactive stuff to the Config class. |
8 Adds some interactive stuff to the Config class. |
9 """ |
9 """ |
10 |
10 |
|
11 from shutil import copy2 |
|
12 |
11 from VirtualMailManager import ENCODING |
13 from VirtualMailManager import ENCODING |
12 from VirtualMailManager.Config import Config |
14 from VirtualMailManager.Config import Config, ConfigValueError, LazyConfig |
|
15 from VirtualMailManager.Exceptions import VMMConfigException |
13 from VirtualMailManager.cli import w_std |
16 from VirtualMailManager.cli import w_std |
14 from VirtualMailManager.constants.ERROR import VMM_TOO_MANY_FAILURES |
17 from VirtualMailManager.constants.ERROR import VMM_TOO_MANY_FAILURES |
|
18 |
15 |
19 |
16 class CliConfig(Config): |
20 class CliConfig(Config): |
17 """Adds the interactive ``configure`` method to the `Config` class |
21 """Adds the interactive ``configure`` method to the `Config` class |
18 and overwrites `LazyConfig.set(), in order to update a single option |
22 and overwrites `LazyConfig.set(), in order to update a single option |
19 in the configuration file with a single command line command. |
23 in the configuration file with a single command line command. |
20 """ |
24 """ |
|
25 |
21 def configure(self, sections): |
26 def configure(self, sections): |
22 """Interactive method for configuring all options of the given |
27 """Interactive method for configuring all options of the given |
23 iterable ``sections`` object.""" |
28 iterable ``sections`` object.""" |
24 input_fmt = _(u'Enter new value for option %(option)s \ |
29 input_fmt = _(u'Enter new value for option %(option)s \ |
25 [%(current_value)s]: ') |
30 [%(current_value)s]: ') |
26 failures = 0 |
31 failures = 0 |
27 |
32 |
28 w_std(_(u'Using configuration file: %s\n') % self._cfgFileName) |
33 w_std(_(u'Using configuration file: %s\n') % self._cfgFileName) |
29 for s in sections: |
34 for s in sections: |
30 w_std(_(u'* Configuration section: ā%sā') % s ) |
35 w_std(_(u'* Configuration section: %r') % s) |
31 for opt, val in self.items(s): |
36 for opt, val in self.items(s): |
32 failures = 0 |
37 failures = 0 |
33 while True: |
38 while True: |
34 newval = raw_input(input_fmt.encode(ENCODING,'replace') %{ |
39 newval = raw_input(input_fmt.encode(ENCODING, 'replace') % |
35 'option': opt, 'current_value': val}) |
40 {'option': opt, 'current_value': val}) |
36 if newval and newval != val: |
41 if newval and newval != val: |
37 try: |
42 try: |
38 LazyConfig.set('%s.%s' % (s, opt), newval) |
43 LazyConfig.set(self, '%s.%s' % (s, opt), newval) |
39 break |
44 break |
40 except (ValueError, ConfigValueError), e: |
45 except (ValueError, ConfigValueError), e: |
41 w_std(_(u'Warning: %s') % e) |
46 w_std(_(u'Warning: %s') % e) |
42 failures += 1 |
47 failures += 1 |
43 if failures > 2: |
48 if failures > 2: |
44 raise VMMConfigException( |
49 raise VMMConfigException( |
45 _(u'Too many failures - try again later.'), |
50 _(u'Too many failures - try again later.'), |
46 VMM_TOO_MANY_FAILURES) |
51 VMM_TOO_MANY_FAILURES) |
47 else: |
52 else: |
48 break |
53 break |
49 print |
54 print |
50 if self._modified: |
55 if self._modified: |
51 self.__saveChanges() |
56 self.__saveChanges() |
58 |
63 |
59 Throws a ``ValueError`` if `value` couldn't be converted to |
64 Throws a ``ValueError`` if `value` couldn't be converted to |
60 ``LazyConfigOption.cls``""" |
65 ``LazyConfigOption.cls``""" |
61 section, option_ = self._get_section_option(option) |
66 section, option_ = self._get_section_option(option) |
62 val = self._cfg[section][option_].cls(value) |
67 val = self._cfg[section][option_].cls(value) |
63 if not self._cfg[section][option_].validate is None: |
68 if self._cfg[section][option_].validate: |
64 val = self._cfg[section][option_].validate(val) |
69 val = self._cfg[section][option_].validate(val) |
65 # Do not write default values also skip identical values |
70 # Do not write default values also skip identical values |
66 if not self._cfg[section][option_].default is None: |
71 if not self._cfg[section][option_].default is None: |
67 old_val = self.dget(option) |
72 old_val = self.dget(option) |
68 else: |
73 else: |
74 RawConfigParser.set(self, section, option_, val) |
79 RawConfigParser.set(self, section, option_, val) |
75 self.__saveChanges() |
80 self.__saveChanges() |
76 |
81 |
77 def __saveChanges(self): |
82 def __saveChanges(self): |
78 """Writes changes to the configuration file.""" |
83 """Writes changes to the configuration file.""" |
79 copy2(self._cfgFileName, self._cfgFileName+'.bak') |
84 copy2(self._cfgFileName, self._cfgFileName + '.bak') |
80 self._cfgFile = open(self._cfgFileName, 'w') |
85 self._cfgFile = open(self._cfgFileName, 'w') |
81 self.write(self._cfgFile) |
86 self.write(self._cfgFile) |
82 self._cfgFile.close() |
87 self._cfgFile.close() |