diff options
Diffstat (limited to 'dodai/config/sections.py')
-rw-r--r-- | dodai/config/sections.py | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/dodai/config/sections.py b/dodai/config/sections.py index feb59be..69602fb 100644 --- a/dodai/config/sections.py +++ b/dodai/config/sections.py | |||
@@ -36,13 +36,16 @@ class ConfigSections(object): | |||
36 | custom object. A good object to use is the | 36 | custom object. A good object to use is the |
37 | dodai.tools.himo Himo object. If the | 37 | dodai.tools.himo Himo object. If the |
38 | string_object is not given the default python | 38 | string_object is not given the default python |
39 | str() object will be used. | 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 | |||
40 | 43 | ||
41 | """ | 44 | """ |
42 | self._string_object = string_object or None | 45 | self._string_object = string_object or None |
43 | self._sections = {} | 46 | self._sections = {} |
44 | 47 | ||
45 | def __call__(self, parser): | 48 | def __call__(self, parser, encoding=None): |
46 | """ | 49 | """ |
47 | Parses the given parser object into this object's sections. | 50 | Parses the given parser object into this object's sections. |
48 | 51 | ||
@@ -54,37 +57,38 @@ class ConfigSections(object): | |||
54 | your own | 57 | your own |
55 | 58 | ||
56 | """ | 59 | """ |
57 | self._build_sections(parser) | 60 | self._build_sections(parser, encoding) |
58 | 61 | ||
59 | def _build_sections(self, parser): | 62 | def _build_sections(self, parser, encoding): |
60 | # Builds ConfigSection objects from the parser object | 63 | # Builds ConfigSection objects from the parser object |
61 | 64 | ||
62 | for section_name in parser.sections(): | 65 | for section_name in parser.sections(): |
63 | section = self.get_section(section_name) | 66 | section = self.get_section(section_name, encoding) |
64 | self._build_options(parser, section_name, section) | 67 | self._build_options(parser, section_name, section, encoding) |
65 | 68 | ||
66 | def _build_options(self, parser, section_name, section): | 69 | def _build_options(self, parser, section_name, section, encoding): |
67 | # Adds the options to the section object | 70 | # Adds the options to the section object |
68 | 71 | ||
69 | for key in parser.options(section_name): | 72 | for key in parser.options(section_name): |
70 | key = unicode(self._build_string_object(key)) | 73 | key = self._build_string_object(key, encoding) |
71 | value = self._build_string_object(parser.get(section_name, key)) | 74 | value = self._build_string_object(parser.get(section_name, key), |
75 | encoding) | ||
72 | setattr(section, key, value) | 76 | setattr(section, key, value) |
73 | 77 | ||
74 | def _build_string_object(self, data): | 78 | def _build_string_object(self, data, encoding=None): |
75 | if self._string_object: | 79 | if self._string_object: |
76 | return self._string_object(data) | 80 | return self._string_object(data, encoding) |
77 | else: | 81 | else: |
78 | return data | 82 | return data |
79 | 83 | ||
80 | def get_section(self, section_name): | 84 | def get_section(self, section_name, encoding=None): |
81 | """ | 85 | """ |
82 | Returns a ConfigSection object from this object's section | 86 | Returns a ConfigSection object from this object's section |
83 | dictionary or creates a new ConfigSection object, which is | 87 | dictionary or creates a new ConfigSection object, which is |
84 | stored int this object's section dictionary then is returned | 88 | stored int this object's section dictionary then is returned |
85 | 89 | ||
86 | """ | 90 | """ |
87 | section_name = unicode(self._build_string_object(section_name)) | 91 | section_name = self._build_string_object(section_name, encoding) |
88 | if section_name in self._sections: | 92 | if section_name in self._sections: |
89 | return self._sections[section_name] | 93 | return self._sections[section_name] |
90 | else: | 94 | else: |