VirtualMailManager/cli/config.py
branchv0.6.x
changeset 320 011066435e6f
parent 316 31d8931dc535
child 322 94bd10e237e5
equal deleted inserted replaced
319:f4956b4ceba1 320:011066435e6f
       
     1 # -*- coding: UTF-8 -*-
       
     2 # Copyright (c) 2010, Pascal Volk
       
     3 # See COPYING for distribution information.
       
     4 """
       
     5     VirtualMailManager.cli.config
       
     6     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
     7 
       
     8     Adds some interactive stuff to the Config class.
       
     9 """
       
    10 
       
    11 from ConfigParser import RawConfigParser
       
    12 from shutil import copy2
       
    13 
       
    14 from VirtualMailManager import ENCODING
       
    15 from VirtualMailManager.config import Config, ConfigValueError, LazyConfig
       
    16 from VirtualMailManager.errors import ConfigError
       
    17 from VirtualMailManager.cli import w_err, w_std
       
    18 from VirtualMailManager.constants import VMM_TOO_MANY_FAILURES
       
    19 
       
    20 _ = lambda msg: msg
       
    21 
       
    22 
       
    23 class CliConfig(Config):
       
    24     """Adds the interactive ``configure`` method to the `Config` class
       
    25     and overwrites `LazyConfig.set(), in order to update a single option
       
    26     in the configuration file with a single command line command.
       
    27     """
       
    28 
       
    29     def configure(self, sections):
       
    30         """Interactive method for configuring all options of the given
       
    31         iterable ``sections`` object."""
       
    32         input_fmt = _(u'Enter new value for option %(option)s '
       
    33                       u'[%(current_value)s]: ')
       
    34         failures = 0
       
    35 
       
    36         w_std(_(u'Using configuration file: %s\n') % self._cfg_filename)
       
    37         for section in sections:
       
    38             w_std(_(u'* Configuration section: %r') % section)
       
    39             for opt, val in self.items(section):
       
    40                 failures = 0
       
    41                 while True:
       
    42                     newval = raw_input(input_fmt.encode(ENCODING, 'replace') %
       
    43                                        {'option': opt, 'current_value': val})
       
    44                     if newval and newval != val:
       
    45                         try:
       
    46                             LazyConfig.set(self, '%s.%s' % (section, opt),
       
    47                                            newval)
       
    48                             break
       
    49                         except (ValueError, ConfigValueError), err:
       
    50                             w_err(0, _(u'Warning: %s') % err)
       
    51                             failures += 1
       
    52                             if failures > 2:
       
    53                                 raise ConfigError(_(u'Too many failures - try '
       
    54                                                     u'again later.'),
       
    55                                                   VMM_TOO_MANY_FAILURES)
       
    56                     else:
       
    57                         break
       
    58             print
       
    59         if self._modified:
       
    60             self.__save_changes()
       
    61 
       
    62     def set(self, option, value):
       
    63         """Set the value of an option.
       
    64 
       
    65         If the new `value` has been set, the configuration file will be
       
    66         immediately updated.
       
    67 
       
    68         Throws a ``ValueError`` if `value` couldn't be converted to
       
    69         ``LazyConfigOption.cls``"""
       
    70         section, option_ = self._get_section_option(option)
       
    71         val = self._cfg[section][option_].cls(value)
       
    72         if self._cfg[section][option_].validate:
       
    73             val = self._cfg[section][option_].validate(val)
       
    74         # Do not write default values also skip identical values
       
    75         if not self._cfg[section][option_].default is None:
       
    76             old_val = self.dget(option)
       
    77         else:
       
    78             old_val = self.pget(option)
       
    79         if val == old_val:
       
    80             return
       
    81         if not RawConfigParser.has_section(self, section):
       
    82             self.add_section(section)
       
    83         RawConfigParser.set(self, section, option_, val)
       
    84         self.__save_changes()
       
    85 
       
    86     def __save_changes(self):
       
    87         """Writes changes to the configuration file."""
       
    88         copy2(self._cfg_filename, self._cfg_filename + '.bak')
       
    89         self._cfg_file = open(self._cfg_filename, 'w')
       
    90         self.write(self._cfg_file)
       
    91         self._cfg_file.close()
       
    92 
       
    93 del _