VirtualMailManager/cli/Config.py
author Pascal Volk <neverseen@users.sourceforge.net>
Sat, 13 Feb 2010 04:20:09 +0000
branchv0.6.x
changeset 205 bc9726c9ad85
parent 204 83938336c518
child 206 da07dd944ad1
permissions -rw-r--r--
VMM/cli: added __init__.py to the repository.
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
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
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    11
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
    12
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
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
    14
from VirtualMailManager.Config import Config, ConfigValueError, LazyConfig
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    15
from VirtualMailManager.Exceptions import VMMConfigException
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
from VirtualMailManager.cli import w_std
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
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
    18
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    19
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
class CliConfig(Config):
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
    """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
    22
    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
    23
    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
    24
    """
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    25
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
    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
    27
        """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
    28
        iterable ``sections`` object."""
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
        input_fmt = _(u'Enter new value for option %(option)s \
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
[%(current_value)s]: ')
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
        failures = 0
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
        w_std(_(u'Using configuration file: %s\n') % self._cfgFileName)
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
        for s in sections:
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    35
            w_std(_(u'* Configuration section: %r') % s)
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
            for opt, val in self.items(s):
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
                failures = 0
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
                while True:
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    39
                    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
    40
                                       {'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
    41
                    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
    42
                        try:
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    43
                            LazyConfig.set(self, '%s.%s' % (s, opt), newval)
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
                            break
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
                        except (ValueError, ConfigValueError), e:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
                            w_std(_(u'Warning: %s') % e)
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
                            failures += 1
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
                            if failures > 2:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
                                raise VMMConfigException(
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
                                    _(u'Too many failures - try again later.'),
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    51
                                                         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
    52
                    else:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
                        break
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
            print
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
        if self._modified:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
            self.__saveChanges()
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
    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
    59
        """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
    60
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
        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
    62
        immediately updated.
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
        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
    65
        ``LazyConfigOption.cls``"""
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
        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
    67
        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
    68
        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
    69
            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
    70
        # 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
    71
        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
    72
            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
    73
        else:
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
            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
    75
        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
    76
            return
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
        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
    78
            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
    79
        RawConfigParser.set(self, section, option_, val)
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        self.__saveChanges()
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
    def __saveChanges(self):
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
        """Writes changes to the configuration file."""
204
83938336c518 VMM/{,cli/}Config: fixed imports. Small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 189
diff changeset
    84
        copy2(self._cfgFileName, self._cfgFileName + '.bak')
187
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        self._cfgFile = open(self._cfgFileName, 'w')
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
        self.write(self._cfgFile)
38b9a9859749 VMM/{,cli/Cli}Config: Moved interactive stuff to new CliConfig class.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
        self._cfgFile.close()