VirtualMailManager/cli/Config.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 26 Jul 2010 16:02:58 +0000
branchv0.6.x
changeset 314 0b4a6e9d9f27
parent 313 c17c46d9e440
child 316 31d8931dc535
permissions -rw-r--r--
VMM/cli: Removed misplaced function string_io.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     2
# Copyright (c) 2010, Pascal Volk
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
"""
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
    VirtualMailManager.cli.CliConfig
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
     7
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
    Adds some interactive stuff to the Config class.
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
"""
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
206
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
    12
from ConfigParser import RawConfigParser
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    13
from shutil import copy2
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    14
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
from VirtualMailManager import ENCODING
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    16
from VirtualMailManager.Config import Config, ConfigValueError, LazyConfig
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    17
from VirtualMailManager.errors import ConfigError
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    18
from VirtualMailManager.cli import w_err, w_std
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
from VirtualMailManager.constants.ERROR import VMM_TOO_MANY_FAILURES
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    21
_ = lambda msg: msg
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    22
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    23
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
class CliConfig(Config):
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
    """Adds the interactive ``configure`` method to the `Config` class
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
    and overwrites `LazyConfig.set(), in order to update a single option
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
    in the configuration file with a single command line command.
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
    """
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    29
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
    def configure(self, sections):
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
        """Interactive method for configuring all options of the given
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
        iterable ``sections`` object."""
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    33
        input_fmt = _(u'Enter new value for option %(option)s '
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    34
                      u'[%(current_value)s]: ')
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
        failures = 0
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 206
diff changeset
    37
        w_std(_(u'Using configuration file: %s\n') % self._cfg_filename)
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    38
        for section in sections:
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    39
            w_std(_(u'* Configuration section: %r') % section)
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    40
            for opt, val in self.items(section):
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
                failures = 0
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    42
                while True:
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    43
                    newval = raw_input(input_fmt.encode(ENCODING, 'replace') %
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    44
                                       {'option': opt, 'current_value': val})
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
                    if newval and newval != val:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
                        try:
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    47
                            LazyConfig.set(self, '%s.%s' % (section, opt),
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    48
                                           newval)
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
                            break
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    50
                        except (ValueError, ConfigValueError), err:
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    51
                            w_err(0, _(u'Warning: %s') % err)
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
                            failures += 1
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
                            if failures > 2:
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    54
                                raise ConfigError(_(u'Too many failures - try '
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    55
                                                    u'again later.'),
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    56
                                                  VMM_TOO_MANY_FAILURES)
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
                    else:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
                        break
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
            print
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        if self._modified:
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    61
            self.__save_changes()
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
    def set(self, option, value):
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
        """Set the value of an option.
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
        If the new `value` has been set, the configuration file will be
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
        immediately updated.
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
        Throws a ``ValueError`` if `value` couldn't be converted to
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
        ``LazyConfigOption.cls``"""
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
        section, option_ = self._get_section_option(option)
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
        val = self._cfg[section][option_].cls(value)
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    73
        if self._cfg[section][option_].validate:
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
            val = self._cfg[section][option_].validate(val)
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
        # Do not write default values also skip identical values
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
        if not self._cfg[section][option_].default is None:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
            old_val = self.dget(option)
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
        else:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
            old_val = self.pget(option)
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        if val == old_val:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
            return
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
        if not RawConfigParser.has_section(self, section):
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
            self.add_section(section)
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
        RawConfigParser.set(self, section, option_, val)
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    85
        self.__save_changes()
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    87
    def __save_changes(self):
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
        """Writes changes to the configuration file."""
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 206
diff changeset
    89
        copy2(self._cfg_filename, self._cfg_filename + '.bak')
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 206
diff changeset
    90
        self._cfg_file = open(self._cfg_filename, 'w')
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 206
diff changeset
    91
        self.write(self._cfg_file)
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 206
diff changeset
    92
        self._cfg_file.close()
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    93
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    94
del _