diff options
author | Six <unknown> | 2010-04-10 23:14:40 -0400 |
---|---|---|
committer | Six <unknown> | 2010-04-10 23:14:40 -0400 |
commit | 3d0a8b94ce0a2b6c9650a66c5ebe275212533b97 (patch) | |
tree | 99d98858ad80702df38ecea1027297b4218c3258 | |
parent | f0f3489540586f0dd173b7e21c18d58d0cff306d (diff) | |
download | dodai-macsupport-3d0a8b94ce0a2b6c9650a66c5ebe275212533b97.tar.bz2 dodai-macsupport-3d0a8b94ce0a2b6c9650a66c5ebe275212533b97.tar.xz dodai-macsupport-3d0a8b94ce0a2b6c9650a66c5ebe275212533b97.zip |
added encoding options
-rw-r--r-- | dodai/config/files.py | 73 |
1 files changed, 36 insertions, 37 deletions
diff --git a/dodai/config/files.py b/dodai/config/files.py index 57e964c..2401b1e 100644 --- a/dodai/config/files.py +++ b/dodai/config/files.py | |||
@@ -17,7 +17,7 @@ | |||
17 | 17 | ||
18 | import os | 18 | import os |
19 | import ConfigParser | 19 | import ConfigParser |
20 | from dodai.tools.himo import Himo | 20 | from dodai.tools.himo import String2Himo |
21 | from dodai.config.sections import ConfigSections | 21 | from dodai.config.sections import ConfigSections |
22 | 22 | ||
23 | class ConfigFiles(object): | 23 | class ConfigFiles(object): |
@@ -25,51 +25,50 @@ class ConfigFiles(object): | |||
25 | def __init__(self): | 25 | def __init__(self): |
26 | 26 | ||
27 | self._default_parser = ConfigParser.ConfigParser() | 27 | self._default_parser = ConfigParser.ConfigParser() |
28 | self._string_object = Himo | 28 | self._string_object = String2Himo() |
29 | self._files = {} | 29 | self._files = {} |
30 | self._paths = [] | ||
31 | 30 | ||
32 | def set_default_parser(self, parser): | 31 | def set_default_parser(self, parser): |
33 | self.check_config_parser(parser) | 32 | self.check_config_parser(parser) |
34 | self._default_parser = parser | 33 | self._default_parser = parser |
35 | 34 | ||
36 | def set_string_object(self, obj): | 35 | def set_string_object(self, obj): |
37 | self._string_object = obj | 36 | self._string_object = obj |
38 | 37 | ||
39 | def add_file(self, path, parser=None): | 38 | def add_file(self, path, encoding=None, parser=None): |
39 | if parser: | ||
40 | self.check_config_parser(parser) | ||
41 | if os.path.exists(path): | ||
40 | if parser: | 42 | if parser: |
41 | self.check_config_parser(parser) | 43 | self._files[path] = [parser, encoding] |
42 | if os.path.exists(path): | ||
43 | if parser: | ||
44 | self._files[path] = parser | ||
45 | else: | ||
46 | self._paths.append(path) | ||
47 | else: | 44 | else: |
48 | raise FileDoesNotExist(path) | 45 | self._files[path] = [self._default_parser, encoding] |
46 | else: | ||
47 | raise FileDoesNotExist(path) | ||
49 | 48 | ||
50 | def is_valid_config_parser(self, obj): | 49 | def is_valid_config_parser(self, obj): |
51 | out = True | 50 | out = True |
52 | if not hasattr(obj, 'read'): | 51 | if not hasattr(obj, 'read'): |
53 | out = False | 52 | out = False |
54 | return out | 53 | return out |
55 | 54 | ||
56 | def check_config_parser(self, obj): | 55 | def check_config_parser(self, obj): |
57 | if self.is_valid_config_parser(obj): | 56 | if self.is_valid_config_parser(obj): |
58 | return True | 57 | return True |
59 | else: | 58 | else: |
60 | raise InvalidConfigParser("Please make sure your object "\ | 59 | raise InvalidConfigParser("Please make sure your object "\ |
61 | "has the following methods: "\ | 60 | "has the following methods: "\ |
62 | "sections, options, read and get.") | 61 | "sections, options, read and get.") |
63 | 62 | ||
64 | def load(self, sections_object=None): | 63 | def load(self, sections_object=None): |
65 | paths = [] | 64 | paths = [] |
66 | sections = sections_object or ConfigSections(self._string_object) | 65 | sections = sections_object or ConfigSections(self._string_object) |
67 | self._default_parser.read(self._paths) | 66 | for path, data in self._files.items(): |
68 | sections(self._default_parser) | 67 | parser = data[0] |
69 | for path, parser in self._files: | 68 | encoding = data[1] |
70 | parser.read(path) | 69 | parser.read(path) |
71 | sections(parser) | 70 | sections(parser, encoding) |
72 | return sections | 71 | return sections |
73 | 72 | ||
74 | 73 | ||
75 | class InvalidConfigParser(Exception): | 74 | class InvalidConfigParser(Exception): |