aboutsummaryrefslogtreecommitdiff
path: root/dodai/__init__.py
blob: 87155db016d3edd7605c504c179e8a75f070b52f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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 <http://www.gnu.org/licenses/>.



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)