VirtualMailManager/cli/Config.py
branchv0.6.x
changeset 313 c17c46d9e440
parent 290 e2785e04f92e
child 316 31d8931dc535
equal deleted inserted replaced
312:6f39a1e56f4a 313:c17c46d9e440
     2 # Copyright (c) 2010, Pascal Volk
     2 # Copyright (c) 2010, Pascal Volk
     3 # See COPYING for distribution information.
     3 # See COPYING for distribution information.
     4 
     4 
     5 """
     5 """
     6     VirtualMailManager.cli.CliConfig
     6     VirtualMailManager.cli.CliConfig
       
     7     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     7 
     8 
     8     Adds some interactive stuff to the Config class.
     9     Adds some interactive stuff to the Config class.
     9 """
    10 """
    10 
    11 
    11 from ConfigParser import RawConfigParser
    12 from ConfigParser import RawConfigParser
    12 from shutil import copy2
    13 from shutil import copy2
    13 
    14 
    14 from VirtualMailManager import ENCODING
    15 from VirtualMailManager import ENCODING
    15 from VirtualMailManager.Config import Config, ConfigValueError, LazyConfig
    16 from VirtualMailManager.Config import Config, ConfigValueError, LazyConfig
    16 from VirtualMailManager.errors import ConfigError
    17 from VirtualMailManager.errors import ConfigError
    17 from VirtualMailManager.cli import w_std
    18 from VirtualMailManager.cli import w_err, w_std
    18 from VirtualMailManager.constants.ERROR import VMM_TOO_MANY_FAILURES
    19 from VirtualMailManager.constants.ERROR import VMM_TOO_MANY_FAILURES
       
    20 
       
    21 _ = lambda msg: msg
    19 
    22 
    20 
    23 
    21 class CliConfig(Config):
    24 class CliConfig(Config):
    22     """Adds the interactive ``configure`` method to the `Config` class
    25     """Adds the interactive ``configure`` method to the `Config` class
    23     and overwrites `LazyConfig.set(), in order to update a single option
    26     and overwrites `LazyConfig.set(), in order to update a single option
    30         input_fmt = _(u'Enter new value for option %(option)s '
    33         input_fmt = _(u'Enter new value for option %(option)s '
    31                       u'[%(current_value)s]: ')
    34                       u'[%(current_value)s]: ')
    32         failures = 0
    35         failures = 0
    33 
    36 
    34         w_std(_(u'Using configuration file: %s\n') % self._cfg_filename)
    37         w_std(_(u'Using configuration file: %s\n') % self._cfg_filename)
    35         for s in sections:
    38         for section in sections:
    36             w_std(_(u'* Configuration section: %r') % s)
    39             w_std(_(u'* Configuration section: %r') % section)
    37             for opt, val in self.items(s):
    40             for opt, val in self.items(section):
    38                 failures = 0
    41                 failures = 0
    39                 while True:
    42                 while True:
    40                     newval = raw_input(input_fmt.encode(ENCODING, 'replace') %
    43                     newval = raw_input(input_fmt.encode(ENCODING, 'replace') %
    41                                        {'option': opt, 'current_value': val})
    44                                        {'option': opt, 'current_value': val})
    42                     if newval and newval != val:
    45                     if newval and newval != val:
    43                         try:
    46                         try:
    44                             LazyConfig.set(self, '%s.%s' % (s, opt), newval)
    47                             LazyConfig.set(self, '%s.%s' % (section, opt),
       
    48                                            newval)
    45                             break
    49                             break
    46                         except (ValueError, ConfigValueError), e:
    50                         except (ValueError, ConfigValueError), err:
    47                             w_std(_(u'Warning: %s') % e)
    51                             w_err(0, _(u'Warning: %s') % err)
    48                             failures += 1
    52                             failures += 1
    49                             if failures > 2:
    53                             if failures > 2:
    50                                 raise ConfigError(_(u'Too many failures - try '
    54                                 raise ConfigError(_(u'Too many failures - try '
    51                                                     u'again later.'),
    55                                                     u'again later.'),
    52                                                   VMM_TOO_MANY_FAILURES)
    56                                                   VMM_TOO_MANY_FAILURES)
    53                     else:
    57                     else:
    54                         break
    58                         break
    55             print
    59             print
    56         if self._modified:
    60         if self._modified:
    57             self.__saveChanges()
    61             self.__save_changes()
    58 
    62 
    59     def set(self, option, value):
    63     def set(self, option, value):
    60         """Set the value of an option.
    64         """Set the value of an option.
    61 
    65 
    62         If the new `value` has been set, the configuration file will be
    66         If the new `value` has been set, the configuration file will be
    76         if val == old_val:
    80         if val == old_val:
    77             return
    81             return
    78         if not RawConfigParser.has_section(self, section):
    82         if not RawConfigParser.has_section(self, section):
    79             self.add_section(section)
    83             self.add_section(section)
    80         RawConfigParser.set(self, section, option_, val)
    84         RawConfigParser.set(self, section, option_, val)
    81         self.__saveChanges()
    85         self.__save_changes()
    82 
    86 
    83     def __saveChanges(self):
    87     def __save_changes(self):
    84         """Writes changes to the configuration file."""
    88         """Writes changes to the configuration file."""
    85         copy2(self._cfg_filename, self._cfg_filename + '.bak')
    89         copy2(self._cfg_filename, self._cfg_filename + '.bak')
    86         self._cfg_file = open(self._cfg_filename, 'w')
    90         self._cfg_file = open(self._cfg_filename, 'w')
    87         self.write(self._cfg_file)
    91         self.write(self._cfg_file)
    88         self._cfg_file.close()
    92         self._cfg_file.close()
       
    93 
       
    94 del _