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