VirtualMailManager/cli/config.py
author Pascal Volk <neverseen@users.sourceforge.net>
Sat, 07 Aug 2010 04:44:23 +0000
branchv0.6.x
changeset 349 d60ffbc0124b
parent 344 0d2430dc6ef8
child 366 d6573da35b5f
permissions -rw-r--r--
VMM/cli/config: Except the VMMError from exec_ok in configure().
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
"""
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
     5
    VirtualMailManager.cli.config
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
    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
     9
"""
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
206
da07dd944ad1 VMM/Config: renamed LazyConfig's get_boolean() to getboolean().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 204
diff changeset
    11
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
    12
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
    13
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
from VirtualMailManager import ENCODING
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    15
from VirtualMailManager.config import Config, ConfigValueError, LazyConfig
349
d60ffbc0124b VMM/cli/config: Except the VMMError from exec_ok in configure().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 344
diff changeset
    16
from VirtualMailManager.errors import ConfigError, VMMError
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    17
from VirtualMailManager.cli import w_err, w_std
344
0d2430dc6ef8 VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
    18
from VirtualMailManager.constants import CONF_ERROR, 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
    19
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    20
_ = lambda msg: msg
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    21
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    22
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
class CliConfig(Config):
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
    """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
    25
    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
    26
    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
    27
    """
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    28
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
    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
    30
        """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
    31
        iterable ``sections`` object."""
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    32
        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
    33
                      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
    34
        failures = 0
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 206
diff changeset
    36
        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
    37
        for section in sections:
341
6709d0faf2f5 VMM/…: Unified messages and removed non-ASCII quotes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 322
diff changeset
    38
            w_std(_(u"* Configuration section: '%s'") % section)
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    39
            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
    40
                failures = 0
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
                while True:
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    42
                    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
    43
                                       {'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
    44
                    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
    45
                        try:
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    46
                            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
    47
                                           newval)
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
                            break
349
d60ffbc0124b VMM/cli/config: Except the VMMError from exec_ok in configure().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 344
diff changeset
    49
                        except (ValueError, ConfigValueError, VMMError), err:
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    50
                            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
    51
                            failures += 1
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
                            if failures > 2:
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    53
                                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
    54
                                                    u'again later.'),
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    55
                                                  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
    56
                    else:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
                        break
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
            print
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
        if self._modified:
322
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    60
            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
    61
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
    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
    63
        """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
    64
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
        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
    66
        immediately updated.
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
344
0d2430dc6ef8 VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
    68
        Throws a ``ConfigError`` if `value` couldn't be converted to
0d2430dc6ef8 VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
    69
        ``LazyConfigOption.cls`` or ``LazyConfigOption.validate`` fails."""
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
        section, option_ = self._get_section_option(option)
344
0d2430dc6ef8 VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
    71
        try:
0d2430dc6ef8 VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
    72
            val = self._cfg[section][option_].cls(value)
0d2430dc6ef8 VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
    73
            if self._cfg[section][option_].validate:
0d2430dc6ef8 VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
    74
                val = self._cfg[section][option_].validate(val)
0d2430dc6ef8 VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
    75
        except (ValueError, ConfigValueError), err:
0d2430dc6ef8 VMM/cli/config: CliConfig.set raise a ConfigError, if the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
    76
            raise ConfigError(str(err), CONF_ERROR)
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
        # 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
    78
        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
    79
            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
    80
        else:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
            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
    82
        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
    83
            return
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
        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
    85
            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
    86
        RawConfigParser.set(self, section, option_, val)
322
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    87
        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
    88
322
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    89
    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
    90
        """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
    91
        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
    92
        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
    93
        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
    94
        self._cfg_file.close()
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    95
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    96
del _