aboutsummaryrefslogtreecommitdiff
path: root/lib/dodai/config/sections.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dodai/config/sections.py')
-rw-r--r--lib/dodai/config/sections.py117
1 files changed, 117 insertions, 0 deletions
diff --git a/lib/dodai/config/sections.py b/lib/dodai/config/sections.py
new file mode 100644
index 0000000..d789a24
--- /dev/null
+++ b/lib/dodai/config/sections.py
@@ -0,0 +1,117 @@
1# Copyright (C) 2010 Leonard Thomas
2#
3# This file is part of Dodai.
4#
5# Dodai is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# Dodai is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with Dodai. If not, see <http://www.gnu.org/licenses/>.
17
18
19class ConfigSections(object):
20 """
21 An iterable object that contains ConfigSection objects
22
23 """
24
25 def __init__(self, ordered_dict_object, section_object,
26 string_object = None,):
27 """
28 Iterable object that handles the conversion of a config
29 parser object to a list of section objects.
30
31
32 string_object: This is an object (non instantiated or
33 callable) that the results of the config's
34 sections, and options will be stored in.
35 This enables you to store your values as a
36 custom object. A good object to use is the
37 dodai.tools.himo Himo object. If the
38 string_object is not given the default python
39 str() object will be used. The callable of
40 this object must also allow for encoding
41 to be passed in string_object(data, 'UTF-8')
42
43
44 """
45 self._string_object = string_object or ''
46 self._ordered_dict_object = ordered_dict_object
47 self._section_object = section_object
48 self._sections = self._ordered_dict_object()
49
50 def __call__(self, parser, encoding=None):
51 """
52 Parses the given parser object into this object's sections.
53
54 parser: The actual parser object that is used to
55 get the sections. This object must have
56 the sections(), options() and get()
57 methods. A good object to use is the native
58 ConfigParse object. However, you can create
59 your own
60
61 """
62 self._build_sections(parser, encoding)
63
64 def _build_sections(self, parser, encoding):
65 # Builds ConfigSection objects from the parser object
66
67 for section_name in parser.sections():
68 section = self.get_section(section_name, encoding)
69 self._build_options(parser, section_name, section, encoding)
70
71 def _build_options(self, parser, section_name, section, encoding):
72 # Adds the options to the section object
73
74 for key in parser.options(section_name):
75 value = self._build_string_object(parser.get(section_name, key),
76 encoding)
77 key = self._build_string_object(key, encoding)
78 section[key] = value
79
80 def _build_string_object(self, data, encoding=None):
81 if self._string_object:
82 return self._string_object(data, encoding)
83 else:
84 return data
85
86 def get_section(self, section_name, encoding=None):
87 """
88 Returns a Section (aka dict) object from this object's section
89 dictionary or creates a new ConfigSection object, which is
90 stored int this object's section dictionary then is returned
91
92 """
93 section_name = self._build_string_object(section_name, encoding)
94 if section_name in self._sections:
95 return self._sections[section_name]
96 else:
97 section = self._section_object(section_name)
98 self._sections[section_name] = section
99 return section
100
101 def __getitem__(self, key):
102 return self._sections[key]
103
104 def __getattr__(self, key):
105 try:
106 out = self._sections[key]
107 except KeyError:
108 return getattr(self._sections, key)
109 else:
110 return out
111
112 def __iter__(self, *args, **kargs):
113 return self._sections.__iter__(*args, **kargs)
114
115
116 def __len__(self):
117 return len(self._sections)