VirtualMailManager/cli/config.py
branchv0.7.x
changeset 643 df1e3b67882a
parent 642 4cd9d0a9f42f
child 658 ad967c4b7fe5
equal deleted inserted replaced
642:4cd9d0a9f42f 643:df1e3b67882a
     6     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     6     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     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 ConfigParser import RawConfigParser
    11 from configparser import RawConfigParser
    12 from shutil import copy2
    12 from shutil import copy2
    13 
    13 
    14 from VirtualMailManager import ENCODING
    14 from VirtualMailManager import ENCODING
    15 from VirtualMailManager.config import Config, ConfigValueError, LazyConfig
    15 from VirtualMailManager.config import Config, ConfigValueError, LazyConfig
    16 from VirtualMailManager.errors import ConfigError, VMMError
    16 from VirtualMailManager.errors import ConfigError, VMMError
    27     """
    27     """
    28 
    28 
    29     def configure(self, sections):
    29     def configure(self, sections):
    30         """Interactive method for configuring all options of the given
    30         """Interactive method for configuring all options of the given
    31         iterable ``sections`` object."""
    31         iterable ``sections`` object."""
    32         input_fmt = _(u'Enter new value for option %(option)s '
    32         input_fmt = _('Enter new value for option %(option)s '
    33                       u'[%(current_value)s]: ')
    33                       '[%(current_value)s]: ')
    34         failures = 0
    34         failures = 0
    35 
    35 
    36         w_std(_(u'Using configuration file: %s\n') % self._cfg_filename)
    36         w_std(_('Using configuration file: %s\n') % self._cfg_filename)
    37         for section in sections:
    37         for section in sections:
    38             w_std(_(u"* Configuration section: '%s'") % section)
    38             w_std(_("* Configuration section: '%s'") % section)
    39             for opt, val in self.items(section):
    39             for opt, val in self.items(section):
    40                 failures = 0
    40                 failures = 0
    41                 while True:
    41                 while True:
    42                     newval = raw_input(input_fmt.encode(ENCODING, 'replace') %
    42                     newval = input(input_fmt.encode(ENCODING, 'replace') %
    43                                        {'option': opt, 'current_value': val})
    43                                        {'option': opt, 'current_value': val})
    44                     if newval and newval != val:
    44                     if newval and newval != val:
    45                         try:
    45                         try:
    46                             LazyConfig.set(self, '%s.%s' % (section, opt),
    46                             LazyConfig.set(self, '%s.%s' % (section, opt),
    47                                            newval)
    47                                            newval)
    48                             break
    48                             break
    49                         except (ValueError, ConfigValueError, VMMError), err:
    49                         except (ValueError, ConfigValueError, VMMError) as err:
    50                             w_err(0, _(u'Warning: %s') % err)
    50                             w_err(0, _('Warning: %s') % err)
    51                             failures += 1
    51                             failures += 1
    52                             if failures > 2:
    52                             if failures > 2:
    53                                 raise ConfigError(_(u'Too many failures - try '
    53                                 raise ConfigError(_('Too many failures - try '
    54                                                     u'again later.'),
    54                                                     'again later.'),
    55                                                   VMM_TOO_MANY_FAILURES)
    55                                                   VMM_TOO_MANY_FAILURES)
    56                     else:
    56                     else:
    57                         break
    57                         break
    58             print
    58             print()
    59         if self._modified:
    59         if self._modified:
    60             self._save_changes()
    60             self._save_changes()
    61 
    61 
    62     def set(self, option, value):
    62     def set(self, option, value):
    63         """Set the value of an option.
    63         """Set the value of an option.
    70         section, option_ = self._get_section_option(option)
    70         section, option_ = self._get_section_option(option)
    71         try:
    71         try:
    72             val = self._cfg[section][option_].cls(value)
    72             val = self._cfg[section][option_].cls(value)
    73             if self._cfg[section][option_].validate:
    73             if self._cfg[section][option_].validate:
    74                 val = self._cfg[section][option_].validate(val)
    74                 val = self._cfg[section][option_].validate(val)
    75         except (ValueError, ConfigValueError), err:
    75         except (ValueError, ConfigValueError) as err:
    76             raise ConfigError(str(err), CONF_ERROR)
    76             raise ConfigError(str(err), CONF_ERROR)
    77         # Do not write default values also skip identical values
    77         # Do not write default values also skip identical values
    78         if not self._cfg[section][option_].default is None:
    78         if not self._cfg[section][option_].default is None:
    79             old_val = self.dget(option)
    79             old_val = self.dget(option)
    80         else:
    80         else: