aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSix <unknown>2010-04-10 22:54:38 -0400
committerSix <unknown>2010-04-10 22:54:38 -0400
commitf0f3489540586f0dd173b7e21c18d58d0cff306d (patch)
tree138010775f1b115bb386d3815b003abf04f48f04
parentc5003706aca7bf32031e848ef57146362bf7e3de (diff)
downloaddodai-macsupport-f0f3489540586f0dd173b7e21c18d58d0cff306d.tar.bz2
dodai-macsupport-f0f3489540586f0dd173b7e21c18d58d0cff306d.tar.xz
dodai-macsupport-f0f3489540586f0dd173b7e21c18d58d0cff306d.zip
added the ability to set the encoding string to the sections object
-rw-r--r--dodai/config/sections.py30
-rw-r--r--test/test_config/config.cfg4
-rw-r--r--test/test_config/test_sections.py6
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):
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:
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
65[test_db_six] 65[test_db_six]
66protocol=sqlite 66protocol=sqlite
67filename=/tmp/test 67filename=/tmp/test
68
69
70[extra]
71name = \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
22path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')) 22path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))
23sys.path.append(path) 23sys.path.append(path)
24from dodai.tools.himo import Himo 24from dodai.tools.himo import Himo
25from dodai.tools.himo import String2Himo
25from dodai.config.sections import ConfigSections 26from dodai.config.sections import ConfigSections
26 27
27 28
@@ -32,7 +33,7 @@ class TestSections(unittest.TestCase):
32 filepath = os.path.join(path, 'config.cfg') 33 filepath = os.path.join(path, 'config.cfg')
33 self.parser = ConfigParser.ConfigParser() 34 self.parser = ConfigParser.ConfigParser()
34 self.parser.readfp(open(filepath)) 35 self.parser.readfp(open(filepath))
35 self.sections = ConfigSections(Himo) 36 self.sections = ConfigSections(String2Himo())
36 37
37 def test_call(self): 38 def test_call(self):
38 self.sections(self.parser) 39 self.sections(self.parser)
@@ -108,6 +109,9 @@ class TestSections(unittest.TestCase):
108 val = self.sections.test_db['foo'] 109 val = self.sections.test_db['foo']
109 self.assertTrue(val == 'bar') 110 self.assertTrue(val == 'bar')
110 111
112 def test_section_object_seven(self):
113 self.sections(self.parser, 'unicode_escape')
114 self.assertEqual(self.sections.extra.name, u'\u8c61')
111 115
112 116
113if __name__ == '__main__': 117if __name__ == '__main__':