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