-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdate_config.py
executable file
·62 lines (54 loc) · 1.76 KB
/
update_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
#!/usr/bin/python3
# Updates the configuration in the json to the database
# Can run without argument for using standard file
# Or specify the file by passing it as a argument
import json
import sqlite3
import sys
if len(sys.argv) > 1:
if str(sys.argv[1]) == 'ASPire':
filename = 'config_ASPire.json'
elif str(sys.argv[1]) == 'Janet':
filename = 'config_Janet.json'
else :
filename = str(sys.argv[1])
else:
filename = 'config_ASPire.json'
print(filename)
try:
cfg = json.load(open(filename))
except FileNotFoundError:
sys.exit('Error to open the file.\nPlease enter in argument either \'ASPire\', \'Janet\' or the filepath.')
conn = sqlite3.connect('asr.db')
db = conn.cursor()
for table in cfg:
data = cfg[table]
setstr = ''
keystr = ''
valstr = ''
for key, value in cfg[table].items():
if isinstance(value, str):
value = '"' + value + '"'
else:
value = str(value)
if (setstr == ''):
setstr = key + ' = ' + value
keystr = key
valstr = value
else:
setstr = setstr + ', ' + key + ' = ' + value
keystr = keystr + ', ' + key
valstr = valstr + ', ' + value
try:
db.execute('SELECT count(*) FROM ' + str(table) + ';')
except sqlite3.OperationalError:
sys.exit('Error to retrieve the tables.\nCheck if the selected file \''+filename+'\' correspond to the current Database configuration')
count = db.fetchone()[0]
if count == 0:
db.execute('INSERT INTO ' + str(table) + ' (' + keystr +
') VALUES (' + valstr + ');')
else:
db.execute('UPDATE ' + str(table) + ' SET ' +
setstr + ' WHERE ID = 1;')
conn.commit()
db.close()