# 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.tools import config_directories class Configure(Config): CONFIG_FILES = ['config', 'configs', 'configure', 'connect', 'connections', 'connection', 'setup'] CONFIG_EXTENSIONS = ['cfg', 'txt', 'ini'] def __init__(self, project_name, config_files=None): self.project_name = project_name self._load_config_files() self._add_files(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: self._add_file(filename) else: self._add_file(filenames) def _add_file(self, filename): # Adds the given filename to the dodai.config.files object if os.path.isfile(filename): self.files.add(filename) def _add_config_files_to_database_handler(self): sections = self.files.load() self.databases.add(sections)