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
|
|
|
import os.path |
22
|
|
|
from ConfigParser import ConfigParser |
23
|
|
|
|
24
|
|
|
class Config: |
25
|
|
|
"""Encapsulation of ConfigParser class. |
26
|
|
|
|
27
|
|
|
Attributes of this class represents sections |
28
|
|
|
of config file. If some want to assing value |
29
|
|
|
to section, an AttruteError is thrown, except |
30
|
|
|
None value, which removes section with given |
31
|
|
|
name. When asking for nonexisting section, |
32
|
|
|
section with same name is created. For |
33
|
|
|
existing section an instance of Section class |
34
|
|
|
is returned. |
35
|
|
|
""" |
36
|
|
|
def __init__(self, filename): |
37
|
|
|
self.__dict__["_config"] = ConfigParser() |
38
|
|
|
self.__dict__["_configFile"] = filename |
39
|
|
|
self._config.read(filename) |
40
|
|
|
|
41
|
|
|
def __getattr__(self, name): |
42
|
|
|
if not self._config.has_section(name): |
43
|
|
|
self._config.add_section(name) |
44
|
|
|
|
45
|
|
|
return Section(self._config, name) |
46
|
|
|
|
47
|
|
|
def __setattr__(self, name, value): |
48
|
|
|
if value == None: |
49
|
|
|
self._config.remove_section(name) |
50
|
|
|
else: |
51
|
|
|
raise AttributeError("Cannot assign value to config section") |
52
|
|
|
|
53
|
|
|
def save(self): |
54
|
|
|
fh = open(self._configFile, 'w') |
55
|
|
|
self._config.write(fh) |
56
|
|
|
fh.close() |
57
|
|
|
|
58
|
|
View Code Duplication |
class Section: |
|
|
|
|
59
|
|
|
"""Represent section of ConfigParser class. |
60
|
|
|
|
61
|
|
|
Attributes of this class represents options |
62
|
|
|
of given section. when asking for option |
63
|
|
|
value None value is returned if given option |
64
|
|
|
does not exist, otherwise option value is |
65
|
|
|
returned. |
66
|
|
|
""" |
67
|
|
|
def __init__(self, config, name): |
68
|
|
|
self.__dict__["_config"] = config |
69
|
|
|
self.__dict__["section"] = name |
70
|
|
|
|
71
|
|
|
def __getattr__(self, name): |
72
|
|
|
if self._config.has_option(self.section, name): |
73
|
|
|
return self._config.get(self.section, name) |
74
|
|
|
else: |
75
|
|
|
return None |
76
|
|
|
|
77
|
|
|
def __setattr__(self, name, value): |
78
|
|
|
if value is None: |
79
|
|
|
self._config.remove_option(self.section, name) |
80
|
|
|
if not self._config.options(self.section): |
81
|
|
|
# no option -- delete me |
82
|
|
|
self._config.remove_section(self.section) |
83
|
|
|
else: |
84
|
|
|
self._config.set(self.section, name, str(value)) |
85
|
|
|
|