# Copyright (C) 2010 Leonard Thomas # # This file is part of Dodai. # # Dodai is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Dodai is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Dodai. If not, see . import os import sys from dodai.config import Config from dodai.config.db import ConfigDbFile class Configure(Config): CONFIG_FILES = ['config', 'configs', 'configure', 'connect', 'connections', 'connection'] 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._add_files(config_files) self._add_config_files() def _add_files(self, filenames): if filenames: if isinstance(filenames, list) or isinstance(filenames, tuple): for filename in filenames: self._add_file(filename) else: self._add_file(filenames) def _add_file(self, filename): 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) def _has_connections(self, config): obj = ConfigDbFile(config) connections = obj() if connections: return True else: return False