VirtualMailManager/Config.py
author Pascal Volk <neverseen@users.sourceforge.net>
Sat, 22 Aug 2009 18:21:04 +0000
changeset 130 9dca3e898ddb
parent 128 cf8116625866
child 133 2d5c4745efec
permissions -rw-r--r--
avoid error message if used with version 0.5.2
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 -*-
102
485d3f7d6981 Removed svn keywords, more POSIX compliant shell scripts
Pascal Volk <neverseen@users.sourceforge.net>
parents: 98
diff changeset
     2
# Copyright (c) 2007 - 2009, VEB IT
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
102
485d3f7d6981 Removed svn keywords, more POSIX compliant shell scripts
Pascal Volk <neverseen@users.sourceforge.net>
parents: 98
diff changeset
    10
from constants.VERSION import *
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
47
191d5a5adc4a * Removed gettext import and setup in VirtualMailManager/:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
    12
import locale
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
import sys
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
from shutil import copy2
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    15
from ConfigParser import ConfigParser, MissingSectionHeaderError, ParsingError
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    16
from cStringIO import StringIO
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
from Exceptions import VMMConfigException
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    19
import constants.ERROR as ERR
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
47
191d5a5adc4a * Removed gettext import and setup in VirtualMailManager/:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
    21
locale.setlocale(locale.LC_ALL, '')
191d5a5adc4a * Removed gettext import and setup in VirtualMailManager/:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
    22
ENCODING = locale.nl_langinfo(locale.CODESET)
32
ceb700bc4a80 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 24
diff changeset
    23
112
d1f345f91e1c Hopefully the final UnicodeDecodeError fix for the Config class
Pascal Volk <neverseen@users.sourceforge.net>
parents: 103
diff changeset
    24
def w_std(*args):
d1f345f91e1c Hopefully the final UnicodeDecodeError fix for the Config class
Pascal Volk <neverseen@users.sourceforge.net>
parents: 103
diff changeset
    25
    for arg in args:
d1f345f91e1c Hopefully the final UnicodeDecodeError fix for the Config class
Pascal Volk <neverseen@users.sourceforge.net>
parents: 103
diff changeset
    26
        sys.stdout.write(arg.encode(ENCODING, 'replace'))
d1f345f91e1c Hopefully the final UnicodeDecodeError fix for the Config class
Pascal Volk <neverseen@users.sourceforge.net>
parents: 103
diff changeset
    27
        sys.stdout.write('\n')
d1f345f91e1c Hopefully the final UnicodeDecodeError fix for the Config class
Pascal Volk <neverseen@users.sourceforge.net>
parents: 103
diff changeset
    28
49
9bd033177377 * 'VirtualMailManager/Config.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    29
class Config(ConfigParser):
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    30
    """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
    31
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
    def __init__(self, filename):
49
9bd033177377 * 'VirtualMailManager/Config.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    33
        """Creates a new Config instance
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    35
        Arguments:
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    36
        filename -- path to the configuration file
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
        ConfigParser.__init__(self)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
        self.__cfgFileName = filename
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
        self.__cfgFile = None
21
6feb31787a66 * 'vmm.1'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 20
diff changeset
    41
        self.__VMMsections = ['database', 'maildir', 'services', 'domdir',
6feb31787a66 * 'vmm.1'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 20
diff changeset
    42
                'bin', 'misc', 'config']
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
        self.__changes = False
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    44
        self.__missing = {}
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
        self.__dbopts = [
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
                ['host', 'localhot'],
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
                ['user', 'vmm'],
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
                ['pass', 'your secret password'],
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
                ['name', 'mailsys']
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
                ]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
        self.__mdopts = [
68
7ae95b9938c4 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    52
                ['name', 'Maildir'],
7ae95b9938c4 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    53
                ['folders', 'Drafts:Sent:Templates:Trash'],
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
                ['mode', 448],
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
                ['diskusage', 'false'],
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
                ['delete', 'false']
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
                ]
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
    58
        self.__serviceopts = [
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
    59
                ['smtp', 'true'],
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
    60
                ['pop3', 'true'],
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
    61
                ['imap', 'true'],
113
e35755191ff3 Config: renamed services.managesieve to services.sieve,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 112
diff changeset
    62
                ['sieve', 'true']
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
    63
                ]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
        self.__domdopts = [
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
    65
                ['base', '/srv/mail'],
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
                ['mode', 504],
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
                ['delete', 'false']
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
                ]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
        self.__binopts = [
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
                ['dovecotpw', '/usr/sbin/dovecotpw'],
86
e43e4e7e9ec0 * 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    71
                ['du', '/usr/bin/du'],
e43e4e7e9ec0 * 'VirtualMailManager/ext/Postconf.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    72
                ['postconf', '/usr/sbin/postconf']
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
                ]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
        self.__miscopts = [
24
48ea255e8a85 * 'vmm.cfg.5'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 21
diff changeset
    75
                ['passwdscheme', 'PLAIN'],
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
                ['gid_mail', 8],
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    77
                ['forcedel', 'false'],
113
e35755191ff3 Config: renamed services.managesieve to services.sieve,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 112
diff changeset
    78
                ['transport', 'dovecot:'],
e35755191ff3 Config: renamed services.managesieve to services.sieve,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 112
diff changeset
    79
                ['dovecotvers', '11']
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
                ]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
    def load(self):
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    83
        """Loads the configuration, read only.
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    84
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    85
        Raises a VMMConfigException if the configuration syntax is invalid.
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    86
        """
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
        try:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
            self.__cfgFile = file(self.__cfgFileName, 'r')
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    89
            self.readfp(self.__cfgFile)
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    90
        except (MissingSectionHeaderError, ParsingError), e:
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    91
            self.__cfgFile.close()
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    92
            raise VMMConfigException(str(e), ERR.CONF_ERROR)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        self.__cfgFile.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    95
    def check(self):
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    96
        """Performs a configuration check.
128
cf8116625866 Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 120
diff changeset
    97
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    98
        Raises a VMMConfigException if the check fails.
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
    99
        """
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   100
        if not self.__chkSections():
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   101
            errmsg = StringIO()
