-
Notifications
You must be signed in to change notification settings - Fork 39
/
env_to_json.py
54 lines (44 loc) · 1.36 KB
/
env_to_json.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
#!/usr/bin/env python
# Converts the old env to a json file
import json
import sys
import csv
_BOOLEANS = {'1': True, 'yes': True, 'true': True, 'on': True,
'0': False, 'no': False, 'false': False, 'off': False}
def cast_boolean(value):
"""
Helper to convert config values to boolean as ConfigParser do.
"""
value = str(value)
if value.lower() not in _BOOLEANS:
return value
return _BOOLEANS[value.lower()]
try:
dotenv = sys.argv[1]
except IndexError as e:
dotenv = '.env'
with open(dotenv, 'r') as f:
content = f.readlines()
newcontent = []
# removes whitespace chars like '\n' at the end of each line
for x in content:
if '#' in x:
y = x.strip().replace("'","\'").replace('#','')
newcontent.append(['/* '+y,"*/"])
elif '=' in x:
y = x.strip().replace("'","\'")
y = y.split('=', 1)
# If value is a csv split it to a list
if ',' in y[1]:
y[1] = list(csv.reader([y[1]]))[0]
else:
y[1] = y[1].strip()
# Try to convert to integer
try:
y[1] = int(y[1])
except:
# Try to convert to boolean
y[1] = cast_boolean(y[1])
newcontent.append(y)
#print('My list:', *newcontent, sep='\n- ')
print(json.dumps(dict(newcontent), indent=4, separators=(',', ': ')))