-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotos.py
executable file
·82 lines (61 loc) · 2.1 KB
/
protos.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
import inspect
from collections import OrderedDict
cformats = { 'B' : 'unsigned char',
'H' : 'unsigned int',
'I' : 'unsigned long int',
'b' : 'char',
'h' : 'int',
'i' : 'long int',
'f' : 'float' }
class Proto:
pass
class Packet:
def __init__(self, id, transmitter = "both", attrs = []):
self.id = id
if transmitter not in [ "pic", "arm", "both" ]:
print("Warning: transmitter must be 'pic', 'arm' or 'both'. "
"Assume 'both'.", file=sys.stderr)
self.transmitter = "both"
else:
self.transmitter = transmitter
self.attrs = attrs
def load():
import semantic
protos = OrderedDict()
for name, proto in inspect.getmembers(semantic,
lambda x: inspect.isclass(x) and issubclass(x, semantic.Proto)):
if name != "Proto" and name != "Common":
protos[name] = load_proto(proto)
return protos
def load_proto(proto):
import semantic
p = OrderedDict()
p['id'] = proto.type
p['packets'] = OrderedDict()
for name, packet in inspect.getmembers(semantic.Common,
lambda x: isinstance(x, semantic.Packet)):
p['packets'][name] = load_packet(packet)
for name, packet in inspect.getmembers(proto,
lambda x: isinstance(x, semantic.Packet)):
p['packets'][name] = load_packet(packet)
return p
def load_packet(packet):
p = OrderedDict()
p['id'] = packet.id
p['transmitter'] = packet.transmitter
p['args'] = OrderedDict()
for arg, type in packet.attrs:
p['args'][arg] = type
return p
if __name__ == "__main__":
protos = load()
for proto in protos:
p = protos[proto]
print("%s: (board %d)" %(proto, p['id']))
for packet in p['packets']:
pa = p['packets'][packet]
print(" [%3d, %4s] %s" %(pa['id'], pa['transmitter'], packet))
for arg in pa['args']:
format = pa['args'][arg]
print(" %-10s \t(%s)" %(arg, cformats[format]))