diff options
Diffstat (limited to 'lib/dodai/config/files.py')
-rw-r--r-- | lib/dodai/config/files.py | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/lib/dodai/config/files.py b/lib/dodai/config/files.py new file mode 100644 index 0000000..a27e4a2 --- /dev/null +++ b/lib/dodai/config/files.py | |||
@@ -0,0 +1,106 @@ | |||
1 | # Copyright (C) 2010 Leonard Thomas | ||
2 | # | ||
3 | # This file is part of Dodai. | ||
4 | # | ||
5 | # Dodai is free software: you can redistribute it and/or modify | ||
6 | # it under the terms of the GNU General Public License as published by | ||
7 | # the Free Software Foundation, either version 3 of the License, or | ||
8 | # (at your option) any later version. | ||
9 | # | ||
10 | # Dodai is distributed in the hope that it will be useful, | ||
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | # GNU General Public License for more details. | ||
14 | # | ||
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/>. | ||
17 | |||
18 | import os | ||
19 | import ConfigParser | ||
20 | from dodai.config.sections import ConfigSections | ||
21 | from dodai.exception import InvalidConfigParser | ||
22 | from dodai.exception import FileDoesNotExist | ||
23 | from dodai.exception import FileIsDirectory | ||
24 | |||
25 | class ConfigFiles(object): | ||
26 | |||
27 | REQUIRED_METHODS = ['read', 'sections', 'options', 'get'] | ||
28 | |||
29 | def __init__(self, ordered_dict_object, section_object, string_object): | ||
30 | |||
31 | self._ordered_dict_object = ordered_dict_object | ||
32 | self._section_object = section_object | ||
33 | self._string_object = string_object | ||
34 | self.parser_objects = [] | ||
35 | self.files = self._ordered_dict_object() | ||
36 | self.files_read = [] | ||
37 | |||
38 | def register_parser_object(self, parser_object): | ||
39 | """Registers a config file parser with this object. Raises | ||
40 | InvalidConfigParser error if the parser does not have the 'read', | ||
41 | 'sections', 'options' and 'get' methods. | ||
42 | |||
43 | """ | ||
44 | if self.check_parser_object(parser_object): | ||
45 | self.parser_objects.append(parser_object) | ||
46 | |||
47 | def check_parser_object(self, parser_object): | ||
48 | """Checks the given parser object to insure it has all of the required | ||
49 | methods needed to parse files. | ||
50 | |||
51 | """ | ||
52 | for name in self.REQUIRED_METHODS: | ||
53 | if not hasattr(parser_object, name): | ||
54 | raise InvalidConfigParser(self.REQUIRED_METHODS, | ||
55 | parser_object.__name__) | ||
56 | return True | ||
57 | |||
58 | def add(self, filename, encoding=None): | ||
59 | """Adds the given filename to this object to be parsed. | ||
60 | |||
61 | """ | ||
62 | if os.path.exists(filename): | ||
63 | if os.path.isdir(filename): | ||
64 | raise FileIsDirectory(filename) | ||
65 | else: | ||
66 | filename = os.path.realpath(filename) | ||
67 | self.files[filename] = encoding | ||
68 | else: | ||
69 | raise FileDoesNotExist(filename) | ||
70 | |||
71 | def _read(self): | ||
72 | self.files_read = [] | ||
73 | out = self._ordered_dict_object() | ||
74 | for filename, encoding in self.files.items(): | ||
75 | error = False | ||
76 | for parser_obj in self.parser_objects: | ||
77 | try: | ||
78 | parser = parser_obj() | ||
79 | parser.read(filename) | ||
80 | except Exception, e: | ||
81 | error = True | ||
82 | else: | ||
83 | out[filename] = self._ordered_dict_object() | ||
84 | out[filename]['encoding'] = encoding | ||
85 | out[filename]['parser'] = parser | ||
86 | self.files_read.append(filename) | ||
87 | error = False | ||
88 | break | ||
89 | if error: | ||
90 | raise e | ||
91 | return out | ||
92 | |||
93 | |||
94 | def load(self): | ||
95 | """Returns a ConfigSections object, which acts like a dictionary, | ||
96 | that contains all parsed data. | ||
97 | |||
98 | """ | ||
99 | files = self._read() | ||
100 | sections = ConfigSections(self._ordered_dict_object, | ||
101 | self._section_object, self._string_object) | ||
102 | for filename, data in files.items(): | ||
103 | encoding = data['encoding'] | ||
104 | parser = data['parser'] | ||
105 | sections(parser, encoding) | ||
106 | return sections | ||