VirtualMailManager/Config.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 18 Jan 2010 03:23:50 +0000
branchv0.6.x
changeset 169 a72908248153
parent 168 fd496561acc6
child 171 6f8ac86d1611
permissions -rw-r--r--
man: updated man/{de/,}man5/vmm.cfg.5, using reStructuredText now
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
162
0ac9ef587769 Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
     2
# Copyright (c) 2007 - 2010, Pascal Volk
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
     5
"""Configuration class for read, modify and write the
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
configuration from Virtual Mail Manager.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
"""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
from shutil import copy2
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    11
from ConfigParser import ConfigParser, MissingSectionHeaderError, ParsingError
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    12
from cStringIO import StringIO
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 136
diff changeset
    14
from __main__ import ENCODING, ERR, w_std
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
from Exceptions import VMMConfigException
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
49
9bd033177377 * 'VirtualMailManager/Config.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    17
class Config(ConfigParser):
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    18
    """This class is for reading and modifying vmm's configuration file."""
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
    def __init__(self, filename):
49
9bd033177377 * 'VirtualMailManager/Config.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    21
        """Creates a new Config instance
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    23
        Arguments:
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    24
        filename -- path to the configuration file
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
        ConfigParser.__init__(self)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
        self.__cfgFileName = filename
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
        self.__cfgFile = None
166
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    29
        self.__VMMsections = ('account', 'bin', 'database', 'domain',
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    30
                              'maildir', 'misc', 'config')
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
        self.__changes = False
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    32
        self.__missing = {}
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
        self.__dbopts = [
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
                ['host', 'localhot'],
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
                ['user', 'vmm'],
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
                ['pass', 'your secret password'],
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
                ['name', 'mailsys']
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
                ]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
        self.__mdopts = [
68
7ae95b9938c4 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    40
                ['name', 'Maildir'],
7ae95b9938c4 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    41
                ['folders', 'Drafts:Sent:Templates:Trash'],
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    42
                ]
166
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    43
        self.__accountopts = [
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    44
                ['delete_directory', 'false'],
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    45
                ['directory_mode', 448],
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    46
                ['disk_usage', 'false'],
168
fd496561acc6 vmm.cfg: a few re-renamed configuration options.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 166
diff changeset
    47
                ['password_length', 8],
166
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    48
                ['random_password', 'false'],
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
    49
                ['smtp', 'true'],
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
    50
                ['pop3', 'true'],
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
    51
                ['imap', 'true'],
113
e35755191ff3 Config: renamed services.managesieve to services.sieve,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 112
diff changeset
    52
                ['sieve', 'true']
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
    53
                ]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
        self.__domdopts = [
166
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    55
                ['auto_postmaster', 'true'],
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    56
                ['delete_directory', 'false'],
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    57
                ['directory_mode', 504],
168
fd496561acc6 vmm.cfg: a few re-renamed configuration options.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 166
diff changeset
    58
                ['force_deletion', 'false'],
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
                ]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        self.__binopts = [
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
                ['dovecotpw', '/usr/sbin/dovecotpw'],
86
e43e4e7e9ec0 * 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    62
                ['du', '/usr/bin/du'],
e43e4e7e9ec0 * 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    63
                ['postconf', '/usr/sbin/postconf']
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
                ]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
        self.__miscopts = [
168
fd496561acc6 vmm.cfg: a few re-renamed configuration options.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 166
diff changeset
    66
                ['base_directory', '/srv/mail'],
fd496561acc6 vmm.cfg: a few re-renamed configuration options.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 166
diff changeset
    67
                ['dovecot_version', '11'],
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
                ['gid_mail', 8],
166
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    69
                ['password_scheme', 'PLAIN'],
113
e35755191ff3 Config: renamed services.managesieve to services.sieve,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 112
diff changeset
    70
                ['transport', 'dovecot:'],
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
                ]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
    def load(self):
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    74
        """Loads the configuration, read only.
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    75
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    76
        Raises a VMMConfigException if the configuration syntax is invalid.
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    77
        """
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
        try:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
            self.__cfgFile = file(self.__cfgFileName, 'r')
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    80
            self.readfp(self.__cfgFile)
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    81
        except (MissingSectionHeaderError, ParsingError), e:
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    82
            self.__cfgFile.close()
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    83
            raise VMMConfigException(str(e), ERR.CONF_ERROR)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
        self.__cfgFile.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    86
    def check(self):
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    87
        """Performs a configuration check.
