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