-
Notifications
You must be signed in to change notification settings - Fork 0
/
getConfigEntry
53 lines (39 loc) · 1.06 KB
/
getConfigEntry
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 python3
import argparse
import json
import sys
"""
Function to get any value from database
Example usage:
`setConfigEntry configuration.network.hostname`
would return the hostname of the network
in BASH use:
NAME=$(getConfigEntry configuration.network.hostname 2>&1)
echo $NAME
"""
parser = argparse.ArgumentParser()
parser.add_argument("key", help="dot (.) separated JSON value to get from Aroio_DB", type=str)
args = parser.parse_args()
databasePath = "/tmp/aroio_db.json"
SEPARATOR = '.'
def load_aroio() -> str:
"""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 accessable, generate Database.")
def get_key_from_json(obj: json):
for i in args.key.split(SEPARATOR):
if i in obj:
obj = obj[i]
else:
return None
return obj
def main():
aroio = load_aroio()
value = get_key_from_json(aroio)
sys.exit(value)
if __name__ == '__main__':
main()