diff options
Diffstat (limited to 'dodai/config/sections.py')
-rw-r--r-- | dodai/config/sections.py | 78 |
1 files changed, 10 insertions, 68 deletions
diff --git a/dodai/config/sections.py b/dodai/config/sections.py index 2b08587..d789a24 100644 --- a/dodai/config/sections.py +++ b/dodai/config/sections.py | |||
@@ -15,7 +15,6 @@ | |||
15 | # You should have received a copy of the GNU General Public License | 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/>. | 16 | # along with Dodai. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | import unicodedata | ||
19 | 18 | ||
20 | class ConfigSections(object): | 19 | class ConfigSections(object): |
21 | """ | 20 | """ |
@@ -23,7 +22,8 @@ class ConfigSections(object): | |||
23 | 22 | ||
24 | """ | 23 | """ |
25 | 24 | ||
26 | def __init__(self, string_object = None): | 25 | def __init__(self, ordered_dict_object, section_object, |
26 | string_object = None,): | ||
27 | """ | 27 | """ |
28 | Iterable object that handles the conversion of a config | 28 | Iterable object that handles the conversion of a config |
29 | parser object to a list of section objects. | 29 | parser object to a list of section objects. |
@@ -42,8 +42,10 @@ class ConfigSections(object): | |||
42 | 42 | ||
43 | 43 | ||
44 | """ | 44 | """ |
45 | self._string_object = string_object or None | 45 | self._string_object = string_object or '' |
46 | self._sections = {} | 46 | self._ordered_dict_object = ordered_dict_object |
47 | self._section_object = section_object | ||
48 | self._sections = self._ordered_dict_object() | ||
47 | 49 | ||
48 | def __call__(self, parser, encoding=None): | 50 | def __call__(self, parser, encoding=None): |
49 | """ | 51 | """ |
@@ -70,10 +72,10 @@ class ConfigSections(object): | |||
70 | # Adds the options to the section object | 72 | # Adds the options to the section object |
71 | 73 | ||
72 | for key in parser.options(section_name): | 74 | for key in parser.options(section_name): |
73 | key = self._build_string_object(key, encoding) | ||
74 | value = self._build_string_object(parser.get(section_name, key), | 75 | value = self._build_string_object(parser.get(section_name, key), |
75 | encoding) | 76 | encoding) |
76 | setattr(section, key, value) | 77 | key = self._build_string_object(key, encoding) |
78 | section[key] = value | ||
77 | 79 | ||
78 | def _build_string_object(self, data, encoding=None): | 80 | def _build_string_object(self, data, encoding=None): |
79 | if self._string_object: | 81 | if self._string_object: |
@@ -83,7 +85,7 @@ class ConfigSections(object): | |||
83 | 85 | ||
84 | def get_section(self, section_name, encoding=None): | 86 | def get_section(self, section_name, encoding=None): |
85 | """ | 87 | """ |
86 | Returns a ConfigSection object from this object's section | 88 | Returns a Section (aka dict) object from this object's section |
87 | dictionary or creates a new ConfigSection object, which is | 89 | dictionary or creates a new ConfigSection object, which is |
88 | stored int this object's section dictionary then is returned | 90 | stored int this object's section dictionary then is returned |
89 | 91 | ||
@@ -92,16 +94,14 @@ class ConfigSections(object): | |||
92 | if section_name in self._sections: | 94 | if section_name in self._sections: |
93 | return self._sections[section_name] | 95 | return self._sections[section_name] |
94 | else: | 96 | else: |
95 | section = ConfigSection(section_name) | 97 | section = self._section_object(section_name) |
96 | self._sections[section_name] = section | 98 | self._sections[section_name] = section |
97 | return section | 99 | return section |
98 | 100 | ||
99 | def __getitem__(self, key): | 101 | def __getitem__(self, key): |
100 | key = normalize_key(key) | ||
101 | return self._sections[key] | 102 | return self._sections[key] |
102 | 103 | ||
103 | def __getattr__(self, key): | 104 | def __getattr__(self, key): |
104 | key = normalize_key(key) | ||
105 | try: | 105 | try: |
106 | out = self._sections[key] | 106 | out = self._sections[key] |
107 | except KeyError: | 107 | except KeyError: |
@@ -115,61 +115,3 @@ class ConfigSections(object): | |||
115 | 115 | ||
116 | def __len__(self): | 116 | def __len__(self): |
117 | return len(self._sections) | 117 | return len(self._sections) |
118 | |||
119 | class ConfigSection(object): | ||
120 | """ | ||
121 | A generic object to hold keys and values primarily from a config file | ||
122 | |||
123 | """ | ||
124 | def __init__(self, title): | ||
125 | """ | ||
126 | Holds keys and values primarily from a section of a config file | ||
127 | |||
128 | title: The title of the section of the config file | ||
129 | |||
130 | """ | ||
131 | self.___title___ = title | ||
132 | self.___options___ = {} | ||
133 | |||
134 | |||
135 | def get_title(self): | ||
136 | """ | ||
137 | Returns the title of the section | ||
138 | |||
139 | """ | ||
140 | return self.___title___ | ||
141 | |||
142 | def __setattr__(self, key, value): | ||
143 | if key.startswith('___') and key.endswith('___'): | ||
144 | object.__setattr__(self, key, value) | ||
145 | else: | ||
146 | key = normalize_key(key) | ||
147 | if self.___options___.has_key(key): | ||
148 | self.___options___[key] = value | ||
149 | else: | ||
150 | dict.__setitem__(self.___options___, key, value) | ||
151 | |||
152 | def __getattr__(self, key): | ||
153 | if key.startswith('___') and key.endswith('___'): | ||
154 | return self.__dict__[key] | ||
155 | else: | ||
156 | key = normalize_key(key) | ||
157 | try: | ||
158 | out = self.___options___[key] | ||
159 | except KeyError: | ||
160 | return getattr(self.___options___, key) | ||
161 | else: | ||
162 | return out | ||
163 | |||
164 | def __getitem__(self, key): | ||
165 | key = normalize_key(key) | ||
166 | return self.___options___[key] | ||
167 | |||
168 | def __iter__(self, *args, **kargs): | ||
169 | return self.___options___.__iter__(*args, **kargs) | ||
170 | |||
171 | |||
172 | def normalize_key(key): | ||
173 | key = unicode(key) | ||
174 | key = unicodedata.normalize('NFC', key) | ||
175 | return key | ||