/
config.py
93 lines (73 loc) · 3.37 KB
/
config.py
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
import codecs
import os
import sys
import re
class Config(object):
DB_VERSION_TABLE = "__db_version__"
def __init__(self):
self._config = {}
def __repr__(self):
return str(self._config)
def get(self, config_key):
try:
return self._config[config_key]
except KeyError:
raise Exception("invalid configuration key ('%s')" % config_key)
def put(self, config_key, config_value):
if config_key in self._config:
raise Exception("the configuration key '%s' already exists and you cannot override any configuration" % config_key)
self._config[config_key] = config_value
def _parse_migrations_dir(self, dirs, config_dir=''):
abs_dirs = []
for dir in dirs.split(':'):
if config_dir == '':
abs_dirs.append(os.path.abspath(dir))
else:
abs_dirs.append(os.path.abspath('%s/%s' % (config_dir, dir)))
return abs_dirs
class FileConfig(Config):
class SettingsFile(object):
@staticmethod
def import_file(full_filename):
path, filename = os.path.split(full_filename)
# add settings dir from path
sys.path.insert(0, path)
# read config file
try:
execfile(full_filename)
except IOError:
raise Exception("%s: file not found" % full_filename)
except Exception, e:
raise Exception("error interpreting config file '%s': %s" % (filename, str(e)))
# remove settings dir from path
sys.path.remove(path)
return locals()
def __init__(self, config_file="simple-db-migrate.conf"):
self._config = {}
self.put("db_version_table", self.DB_VERSION_TABLE)
# read configuration
settings = FileConfig.SettingsFile.import_file(config_file)
self.put("db_host", self.get_variable(settings, 'DATABASE_HOST', 'HOST'))
self.put("db_user", self.get_variable(settings, 'DATABASE_USER', 'USERNAME'))
self.put("db_password", self.get_variable(settings, 'DATABASE_PASSWORD', 'PASSWORD'))
self.put("db_name", self.get_variable(settings, 'DATABASE_NAME', 'DATABASE'))
migrations_dir = self.get_variable(settings, 'DATABASE_MIGRATIONS_DIR', 'MIGRATIONS_DIR')
config_dir = os.path.split(config_file)[0]
self.put("migrations_dir", self._parse_migrations_dir(migrations_dir, config_dir))
def get_variable(self, settings, name, old_name):
if name in settings or old_name in settings:
return settings.get(name, settings.get(old_name))
else:
raise NameError("config file error: name '%s' is not defined" % name)
class InPlaceConfig(Config):
def __init__(self, db_host, db_user, db_password, db_name, migrations_dir, db_version_table=''):
if not db_version_table or db_version_table == '':
db_version_table = self.DB_VERSION_TABLE
self._config = {
"db_host": db_host,
"db_user": db_user,
"db_password": db_password,
"db_name": db_name,
"db_version_table": db_version_table,
"migrations_dir": self._parse_migrations_dir(migrations_dir)
}