VirtualMailManager/cli/config.py
author Pascal Volk <user@localhost.localdomain.org>
Sat, 01 Dec 2012 17:25:11 +0000
branchv0.7.x
changeset 656 4bbca60e0ba4
parent 643 df1e3b67882a
child 658 ad967c4b7fe5
permissions -rw-r--r--
VMM/cli/subcommands: Print usable password schemes in sorted order.
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 -*-
568
14abdd04ddf5 Updated copyright notices to include the year 2012.
Pascal Volk <user@localhost.localdomain.org>
parents: 366
diff changeset
     2
# Copyright (c) 2010 - 2012, Pascal Volk
187
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
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
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."""
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
diff changeset
    32
        input_fmt = _('Enter new value for option %(option)s '
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
diff changeset
    33
                      '[%(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
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
diff changeset
    36
        w_std(_('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:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
diff changeset
    38
            w_std(_("* 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:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
diff changeset
    42
                    newval = input(input_fmt.encode(ENCODING, 'replace') %
204
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
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
diff changeset
    49
                        except (ValueError, ConfigValueError, VMMError) as err:
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
diff changeset
    50
                            w_err(0, _('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:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
diff changeset
    53
                                raise ConfigError(_('Too many failures - try '
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
diff changeset
    54
                                                    '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
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
diff changeset
    58
            print()
187
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)
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 642
diff changeset
    75
        except (ValueError, ConfigValueError) as err:
344
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')
642
4cd9d0a9f42f Use the with statement for file operations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    92
        with open(self._cfg_filename, 'w') as self._cfg_file:
4cd9d0a9f42f Use the with statement for file operations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    93
            self.write(self._cfg_file)
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    94
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    95
del _