Issues (229)

server/lib/ige/Config.py (1 issue)

1
#
2
#  Copyright 2001 - 2016 Ludek Smid [http://www.ospace.net/]
3
#
4
#  This file is part of Outer Space.
5
#
6
#  Outer Space is free software; you can redistribute it and/or modify
7
#  it under the terms of the GNU General Public License as published by
8
#  the Free Software Foundation; either version 2 of the License, or
9
#  (at your option) any later version.
10
#
11
#  Outer Space is distributed in the hope that it will be useful,
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#  GNU General Public License for more details.
15
#
16
#  You should have received a copy of the GNU General Public License
17
#  along with Outer Space; if not, write to the Free Software
18
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
21
from ConfigParser import ConfigParser
22
23
class Config:
24
    """Encapsulation of ConfigParser class.
25
26
    Attributes of this class represents sections
27
    of config file. If some want to assing value
28
    to section, an AttruteError is thrown, except
29
    None value, which removes section with given
30
    name. When asking for nonexisting section,
31
    section with same name is created. For
32
    existing section an instance of Section class
33
    is returned.
34
    """
35
    def __init__(self, _file, defaults = dict()):
36
        self.__dict__["_config"] = ConfigParser(defaults)
37
        self.__dict__["_file"] = _file
38
        self._config.read(_file)
39
40
    def __getitem__(self, name):
41
        return self.__getattr__(name)
42
43
    def __getattr__(self, name):
44
        if not self._config.has_section(name):
45
            self._config.add_section(name)
46
47
        return Section(self._config, name)
48
49
    def __setattr__(self, name, value):
50
        if value == None:
51
            self._config.remove_section(name)
52
        else:
53
            raise AttributeError("Cannot assign value to config section")
54
55
    def sections(self):
56
        return self._config.sections()
57
58
    def save(self, _file = None):
59
        if _file is None:
60
            _file = self._file
61
        with open(_file, 'w') as fh:
62
            self._config.write(fh)
63
64 View Code Duplication
class Section:
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
65
    """Represent section of ConfigParser class.
66
67
    Attributes of this class represents options
68
    of given section. when asking for option
69
    value None value is returned if given option
70
    does not exist, otherwise option value is
71
    returned.
72
    """
73
    def __init__(self, config, name):
74
        self.__dict__["_config"] = config
75
        self.__dict__["section"] = name
76
77
    def __getattr__(self, name):
78
        if self._config.has_option(self.section, name):
79
            return self._config.get(self.section, name)
80
        else:
81
            return None
82
83
    def __setattr__(self, name, value):
84
        if value is None:
85
            self._config.remove_option(self.section, name)
86
            if not self._config.options(self.section):
87
                # no option -- delete me
88
                self._config.remove_section(self.section)
89
        else:
90
            self._config.set(self.section, name, str(value))
91