diff options
Diffstat (limited to 'lib/dodai/__init__.py')
-rw-r--r-- | lib/dodai/__init__.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/dodai/__init__.py b/lib/dodai/__init__.py new file mode 100644 index 0000000..55bb57d --- /dev/null +++ b/lib/dodai/__init__.py | |||
@@ -0,0 +1,72 @@ | |||
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 sys | ||
20 | from dodai.config import Config | ||
21 | from dodai.tools import config_directories | ||
22 | |||
23 | class Configure(Config): | ||
24 | |||
25 | CONFIG_FILES = ['config', 'configs', 'configure', 'connect', | ||
26 | 'connections', 'connection', 'setup'] | ||
27 | CONFIG_EXTENSIONS = ['cfg', 'txt', 'ini'] | ||
28 | |||
29 | |||
30 | def __init__(self, project_name, config_files=None): | ||
31 | self.project_name = project_name | ||
32 | self._load_config_files() | ||
33 | self._add_files(config_files) | ||
34 | self._add_config_files_to_database_handler() | ||
35 | |||
36 | def _config_files(self): | ||
37 | # Returns a list of possible config file names | ||
38 | out = [] | ||
39 | for name in self.CONFIG_FILES: | ||
40 | out.append(name) | ||
41 | for ext in self.CONFIG_EXTENSIONS: | ||
42 | name = "{0}.{1}".format(name, ext) | ||
43 | out.append(name) | ||
44 | return out | ||
45 | |||
46 | def _load_config_files(self): | ||
47 | # Adds any default config file if it exists | ||
48 | out = [] | ||
49 | directories = config_directories(self.project_name) | ||
50 | for dir in directories: | ||
51 | if os.path.isdir(dir): | ||
52 | for name in self._config_files(): | ||
53 | path = os.path.join(dir, name) | ||
54 | self._add_file(path) | ||
55 | |||
56 | def _add_files(self, filenames): | ||
57 | # Adds a list or tuple of filenames | ||
58 | if filenames: | ||
59 | if isinstance(filenames, list) or isinstance(filenames, tuple): | ||
60 | for filename in filenames: | ||
61 | self._add_file(filename) | ||
62 | else: | ||
63 | self._add_file(filenames) | ||
64 | |||
65 | def _add_file(self, filename): | ||
66 | # Adds the given filename to the dodai.config.files object | ||
67 | if os.path.isfile(filename): | ||
68 | self.files.add(filename) | ||
69 | |||
70 | def _add_config_files_to_database_handler(self): | ||
71 | sections = self.files.load() | ||
72 | self.databases.add(sections) | ||