-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetConfigEntry
More file actions
79 lines (60 loc) · 1.93 KB
/
setConfigEntry
File metadata and controls
79 lines (60 loc) · 1.93 KB
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
#!/usr/bin/env python3
import argparse
import json
import sys
"""
Function to set any value from database.
Example usage:
`python3 setConfigEntry configuration.network.hostname horst`
would change the hostname of the network to "horst"
in BASH use:
setConfigEntry configuration.network.hostname $NAME
"""
parser = argparse.ArgumentParser()
parser.add_argument("key", help="dot (.) separated JSON value to get from Aroio_DB", type=str)
parser.add_argument("value", help="Value to be set to the given key")
args = parser.parse_args()
databasePath = "/tmp/aroio_db.json"
SEPARATOR = '.'
def save_aroio(aroio: dict):
"""Persist the aroio in json format"""
with open(databasePath, 'r+') as f:
f.write(json.dumps(aroio))
f.truncate()
def load_aroio() -> json:
"""Reads data of Aroio from userconfig"""
try:
with open(databasePath) as f:
aroio_db = json.load(f)
return aroio_db
except IOError:
print("Database not accessible, generate Database.")
def set_config_entry(obj: json, key: str, value) -> int:
"""Sets the value of the key in the given object. Returning the exit code"""
keys = key.split(sep=SEPARATOR)
try:
if len(keys) == 1:
obj[keys[0]] = value
else:
key = keys.pop(0)
set_config_entry(obj=obj[key], key='.'.join(keys), value=value)
except OSError:
print("Could not set value for key", keys[0])
return 1
return 0
def main():
key = args.key
value = args.value
if not key:
raise KeyError("Need to pass a key to be set")
if not value:
raise KeyError("Need to pass a value to set")
aroio = load_aroio()
if not aroio:
raise FileNotFoundError("Aroio was not found")
exit_code = set_config_entry(obj=aroio, key=key, value=value)
if exit_code == 0:
save_aroio(aroio)
sys.exit(exit_code)
if __name__ == '__main__':
main()