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