94
0d303d15549e * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 86
diff changeset
   102
            errmsg.write(_("Using configuration file: %s\n") %\
0d303d15549e * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 86
diff changeset
   103
                    self.__cfgFileName)
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   104
            for k,v in self.__missing.items():
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   105
                if v[0] is True:
47
191d5a5adc4a * Removed gettext import and setup in VirtualMailManager/:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   106
                    errmsg.write(_(u"missing section: %s\n") % k)
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   107
                else:
47
191d5a5adc4a * Removed gettext import and setup in VirtualMailManager/:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   108
                    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
   109
                    for o in v:
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   110
                        errmsg.write(" * %s\n" % o)
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
   111
            raise VMMConfigException(errmsg.getvalue(), ERR.CONF_ERROR)
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   112
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   113
    def getsections(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
        """Return a list with all configurable sections."""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   115
        return self.__VMMsections[:-1]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   116
69
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 68
diff changeset
   117
    def get(self, section, option, raw=False, vars=None):
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 68
diff changeset
   118
        return unicode(ConfigParser.get(self, section, option, raw, vars),
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 68
diff changeset
   119
                ENCODING, 'replace')
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 68
diff changeset
   120
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   121
    def configure(self, sections):
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   122
        """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
   123
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   124
        Arguments:
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   125
        sections -- list of strings with section names
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   126
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   127
        if not isinstance(sections, list):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 51
diff changeset
   128
            raise TypeError("Argument 'sections' is not a list.")
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   129
        # if [config] done = false (default at 1st run),
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   130
        # then set changes true
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   131
        try:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   132
            if not self.getboolean('config', 'done'):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   133
                self.__changes = True
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   134
        except ValueError:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   135
            self.set('config', 'done', 'False')
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   136
            self.__changes = True
112
d1f345f91e1c Hopefully the final UnicodeDecodeError fix for the Config class
Pascal Volk <neverseen@users.sourceforge.net>
parents: 103
diff changeset
   137
        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
   138
        for s in sections:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
   139
            if s != 'config':
112
d1f345f91e1c Hopefully the final UnicodeDecodeError fix for the Config class
Pascal Volk <neverseen@users.sourceforge.net>
parents: 103
diff changeset
   140
                w_std(_(u'* Config section: »%s«') % s )
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   141
            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
   142
                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
   143
                _('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
   144
                    ENCODING, 'replace') % {'opt': opt, 'val': val})
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   145
                if newval and newval != val:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   146
                    self.set(s, opt, newval)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   147
                    self.__changes = True
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   148
            print
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   149
        if self.__changes:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   150
            self.__saveChanges()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   151
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   152
    def __saveChanges(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   153
        """Writes changes to the configuration file."""
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   154
        self.set('config', 'done', 'true')
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   155
        copy2(self.__cfgFileName, self.__cfgFileName+'.bak')
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   156
        self.__cfgFile = file(self.__cfgFileName, 'w')
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   157
        self.write(self.__cfgFile)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   158
        self.__cfgFile.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   159
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   160
    def __chkSections(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   161
        """Checks if all configuration sections are existing."""
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   162
        errors = False
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   163
        for s in self.__VMMsections:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   164
            if not self.has_section(s):
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   165
                self.__missing[s] = [True]
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   166
            elif not self.__chkOptions(s):
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   167
                errors = True
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   168
        return not errors
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   169
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   170
    def __chkOptions(self, section):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   171
        """Checks if all configuration options in section are existing.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   172
120
928659c8ee9f Comments updated.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   173
        Arguments:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   174
        section -- the section to be checked
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   175
        """
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   176
        retval = True
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   177
        missing = []
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   178
        if section == 'database':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   179
            opts = self.__dbopts
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   180
        elif section == 'maildir':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   181
            opts = self.__mdopts
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   182
        elif section == 'services':
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   183
            opts = self.__serviceopts
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   184
        elif section == 'domdir':
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   185
            opts = self.__domdopts
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   186
        elif section == 'bin':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   187
            opts = self.__binopts
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   188
        elif section == 'misc':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   189
            opts = self.__miscopts
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   190
        elif section == 'config':
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   191
            opts = [['done', 'false']]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   192
        for o, v in opts:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   193
            if not self.has_option(section, o):
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   194
                missing.append(o)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   195
                retval = False
2
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   196
        if len(missing):
9b39f828aa8a * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
   197
            self.__missing[section] = missing
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   198
        return retval