From f0f3489540586f0dd173b7e21c18d58d0cff306d Mon Sep 17 00:00:00 2001 From: Six Date: Sat, 10 Apr 2010 22:54:38 -0400 Subject: added the ability to set the encoding string to the sections object --- dodai/config/sections.py | 30 +++++++++++++++++------------- test/test_config/config.cfg | 4 ++++ test/test_config/test_sections.py | 6 +++++- 3 files changed, 26 insertions(+), 14 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): custom object. A good object to use is the dodai.tools.himo Himo object. If the string_object is not given the default python - str() object will be used. + str() object will be used. The callable of + this object must also allow for encoding + to be passed in string_object(data, 'UTF-8') + """ self._string_object = string_object or None self._sections = {} - def __call__(self, parser): + def __call__(self, parser, encoding=None): """ Parses the given parser object into this object's sections. @@ -54,37 +57,38 @@ class ConfigSections(object): your own """ - self._build_sections(parser) + self._build_sections(parser, encoding) - def _build_sections(self, parser): + def _build_sections(self, parser, encoding): # Builds ConfigSection objects from the parser object for section_name in parser.sections(): - section = self.get_section(section_name) - self._build_options(parser, section_name, section) + section = self.get_section(section_name, encoding) + self._build_options(parser, section_name, section, encoding) - def _build_options(self, parser, section_name, section): + def _build_options(self, parser, section_name, section, encoding): # Adds the options to the section object for key in parser.options(section_name): - key = unicode(self._build_string_object(key)) - value = self._build_string_object(parser.get(section_name, key)) + key = self._build_string_object(key, encoding) + value = self._build_string_object(parser.get(section_name, key), + encoding) setattr(section, key, value) - def _build_string_object(self, data): + def _build_string_object(self, data, encoding=None): if self._string_object: - return self._string_object(data) + return self._string_object(data, encoding) else: return data - def get_section(self, section_name): + def get_section(self, section_name, encoding=None): """ Returns a ConfigSection object from this object's section dictionary or creates a new ConfigSection object, which is stored int this object's section dictionary then is returned """ - section_name = unicode(self._build_string_object(section_name)) + section_name = self._build_string_object(section_name, encoding) if section_name in self._sections: return self._sections[section_name] else: diff --git a/test/test_config/config.cfg b/test/test_config/config.cfg index 14764ae..3480016 100644 --- a/test/test_config/config.cfg +++ b/test/test_config/config.cfg @@ -65,3 +65,7 @@ database=testing [test_db_six] protocol=sqlite filename=/tmp/test + + +[extra] +name = \u8c61 diff --git a/test/test_config/test_sections.py b/test/test_config/test_sections.py index 2aba3c7..39b41a7 100644 --- a/test/test_config/test_sections.py +++ b/test/test_config/test_sections.py @@ -22,6 +22,7 @@ import unittest path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')) sys.path.append(path) from dodai.tools.himo import Himo +from dodai.tools.himo import String2Himo from dodai.config.sections import ConfigSections @@ -32,7 +33,7 @@ class TestSections(unittest.TestCase): filepath = os.path.join(path, 'config.cfg') self.parser = ConfigParser.ConfigParser() self.parser.readfp(open(filepath)) - self.sections = ConfigSections(Himo) + self.sections = ConfigSections(String2Himo()) def test_call(self): self.sections(self.parser) @@ -108,6 +109,9 @@ class TestSections(unittest.TestCase): val = self.sections.test_db['foo'] self.assertTrue(val == 'bar') + def test_section_object_seven(self): + self.sections(self.parser, 'unicode_escape') + self.assertEqual(self.sections.extra.name, u'\u8c61') if __name__ == '__main__': -- cgit v1.2.3