From 9160d125524c278ca03841296c5c9cf7d1860c11 Mon Sep 17 00:00:00 2001 From: Six Date: Wed, 21 Apr 2010 01:05:16 -0400 Subject: changed the Configure class to use the new databases module --- dodai/__init__.py | 92 ++++++++++++++------------------------ dodai/config/databases/__init__.py | 23 +++++----- 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 @@ import os import sys from dodai.config import Config -from dodai.config.db import ConfigDbFile - +from dodai.tools import config_directories class Configure(Config): - CONFIG_FILES = ['config', 'configs', 'configure', 'connect', 'connections', - 'connection'] + CONFIG_FILES = ['config', 'configs', 'configure', 'connect', + 'connections', 'connection', 'setup'] + CONFIG_EXTENSIONS = ['cfg', 'txt', 'ini'] + def __init__(self, project_name, config_files=None): - self.config_files = [] self.project_name = project_name - self.home_directory = self._home_directory() - self._default_files() + self._load_config_files() self._add_files(config_files) - self._add_config_files() + self._add_config_files_to_database_handler() + + def _config_files(self): + # Returns a list of possible config file names + out = [] + for name in self.CONFIG_FILES: + out.append(name) + for ext in self.CONFIG_EXTENSIONS: + name = "{0}.{1}".format(name, ext) + out.append(name) + return out + + def _load_config_files(self): + # Adds any default config file if it exists + out = [] + directories = config_directories(self.project_name) + for dir in directories: + if os.path.isdir(dir): + for name in self._config_files(): + path = os.path.join(dir, name) + self._add_file(path) def _add_files(self, filenames): + # Adds a list or tuple of filenames if filenames: if isinstance(filenames, list) or isinstance(filenames, tuple): for filename in filenames: @@ -45,56 +65,10 @@ class Configure(Config): self._add_file(filenames) def _add_file(self, filename): + # Adds the given filename to the dodai.config.files object if os.path.isfile(filename): - if not filename in self.config_files: - self.config_files.append(filename) - - def _default_files(self): - dirs = [] - dirs.append(self._construct_system_config_directory()) - dirs.append(self._construct_user_config_directory()) - dirs.append(self._construct_project_config_directory()) - files = [] - for dir in dirs: - for name in self.CONFIG_FILES: - files.append(os.path.join(dir, name)) - for filename in files: - self._add_file(filename) - - def _home_directory(self): - out = None - try: - from win32com.shell import shellcon, shell - out = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0) - except ImportError: - out = os.path.expanduser("~") - return out - - def _construct_system_config_directory(self): - out = os.path.join('etc', self.project_name.lower()) - return out - - def _construct_user_config_directory(self): - project_directory = ".{dir}".format(dir=self.project_name.lower()) - out = os.path.join(self.home_directory, project_directory) - return out - - def _construct_project_config_directory(self): - dir = os.path.dirname(os.path.abspath(sys.argv[0])) - out = os.path.join(dir, 'config') - return out - - def _add_config_files(self): - if self.config_files: - self.files().add_file(self.config_files) - config = self.files().parser() - if self._has_connections(config): - self.dbs().add_config(config) + self.files.add(filename) - def _has_connections(self, config): - obj = ConfigDbFile(config) - connections = obj() - if connections: - return True - else: - return False + def _add_config_files_to_database_handler(self): + sections = self.files.load() + self.databases.add(sections) 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): def _build_default_handler(self): # Creates the default sqlalchemy handler if 'sa' not in self._handlers: - from .sa import Sa + from dodai.config.databases.sa import Sa from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine - from ....db import Db + from dodai.db import Db sa = Sa(create_engine, sessionmaker, Db) self._handlers['sa'] = sa def add_handler(self, name, handler): - """Addes the given handler and name to this objects handlers + """Adds the given handler and name to this objects handlers """ self._handlers[name] = obj @@ -120,13 +120,14 @@ class DatabaseConnectionValidator(object): def load(self): if not self.obj: self._validate() - self.obj = self.handler.load(self.section) + handler = self._get_handler() + self.obj = handler(self.name, self.section) return self.obj - def _set_handler(self): + def _get_handler(self): if hasattr(self.section, 'handler'): name = self.section.handler.lower() - if handler in self.handlers.keys(): + if handler in self._handlers.keys(): return self._handlers[name] return self._handlers[self.DEFAULT_HANDLER] @@ -182,14 +183,14 @@ class DatabaseConnectionValidator(object): return False def _database_type(self): - keys = self.section.__dict__.keys() + keys = self.section.___options___.keys() for type_, pool in self.OPTIONS_REQUIRED.items(): out = True for key in keys: if key not in pool: out = False if out: - return type + return type_ return False @@ -321,13 +322,13 @@ class DatabaseConnectionException(Exception): def __init__(self, section_name, options_required): self.section_name = section_name self.options_required = options_required - self.msg = self._build_message + self.msg = self._build_message() def _build_message(self): out = [] out.append(self.MESSAGE.format(section_name=self.section_name)) - for database_type, options in self.options_required: - options = list_to_englis(options) + for database_type, options in self.options_required.items(): + options = list_to_english(options) out.append(self.MSG_TYPE.format(database_type=database_type, options=options)) 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 @@ # You should have received a copy of the GNU General Public License # along with Dodai. If not, see . +import sys +import os +import platform + +def home_directory(): + """Returns the full real path to the home directory of the user who + is executing this script. + + """ + out = None + try: + from win32com.shell import shellcon, shell + out = shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0) + except ImportError: + out = os.path.expanduser("~") + return os.path.realpath(out) + +def config_directory_system(project_name=None): + """Returns the system config directory with the passed in project + appended to the path. Returns None for windows and java systems. + + """ + path = None + if platform.system and platform.system() not in ['Windows', 'Java']: + if project_name: + path = os.path.join('/etc', project_name.strip()) + else: + path = '/etc' + return path + +def config_directory_user(project_name): + """Returns the config direcotry of the project which is located in + the user's home directory. Returns None for Java and unknown systems + + """ + path = None + project = '.{0}'.format(project_name.strip()) + if platform.system and platform.system() not in ['Java']: + home_dir = home_directory() + path = os.path.join(home_dir, project) + return path + +def config_directory_project(): + """Returns the config directory that is located in the same directory + of the executable that ran this script + + """ + path = os.path.dirname(os.path.abspath(sys.argv[0])) + return os.path.join(path, 'config') + + +def config_directories(project_name): + """Returns a list of possible project directories + + """ + dirs = [] + dir = config_directory_system(project_name) + if dir: + dirs.append(dir) + dir = config_directory_user(project_name) + if dir: + dirs.append(dir) + dir = config_directory_project() + if dir: + dirs.append(dir) + return dirs + + -- cgit v1.2.3