forked from smurfix/owslave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg
executable file
·120 lines (109 loc) · 4.42 KB
/
cfg
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/python3
"""
This program is part of MoaT.
Its job is simply to extract values from a YAML-formatted config file.
"""
from Cfg import Cfg
import sys
def main(f,*kk):
s = Cfg(f)
if kk:
mode = ""
for k in kk:
if mode == "":
if k[0] == '.':
mode = k[1:]
else:
did=[]
ks = k.split('.')
res = s.subtree(*ks)
if isinstance(res,dict):
print(" ".join("{}={}".format(a,v) for a,v in s.keyval(*ks)))
elif isinstance(res,(list,tuple)):
print(" ".join(str(x) for x in res))
else:
try:
f = float(res)
except Exception:
print(res)
else:
g = int(f)
if float(g) == f:
print(g)
else:
print(f)
elif mode == "hdr":
with open("device/"+k+"/dev_config.h","w") as f:
print("""\
#ifndef device_{}_config_h
#define device_{}_config_h
/*
* This file is auto-generated. It contains a mix of global and local
* definitions because I am lazy.
*
* Do not edit. Talk to 'moat.cfg' instead.
*/
""".format(k,k), file=f);
i = 0
typecode = {}
for a in s.subtree('codes','types'):
typecode[a]=i
i += 1
i = 0x70
for a in s.subtree('codes','types_x70'):
typecode[a]=i
print("#define TC_{} {}".format(a.upper(),i), file=f)
i += 1
i = 1
print("typedef enum _ConfigID {", file=f);
for a in s.subtree('codes','blocks'):
typecode[a]=i
print(" CfgID_{} = {},".format(a,i), file=f)
i += 1
print("} ConfigID;", file=f);
for a,v in s.keyval('devices',k,'defs'):
print("#define {} {}".format(a.upper(),v), file=f)
max_t = -1
for a,v in s.keyval('devices',k,'types'):
v = int(v)
if v > 0:
print("#define TC_{} {}".format(a.upper(),typecode[a]), file=f)
print("#define N_{} {}".format(a.upper(),v), file=f)
if max_t < typecode[a]:
max_t = typecode[a]
print("""\
#define MAX_TC {}
#endif /* device_{}_config_h */
""".format(max_t,k,k), file=f);
elif mode == "type":
print(" ".join("{} {}".format(a,v) for a,v in s.keyval('devices',k,'types')))
elif mode == "cdefs":
types = dict((a,int(v)) for a,v in s.keyval('devices',k,'types') if int(v) > 0)
maxtype = max(())
print(" ".join("-D{}=\"{}\"".format(a.upper(),str(v).replace('"','\"')) for a,v in s.keyval('devices',k,'defs')) + " " + " ".join("-DN_{}={}".format(a.upper(),v) for a,v in s.keyval('devices',k,'types') if int(v) > 0))
elif mode == "cfiles":
files = ['dev_data.c']
if s.subtree('devices',k,'defs','have_uart'):
files.append('uart.c')
ow = s.subtree('devices',k,'defs','is_onewire')
if not ow: pass
elif ow in ('moat','ds2423','ds2408'):
files.append(ow+'.c')
files.append('onewire.c')
else:
print("Unknown onewire mode:",ow, file=sys.stderr)
sys.exit(2)
if not s.subtree('devices',k,'defs','use_eeprom'):
files.append('config.o')
print(" ".join(files))
else:
print("Unknown mode:",mode, file=sys.stderr)
sys.exit(2)
else:
import pprint
pprint.pprint(s.data)
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: {} file [type] [key…]".format(sys.argv[0]), file=sys.stderr)
sys.exit(2)
main(*sys.argv[1:])