128
cf8116625866 Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 120
diff changeset
    88
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    89
        Raises a VMMConfigException if the check fails.
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    90
        """
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    91
        if not self.__chkSections():
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    92
            errmsg = StringIO()
94
0d303d15549e * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 86
diff changeset
    93
            errmsg.write(_("Using configuration file: %s\n") %\
0d303d15549e * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 86
diff changeset
    94
                    self.__cfgFileName)
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    95
            for k,v in self.__missing.items():
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    96
                if v[0] is True:
47
191d5a5adc4a * Removed gettext import and setup in VirtualMailManager/:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
    97
                    errmsg.write(_(u"missing section: %s\n") % k)
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    98
                else:
47
191d5a5adc4a * Removed gettext import and setup in VirtualMailManager/:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
    99
                    errmsg.write(_(u"missing options in section %s:\n") % k)
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   100
                    for o in v:
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   101
                        errmsg.write(" * %s\n" % o)
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
   102
            raise VMMConfigException(errmsg.getvalue(), ERR.CONF_ERROR)
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   103
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   104
    def getsections(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
        """Return a list with all configurable sections."""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   106
        return self.__VMMsections[:-1]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
69
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 68
diff changeset
   108
    def get(self, section, option, raw=False, vars=None):
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 68
diff changeset
   109
        return unicode(ConfigParser.get(self, section, option, raw, vars),
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 68
diff changeset
   110
                ENCODING, 'replace')
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 68
diff changeset
   111
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   112
    def configure(self, sections):
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   113
        """Interactive method for configuring all options in the given sections
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   115
        Arguments:
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   116
        sections -- list of strings with section names
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   117
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   118
        if not isinstance(sections, list):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 51
diff changeset
   119
            raise TypeError("Argument 'sections' is not a list.")
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   120
        # if [config] done = false (default at 1st run),
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   121
        # then set changes true
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   122
        try:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   123
            if not self.getboolean('config', 'done'):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   124
                self.__changes = True
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   125
        except ValueError:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   126
            self.set('config', 'done', 'False')
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   127
            self.__changes = True
112
d1f345f91e1c Hopefully the final UnicodeDecodeError fix for the Config class
Pascal Volk <neverseen@users.sourceforge.net>
parents: 103
diff changeset
   128
        w_std(_(u'Using configuration file: %s\n') % self.__cfgFileName)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   129
        for s in sections:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
   130
            if s != 'config':
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 128
diff changeset
   131
                w_std(_(u'* Config section: “%s”') % s )
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   132
            for opt, val in self.items(s):
47
191d5a5adc4a * Removed gettext import and setup in VirtualMailManager/:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   133
                newval = raw_input(
51
f5ca21303871 * Switched from pygettext to xgettext -> small adjustments to these files:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 49
diff changeset
   134
                _('Enter new value for option %(opt)s [%(val)s]: ').encode(
f5ca21303871 * Switched from pygettext to xgettext -> small adjustments to these files:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 49
diff changeset
   135
                    ENCODING, 'replace') % {'opt': opt, 'val': val})
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   136
                if newval and newval != val:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   137
                    self.set(s, opt, newval)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   138
                    self.__changes = True
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   139
            print
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   140
        if self.__changes:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   141
            self.__saveChanges()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   142
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   143
    def __saveChanges(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   144
        """Writes changes to the configuration file."""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   145
        self.set('config', 'done', 'true')
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   146
        copy2(self.__cfgFileName, self.__cfgFileName+'.bak')
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   147
        self.__cfgFile = file(self.__cfgFileName, 'w')
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   148
        self.write(self.__cfgFile)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   149
        self.__cfgFile.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   150
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   151
    def __chkSections(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   152
        """Checks if all configuration sections are existing."""
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   153
        errors = False
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   154
        for s in self.__VMMsections:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   155
            if not self.has_section(s):
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   156
                self.__missing[s] = [True]
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   157
            elif not self.__chkOptions(s):
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   158
                errors = True
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   159
        return not errors
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   160
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   161
    def __chkOptions(self, section):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   162
        """Checks if all configuration options in section are existing.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   163
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   164
        Arguments:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   165
        section -- the section to be checked
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   166
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   167
        retval = True
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   168
        missing = []
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   169
        if section == 'database':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   170
            opts = self.__dbopts
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   171
        elif section == 'maildir':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   172
            opts = self.__mdopts
166
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   173
        elif section == 'account':
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   174
            opts = self.__accountopts
b152ad5c7071 vmm.cfg: dealt with the most overdue section/option renaming.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
   175
        elif section == 'domain':
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   176
            opts = self.__domdopts
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   177
        elif section == 'bin':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   178
            opts = self.__binopts
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   179
        elif section == 'misc':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   180
            opts = self.__miscopts
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   181
        elif section == 'config':
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   182
            opts = [['done', 'false']]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   183
        for o, v in opts:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   184
            if not self.has_option(section, o):
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   185
                missing.append(o)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   186
                retval = False
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   187
        if len(missing):
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   188
            self.__missing[section] = missing
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   189
        return retval