-
Notifications
You must be signed in to change notification settings - Fork 20
/
cfg_write
executable file
·47 lines (40 loc) · 1.19 KB
/
cfg_write
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
#!/usr/bin/env python3
"""
This program is part of MoaT.
Its job is simply to extract values from a YAML-formatted config file.
"""
import sys
import yaml
from traceback import print_exc
from ordered_yaml import load,dump
def usage():
print("Usage: {} <file> <some.key value>…".format(sys.argv[0]), file=sys.stderr)
sys.exit(2)
if len(sys.argv) < 2: usage()
f,*kk = sys.argv[1:]
if not kk or len(kk)%1: usage()
s = load(open(sys.argv[1]))
while kk:
v = s
ks = kk.pop(0).split('.')
lk = ks.pop()
for k in ks:
if not isinstance(v,dict):
raise RuntimeError("Not a dict at {}".format(".".join(ks)))
if k in v:
v = v[k]
else:
v[k] = {}
v = v[k]
if not isinstance(v,dict):
raise RuntimeError("Not a dict at {}.{}".format(".".join(ks),lk))
if not isinstance(v.get(lk,""),(str,int,float)):
raise RuntimeError("Not a scalar value at {}.{}".format(".".join(ks),lk))
val = kk.pop(0)
try: val = float(val)
except ValueError:
try: val = int(val)
except ValueError: pass
else: val = str(val)
v[lk] = val
dump(s, stream=open(sys.argv[1],'w'), default_flow_style=False)