aboutsummaryrefslogtreecommitdiff
path: root/dodai/__init__.py
blob: f1e90bc1c34dd4a0b76f59e501d68e04fa7d73d6 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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.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