-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcanboat2python.py
More file actions
63 lines (45 loc) · 1.67 KB
/
canboat2python.py
File metadata and controls
63 lines (45 loc) · 1.67 KB
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
import json
import os
import re
from jinja2 import Environment, FileSystemLoader
# Load the JSON data
with open("canboat.json") as f:
json_data = json.load(f)
def bits_to_hex(len: int) -> str:
num = 0
for i in range(len):
num = (num << 1) ^ 1
return f"0x{num:X}"
def generate_field_id(field_id, field_type, field_offset):
if field_type == "RESERVED":
return "reserved_" + str(field_offset)
return field_id
pattern = r"[^a-zA-Z0-9]"
def generate_field_python_name(field_name, field_type, field_offset):
if field_type == "RESERVED":
return "reserved_" + str(field_offset)
temp = re.sub(pattern, "_", field_name).lower()
if temp[0].isdigit() or temp == "global":
temp = "__" + temp
return temp
# Set up the Jinja2 environment
file_loader = FileSystemLoader(searchpath="./")
env = Environment(loader=file_loader, extensions=["jinja2.ext.loopcontrols"])
env.globals["bits_to_hex"] = bits_to_hex
env.globals["generate_field_id"] = generate_field_id
env.globals["generate_field_python_name"] = generate_field_python_name
# Load the Jinja2 template
template = env.get_template("python.consts.j2")
# Render the template with the JSON data
output = template.render(data=json_data)
# Save the generated Python code to a file
with open(os.path.join("nmea2000", "consts.py"), "w") as f:
f.write(output)
# Load the Jinja2 template
template = env.get_template("python.PGNs.j2")
# Render the template with the JSON data
output = template.render(data=json_data)
# Save the generated Python code to a file
with open(os.path.join("nmea2000", "pgns.py"), "w") as f:
f.write(output)
print("Python code generated successfully!")