diff options
Diffstat (limited to 'dodai/__init__.py')
-rw-r--r-- | dodai/__init__.py | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/dodai/__init__.py b/dodai/__init__.py new file mode 100644 index 0000000..f1e90bc --- /dev/null +++ b/dodai/__init__.py | |||
@@ -0,0 +1,100 @@ | |||
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 | |||
19 | |||
20 | import os | ||
21 | import sys | ||
22 | from dodai.config import Config | ||
23 | from dodai.config.db import ConfigDbFile | ||
24 | |||
25 | |||
26 | class Configure(Config): | ||
27 | |||
28 | CONFIG_FILES = ['config', 'configs', 'configure', 'connect', 'connections', | ||
29 | 'connection'] | ||
30 | |||
31 | def __init__(self, project_name, config_files=None): | ||
32 | self.config_files = [] | ||
33 | self.project_name = project_name | ||
34 | self.home_directory = self._home_directory() | ||
35 | self._default_files() | ||
36 | self._add_files(config_files) | ||
37 | self._add_config_files() | ||
38 | |||
39 | def _add_files(self, filenames): | ||
40 | if filenames: | ||
41 | if isinstance(filenames, list) or isinstance(filenames, tuple): | ||
42 | for filename in filenames: | ||
43 | self._add_file(filename) | ||
44 | else: | ||
45 | self._add_file(filenames) | ||
46 | |||
47 | def _add_file(self, filename): | ||
48 | if os.path.isfile(filename): | ||
49 | if not filename in self.config_files: | ||
50 | self.config_files.append(filename) | ||
51 | |||
52 | def _default_files(self): | ||
53 | dirs = [] | ||
54 | dirs.append(self._construct_system_config_directory()) | ||
55 | dirs.append(self._construct_user_config_directory()) | ||
56 | dirs.append(self._construct_project_config_directory()) | ||
57 | files = [] | ||
58 | for dir in dirs: | ||
59 | for name in self.CONFIG_FILES: | ||
60 | files.append(os.path.join(dir, name)) | ||
61 | for filename in files: | ||
62 | self._add_file(filename) | ||
63 | |||
64 | def _home_directory(self): | ||
65 | out = None | ||
66 | try: | ||
67 | from win32com.shell import shellcon, shell | ||
68 | out = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0) | ||
69 | except ImportError: | ||
70 | out = os.path.expanduser("~") | ||
71 | return out | ||
72 | |||
73 | def _construct_system_config_directory(self): | ||
74 | out = os.path.join('etc', self.project_name.lower()) | ||
75 | return out | ||
76 | |||
77 | def _construct_user_config_directory(self): | ||
78 | project_directory = ".{dir}".format(dir=self.project_name.lower()) | ||
79 | out = os.path.join(self.home_directory, project_directory) | ||
80 | return out | ||
81 | |||
82 | def _construct_project_config_directory(self): | ||
83 | dir = os.path.dirname(os.path.abspath(sys.argv[0])) | ||
84 | out = os.path.join(dir, 'config') | ||
85 | return out | ||
86 | |||
87 | def _add_config_files(self): | ||
88 | if self.config_files: | ||
89 | self.files().add_file(self.config_files) | ||
90 | config = self.files().parser() | ||
91 | if self._has_connections(config): | ||
92 | self.dbs().add_config(config) | ||
93 | |||
94 | def _has_connections(self, config): | ||
95 | obj = ConfigDbFile(config) | ||
96 | connections = obj() | ||
97 | if connections: | ||
98 | return True | ||
99 | else: | ||
100 | return False | ||