VirtualMailManager/cli/config.py
branchv0.7.x
changeset 659 a6b6b0937cae
parent 658 ad967c4b7fe5
child 676 2bc11dada296
equal deleted inserted replaced
658:ad967c4b7fe5 659:a6b6b0937cae
     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 from string import Template
    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, VMMError
    17 from VirtualMailManager.errors import ConfigError, VMMError
    17 from VirtualMailManager.cli import w_err, w_std
    18 from VirtualMailManager.cli import w_err, w_std
    27     """
    28     """
    28 
    29 
    29     def configure(self, sections):
    30     def configure(self, sections):
    30         """Interactive method for configuring all options of the given
    31         """Interactive method for configuring all options of the given
    31         iterable ``sections`` object."""
    32         iterable ``sections`` object."""
    32         input_fmt = _('Enter new value for option %(option)s '
    33         input_tpl = Template(_('Enter new value for option $option '
    33                       '[%(current_value)s]: ')
    34                                '[$current_value]: '))
    34         failures = 0
    35         failures = 0
    35 
    36 
    36         w_std(_('Using configuration file: %s\n') % self._cfg_filename)
    37         w_std(_('Using configuration file: %s\n') % self._cfg_filename)
    37         for section in sections:
    38         for section in sections:
    38             w_std(_("* Configuration section: '%s'") % section)
    39             w_std(_("* Configuration section: '%s'") % section)
    39             for opt, val in self.items(section):
    40             for opt, val in self.items(section):
    40                 failures = 0
    41                 failures = 0
    41                 while True:
    42                 while True:
    42                     newval = input(input_fmt.encode(ENCODING, 'replace') %
    43                     if isinstance(val, str):
    43                                        {'option': opt, 'current_value': val})
    44                         val = val.encode(ENCODING, 'replace').decode(ENCODING)
       
    45                     newval = input(input_tpl.substitute(option=opt,
       
    46                                                         current_value=val))
    44                     if newval and newval != val:
    47                     if newval and newval != val:
    45                         try:
    48                         try:
    46                             LazyConfig.set(self, '%s.%s' % (section, opt),
    49                             LazyConfig.set(self, '%s.%s' % (section, opt),
    47                                            newval)
    50                                            newval)
    48                             break
    51                             break