forked from wpilibsuite/allwpilib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_topics.py
executable file
·154 lines (134 loc) · 5.2 KB
/
generate_topics.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python3
import argparse
import json
import sys
from pathlib import Path
from jinja2 import Environment, FileSystemLoader
def Output(output_dir: Path, controller_name: str, contents: str):
output_dir.mkdir(parents=True, exist_ok=True)
output_file = output_dir / controller_name
output_file.write_text(contents, encoding="utf-8", newline="\n")
def generate_topics(
output_directory: Path, template_root: Path, types_schema_file: Path
):
with (types_schema_file).open(encoding="utf-8") as f:
types = json.load(f)
# Java files
java_template_directory = template_root / "main/java"
env = Environment(
loader=FileSystemLoader(java_template_directory), autoescape=False
)
generated_output_dir = output_directory / "main/java/edu/wpi/first/networktables"
for fn in java_template_directory.glob("*.jinja"):
template = env.get_template(fn.name)
outfn = fn.stem
if outfn.startswith("NetworkTable") or outfn.startswith("Generic"):
output = template.render(types=types)
Output(generated_output_dir, outfn, output)
else:
for replacements in types:
output = template.render(replacements)
if outfn == "Timestamped.java":
outfn2 = f"Timestamped{replacements['TypeName']}.java"
else:
outfn2 = f"{replacements['TypeName']}{outfn}"
Output(generated_output_dir, outfn2, output)
# C++ classes
cpp_subdirectory = "main/native/include/networktables"
cpp_template_directory = template_root / cpp_subdirectory
env = Environment(
loader=FileSystemLoader(cpp_template_directory),
autoescape=False,
)
generated_output_dir = output_directory / cpp_subdirectory
for fn in cpp_template_directory.glob("*.jinja"):
template = env.get_template(fn.name)
outfn = fn.stem # drop ".jinja"
for replacements in types:
output = template.render(replacements)
outfn2 = f"{replacements['TypeName']}{outfn}"
Output(generated_output_dir, outfn2, output)
# C++ handle API (header)
hdr_subdirectory = "main/native/include"
hdr_template_directory = template_root / hdr_subdirectory
env = Environment(
loader=FileSystemLoader(hdr_template_directory),
autoescape=False,
)
template = env.get_template("ntcore_cpp_types.h.jinja")
output = template.render(types=types)
Output(
output_directory / hdr_subdirectory,
"ntcore_cpp_types.h",
output,
)
# C++ handle API (source)
cpp_subdirectory = "main/native/cpp"
cpp_template_directory = template_root / cpp_subdirectory
env = Environment(
loader=FileSystemLoader(cpp_template_directory),
autoescape=False,
)
template = env.get_template("ntcore_cpp_types.cpp.jinja")
output = template.render(types=types)
Output(
output_directory / cpp_subdirectory,
"ntcore_cpp_types.cpp",
output,
)
# C handle API (header)
hdr_subdirectory = "main/native/include"
hdr_template_directory = template_root / hdr_subdirectory
env = Environment(
loader=FileSystemLoader(hdr_template_directory),
autoescape=False,
)
template = env.get_template("ntcore_c_types.h.jinja")
output = template.render(types=types)
Output(output_directory / hdr_subdirectory, "ntcore_c_types.h", output)
# C handle API (source)
c_subdirectory = "main/native/cpp"
c_template_directory = template_root / c_subdirectory
env = Environment(
loader=FileSystemLoader(c_template_directory),
autoescape=False,
)
template = env.get_template("ntcore_c_types.cpp.jinja")
output = template.render(types=types)
Output(output_directory / c_subdirectory, "ntcore_c_types.cpp", output)
# JNI
jni_subdirectory = "main/native/cpp/jni"
jni_template_directory = template_root / jni_subdirectory
env = Environment(
loader=FileSystemLoader(jni_template_directory),
autoescape=False,
)
template = env.get_template("types_jni.cpp.jinja")
output = template.render(types=types)
Output(output_directory / jni_subdirectory, "types_jni.cpp", output)
def main(argv):
script_path = Path(__file__).resolve()
dirname = script_path.parent
parser = argparse.ArgumentParser()
parser.add_argument(
"--output_directory",
help="Optional. If set, will output the generated files to this directory, otherwise it will use a path relative to the script",
default=dirname / "src/generated",
type=Path,
)
parser.add_argument(
"--types_schema_file",
help="Optional. If set, this file will be used to load the types schema",
default=dirname / "src/generate/types.json",
type=Path,
)
parser.add_argument(
"--template_root",
help="Optional. If set, will use this directory as the root for the jinja templates",
default=dirname / "src/generate",
type=Path,
)
args = parser.parse_args(argv)
generate_topics(args.output_directory, args.template_root, args.types_schema_file)
if __name__ == "__main__":
main(sys.argv[1:])