diff options
author | Six <unknown> | 2010-04-21 01:05:16 -0400 |
---|---|---|
committer | Six <unknown> | 2010-04-21 01:05:16 -0400 |
commit | 9160d125524c278ca03841296c5c9cf7d1860c11 (patch) | |
tree | ceddf073c5c45490e3c3362bad3f81145f737710 | |
parent | b3123bd288380bea9e0796f187eed7abf0ab95ea (diff) | |
download | dodai-macsupport-9160d125524c278ca03841296c5c9cf7d1860c11.tar.bz2 dodai-macsupport-9160d125524c278ca03841296c5c9cf7d1860c11.tar.xz dodai-macsupport-9160d125524c278ca03841296c5c9cf7d1860c11.zip |
changed the Configure class to use the new databases module
-rw-r--r-- | dodai/__init__.py | 92 | ||||
-rw-r--r-- | dodai/config/databases/__init__.py | 23 | ||||
-rw-r--r-- | dodai/tools/__init__.py | 68 |
3 files changed, 113 insertions, 70 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 | ||
diff --git a/dodai/config/databases/__init__.py b/dodai/config/databases/__init__.py index 459ee1b..da317e8 100644 --- a/dodai/config/databases/__init__.py +++ b/dodai/config/databases/__init__.py | |||
@@ -48,15 +48,15 @@ class ConfigDatabases(object): | |||
48 | def _build_default_handler(self): | 48 | def _build_default_handler(self): |
49 | # Creates the default sqlalchemy handler | 49 | # Creates the default sqlalchemy handler |
50 | if 'sa' not in self._handlers: | 50 | if 'sa' not in self._handlers: |
51 | from .sa import Sa | 51 | from dodai.config.databases.sa import Sa |
52 | from sqlalchemy.orm import sessionmaker | 52 | from sqlalchemy.orm import sessionmaker |
53 | from sqlalchemy import create_engine | 53 | from sqlalchemy import create_engine |
54 | from ....db import Db | 54 | from dodai.db import Db |
55 | sa = Sa(create_engine, sessionmaker, Db) | 55 | sa = Sa(create_engine, sessionmaker, Db) |
56 | self._handlers['sa'] = sa | 56 | self._handlers['sa'] = sa |
57 | 57 | ||
58 | def add_handler(self, name, handler): | 58 | def add_handler(self, name, handler): |
59 | """Addes the given handler and name to this objects handlers | 59 | """Adds the given handler and name to this objects handlers |
60 | 60 | ||
61 | """ | 61 | """ |
62 | self._handlers[name] = obj | 62 | self._handlers[name] = obj |
@@ -120,13 +120,14 @@ class DatabaseConnectionValidator(object): | |||
120 | def load(self): | 120 | def load(self): |
121 | if not self.obj: | 121 | if not self.obj: |
122 | self._validate() | 122 | self._validate() |
123 | self.obj = self.handler.load(self.section) | 123 | handler = self._get_handler() |
124 | self.obj = handler(self.name, self.section) | ||
124 | return self.obj | 125 | return self.obj |
125 | 126 | ||
126 | def _set_handler(self): | 127 | def _get_handler(self): |
127 | if hasattr(self.section, 'handler'): | 128 | if hasattr(self.section, 'handler'): |
128 | name = self.section.handler.lower() | 129 | name = self.section.handler.lower() |
129 | if handler in self.handlers.keys(): | 130 | if handler in self._handlers.keys(): |
130 | return self._handlers[name] | 131 | return self._handlers[name] |
131 | return self._handlers[self.DEFAULT_HANDLER] | 132 | return self._handlers[self.DEFAULT_HANDLER] |
132 | 133 | ||
@@ -182,14 +183,14 @@ class DatabaseConnectionValidator(object): | |||
182 | return False | 183 | return False |
183 | 184 | ||
184 | def _database_type(self): | 185 | def _database_type(self): |
185 | keys = self.section.__dict__.keys() | 186 | keys = self.section.___options___.keys() |
186 | for type_, pool in self.OPTIONS_REQUIRED.items(): | 187 | for type_, pool in self.OPTIONS_REQUIRED.items(): |
187 | out = True | 188 | out = True |
188 | for key in keys: | 189 | for key in keys: |
189 | if key not in pool: | 190 | if key not in pool: |
190 | out = False | 191 | out = False |
191 | if out: | 192 | if out: |
192 | return type | 193 | return type_ |
193 | return False | 194 | return False |
194 | 195 | ||
195 | 196 | ||
@@ -321,13 +322,13 @@ class DatabaseConnectionException(Exception): | |||
321 | def __init__(self, section_name, options_required): | 322 | def __init__(self, section_name, options_required): |
322 | self.section_name = section_name | 323 | self.section_name = section_name |
323 | self.options_required = options_required | 324 | self.options_required = options_required |
324 | self.msg = self._build_message | 325 | self.msg = self._build_message() |
325 | 326 | ||
326 | def _build_message(self): | 327 | def _build_message(self): |
327 | out = [] | 328 | out = [] |
328 | out.append(self.MESSAGE.format(section_name=self.section_name)) | 329 | out.append(self.MESSAGE.format(section_name=self.section_name)) |
329 | for database_type, options in self.options_required: | 330 | for database_type, options in self.options_required.items(): |
330 | options = list_to_englis(options) | 331 | options = list_to_english(options) |
331 | out.append(self.MSG_TYPE.format(database_type=database_type, | 332 | out.append(self.MSG_TYPE.format(database_type=database_type, |
332 | options=options)) | 333 | options=options)) |
333 | out.append(self.MSG_END) | 334 | out.append(self.MSG_END) |
diff --git a/dodai/tools/__init__.py b/dodai/tools/__init__.py index 9d2fad7..ab90cfe 100644 --- a/dodai/tools/__init__.py +++ b/dodai/tools/__init__.py | |||
@@ -15,3 +15,71 @@ | |||
15 | # You should have received a copy of the GNU General Public License | 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/>. | 16 | # along with Dodai. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | import sys | ||
19 | import os | ||
20 | import platform | ||
21 | |||
22 | def home_directory(): | ||
23 | """Returns the full real path to the home directory of the user who | ||
24 | is executing this script. | ||
25 | |||
26 | """ | ||
27 | out = None | ||
28 | try: | ||
29 | from win32com.shell import shellcon, shell | ||
30 | out = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0) | ||
31 | except ImportError: | ||
32 | out = os.path.expanduser("~") | ||
33 | return os.path.realpath(out) | ||
34 | |||
35 | def config_directory_system(project_name=None): | ||
36 | """Returns the system config directory with the passed in project | ||
37 | appended to the path. Returns None for windows and java systems. | ||
38 | |||
39 | """ | ||
40 | path = None | ||
41 | if platform.system and platform.system() not in ['Windows', 'Java']: | ||
42 | if project_name: | ||
43 | path = os.path.join('/etc', project_name.strip()) | ||
44 | else: | ||
45 | path = '/etc' | ||
46 | return path | ||
47 | |||
48 | def config_directory_user(project_name): | ||
49 | """Returns the config direcotry of the project which is located in | ||
50 | the user's home directory. Returns None for Java and unknown systems | ||
51 | |||
52 | """ | ||
53 | path = None | ||
54 | project = '.{0}'.format(project_name.strip()) | ||
55 | if platform.system and platform.system() not in ['Java']: | ||
56 | home_dir = home_directory() | ||
57 | path = os.path.join(home_dir, project) | ||
58 | return path | ||
59 | |||
60 | def config_directory_project(): | ||
61 | """Returns the config directory that is located in the same directory | ||
62 | of the executable that ran this script | ||
63 | |||
64 | """ | ||
65 | path = os.path.dirname(os.path.abspath(sys.argv[0])) | ||
66 | return os.path.join(path, 'config') | ||
67 | |||
68 | |||
69 | def config_directories(project_name): | ||
70 | """Returns a list of possible project directories | ||
71 | |||
72 | """ | ||
73 | dirs = [] | ||
74 | dir = config_directory_system(project_name) | ||
75 | if dir: | ||
76 | dirs.append(dir) | ||
77 | dir = config_directory_user(project_name) | ||
78 | if dir: | ||
79 | dirs.append(dir) | ||
80 | dir = config_directory_project() | ||
81 | if dir: | ||
82 | dirs.append(dir) | ||
83 | return dirs | ||
84 | |||
85 | |||