VirtualMailManager/cli/config.py
author Pascal Volk <neverseen@users.sourceforge.net>
Thu, 29 Jul 2010 01:48:25 +0000
branchv0.6.x
changeset 324 38e344ba3d0f
parent 322 94bd10e237e5
child 341 6709d0faf2f5
permissions -rw-r--r--
VMM/constants: Added MIN_GID and MIN_UID.
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
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    16
from VirtualMailManager.errors import ConfigError
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
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 313
diff changeset
    18
from VirtualMailManager.constants import 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:
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    38
            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
    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
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    49
                        except (ValueError, ConfigValueError), err:
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
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
        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
    69
        ``LazyConfigOption.cls``"""
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)
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
        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
    72
        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
    73
            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
    74
        # 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
    75
        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
    76
            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
    77
        else:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
            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
    79
        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
    80
            return
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
        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
    82
            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
    83
        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
    84
        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
    85
322
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    86
    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
    87
        """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
    88
        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
    89
        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
    90
        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
    91
        self._cfg_file.close()
313
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    92
c17c46d9e440 VMM/cli/Config: Write warnings to stderr.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    93
del _