-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdotenv.py
72 lines (58 loc) · 1.98 KB
/
dotenv.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
import re
# import sys
def _parse_envdata(data):
var, value = data.decode().split("=")
return {var.strip(): value.strip()}
def sub_var(env_values, value):
try:
m = re.search(r"\${.*}", value)
try:
if not m:
return eval(value)
except NameError:
return value
var_rep = m.group(0)
var_key = var_rep.replace("${", "").replace("}", "")
value = value.replace(var_rep, env_values.get(var_key))
except Exception as e:
raise e
try:
return eval(value)
except Exception:
return value
def replace_vars(env_values):
return {k: sub_var(env_values, v) for k, v in env_values.items()}
def dotenv_values(env, debug=False):
env_values = {}
try:
with open(env, "rb") as _env:
if debug:
print(f"DOTENV: {env}")
for line in _env:
if not line.startswith(b"#") and b"=" in line:
if debug:
print(line.decode().strip())
env_values.update(**_parse_envdata(line))
except Exception:
# sys.print_exception(e)
print(f"WARNING: configuration '{env}' file not found or corrupted")
return replace_vars(env_values)
def set_env_values(env, kvals, debug=False):
with open(env, "rb+") as envf:
_env = envf.read().decode()
for k, v in kvals.items():
if k in _env: # substitute
start = _env.index(k)
stop = start + _env[start:].index("\n")
entry = _env[start:stop]
_env = _env.replace(entry, f"{k}={v}")
if debug:
print(f"replacing: {entry} by {k}={v}")
else: # append
_env += f"{k}={v}\n"
if debug:
print(f"adding: {k}={v}")
with open(env, "wb") as envf:
envf.write(_env.encode())
if __name__ == "__main__":
print(dotenv_values(".env"))