forked from wpilibsuite/allwpilib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_pwm_motor_controllers.py
executable file
·66 lines (50 loc) · 2.08 KB
/
generate_pwm_motor_controllers.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
#!/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
import sys
from pathlib import Path
from typing import Dict, 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_file = output_dir / filename
output_file.write_text(template.render(controller), encoding="utf-8", newline="\n")
def generate_pwm_motor_controllers(output_root, template_root):
with (template_root / "pwm_motor_controllers.json").open(encoding="utf-8") as f:
controllers = json.load(f)
env = Environment(
loader=FileSystemLoader(str(template_root / "main/java")),
autoescape=False,
keep_trailing_newline=True,
)
root_path = Path(output_root) / "main/java/edu/wpi/first/wpilibj/motorcontrol"
template = env.get_template("pwm_motor_controller.java.jinja")
for controller in controllers:
controller_name = f"{controller['name']}.java"
render_template(template, root_path, controller_name, controller)
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(
"--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_pwm_motor_controllers(args.output_directory, args.template_root)
if __name__ == "__main__":
main(sys.argv[1:])