-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathgenerate_pwm_motor_controllers.py
More file actions
executable file
·86 lines (71 loc) · 2.49 KB
/
Copy pathgenerate_pwm_motor_controllers.py
File metadata and controls
executable file
·86 lines (71 loc) · 2.49 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.
import argparse
import json
from pathlib import Path
from typing import Any
from jinja2 import Environment, FileSystemLoader
from jinja2.environment import Template
def render_template(
template: Template, output_dir: Path, filename: str, controller: dict[str, Any]
):
output_dir.mkdir(parents=True, exist_ok=True)
(output_dir / filename).write_text(
template.render(controller), encoding="utf-8", newline="\n"
)
def generate_pwm_motor_controllers(output_root: Path, template_root: Path):
with (template_root / "pwm_motor_controllers.json").open(encoding="utf-8") as f:
controllers = json.load(f)
template_paths = (
(
"main/java",
"pwm_motor_controller.java.jinja",
output_root / "main/java/org/wpilib/drivers/motor",
".java",
),
(
"main/native/include",
"pwm_motor_controller.hpp.jinja",
output_root / "main/native/include/wpi/drivers/motor",
".hpp",
),
(
"main/native/cpp",
"pwm_motor_controller.cpp.jinja",
output_root / "main/native/cpp/motor",
".cpp",
),
)
for template_dir, template_name, output_dir, suffix in template_paths:
env = Environment(
loader=FileSystemLoader(str(template_root / template_dir)),
autoescape=False,
keep_trailing_newline=True,
)
template = env.get_template(template_name)
for controller in controllers:
render_template(
template, output_dir, f"{controller['name']}{suffix}", controller
)
def main():
script_path = Path(__file__).resolve()
dirname = script_path.parent
parser = argparse.ArgumentParser()
parser.add_argument(
"--output_directory",
help="Optional output directory for generated files",
default=dirname / "src/generated",
type=Path,
)
parser.add_argument(
"--template_root",
help="Optional root directory for the schema and Jinja templates",
default=dirname / "src/generate",
type=Path,
)
args = parser.parse_args()
generate_pwm_motor_controllers(args.output_directory, args.template_root)
if __name__ == "__main__":
main()