-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_env.py
61 lines (42 loc) · 1.27 KB
/
db_env.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
import os
import sys
var_host = "DBHOST"
file_host = "config/dbhost"
var_port = "DBPORT"
file_port = "config/dbport"
var_user = "DBUSER"
file_user = "config/dbuser"
var_pass = "DBPASS"
file_pass = "config/dbpassword"
var_schema = "DBSCHEMA"
file_schema = "config/dbschema"
def _get_value(var_name, file_name, default=None):
if var_name in os.environ.keys() and len(os.environ[var_name]) > 0:
return os.environ[var_name].strip()
elif os.path.isfile(file_name):
with open(file_name) as f:
return f.readline().strip()
elif default:
return default
else:
print("Neither ENV['" + var_name + "'] or file " + file_name + " provided")
sys.exit(1)
def get_host():
return _get_value(var_host, file_host, '127.0.0.1')
def get_port():
return _get_value(var_port, file_port, '3306')
def get_user():
return _get_value(var_user, file_user)
def get_pass():
return _get_value(var_pass, file_pass)
def get_schema():
return _get_value(var_schema, file_schema)
def substitute(url):
return (
url
.replace('<host>', get_host())
.replace('<port>', get_port())
.replace('<user>', get_user())
.replace('<password>', get_pass())
.replace('<schema>', get_schema())
)