-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregistry_gen.py
123 lines (98 loc) · 3.75 KB
/
registry_gen.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
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
121
122
"""
This file is used to generate the registry.md file
It reads the SolDefines.py file to generate a readable list of SOL Types
"""
import sys
import os
# =========================== adjust path =====================================
sys.path.insert(0, os.getcwd())
from sensorobjectlibrary import SolDefines
# =========================== output file constants ===========================
REGISTRY_FILE = "registry.md"
INTRO_SEC = \
"""When a sensor is associated a well-known length, the `L` (length) field MAY be omitted from its binary representation.
When the well-known length is the table below is \"None\", the `L` (length) MUST be present in the object's binary representation.
"""
TAB_HEADER = \
"""
| value | name |
|---------:|-----------------------------------------------------------------------------|
| `0x00` | _reserved_ |
"""
TAB_BOTTOM = \
"""| `0xff` | _reserved_ |
| `0xffff` | _reserved_ |
"""
TAB_VAL_SIZE = 10
TAB_NAME_SIZE = 77
# =========================== helpers =========================================
def pystruct_to_human(pystruct):
human_struct = []
struct_map = {
"b": "INT8",
"B": "INT8U",
"h": "INT16",
"H": "INT16U",
"i": "INT32",
"I": "INT32U",
"l": "INT32",
"L": "INT32U",
"q": "INT64",
"Q": "INT64U",
"f": "INT32",
"p": "INT8",
}
for i in range(0,len(pystruct)):
if struct_map.has_key(pystruct[i]):
human_struct.append(struct_map[pystruct[i]])
elif pystruct[i].isdigit():
human_struct.append(
"".join([
pystruct[i],
struct_map[pystruct[i+1]]
])
)
i+=1
i+=1
return human_struct
# =========================== output file generation ==========================
with open(REGISTRY_FILE, 'w') as reg_file:
# write file head
reg_file.write(INTRO_SEC)
reg_file.write(TAB_HEADER)
# get the SOL Types
dict_types = {}
for sol_type in dir(SolDefines):
if sol_type.startswith('SOL_TYPE_'):
dict_types[getattr(SolDefines,sol_type)] = sol_type
# write SOL Types
for key, value in dict_types.iteritems():
short_type = value[len("SOL_TYPE_"):]
reg_file.write("|{0}|{1}|\n".format(
"".join(["`",format(key, '#04x'),"` "]).rjust(TAB_VAL_SIZE),
"".join([" [`",short_type,"`](#",short_type.lower(),")"]).ljust(TAB_NAME_SIZE)
))
# write Tab bottom
reg_file.write(TAB_BOTTOM)
# write types details
for item in SolDefines.sol_types:
fields_lines = ""
scores_lines = ""
struct_lines = ""
# prepare equally sized columns
for i in range(0, len(item["fields"])):
field = item["fields"][i]
if len(item["structure"]) < 2:
continue
struct = pystruct_to_human(item["structure"][1:])[i]
max_len = max(len(field), len(struct))
fields_lines += "| {0} ".format(field.rjust(max_len))
scores_lines += "|{0}".format("-"*((max_len)+2))
struct_lines += "| {0} ".format(struct.rjust(max_len))
# write columns
reg_file.write("\n#### {0}\n\n{1}|\n{2}|\n{3}|\n".format(
SolDefines.sol_type_to_type_name(item["type"])[len("SOL_TYPE_"):],
fields_lines,
scores_lines,
struct_lines,
))