2023-12-01 23:52:38 -05:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
2021-09-17 00:10:29 -07:00
|
|
|
# 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.
|
|
|
|
|
|
2024-06-18 10:40:37 -04:00
|
|
|
import argparse
|
|
|
|
|
from pathlib import Path
|
2019-08-18 18:00:40 -04:00
|
|
|
|
2024-11-02 17:56:55 -07:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
|
2019-08-18 18:00:40 -04:00
|
|
|
|
2024-06-18 10:40:37 -04:00
|
|
|
def output(output_dir: Path, outfn: str, contents: str):
|
|
|
|
|
output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
output_file = output_dir / outfn
|
2024-10-28 22:28:58 -04:00
|
|
|
output_file.write_text(contents, encoding="utf-8", newline="\n")
|
2019-08-18 18:00:40 -04:00
|
|
|
|
|
|
|
|
|
2024-06-18 10:40:37 -04:00
|
|
|
def generate_numbers(output_directory: Path, template_root: Path):
|
2021-09-17 00:10:29 -07:00
|
|
|
MAX_NUM = 20
|
2019-08-18 18:00:40 -04:00
|
|
|
|
2022-07-01 06:41:44 -07:00
|
|
|
env = Environment(
|
2024-06-18 10:40:37 -04:00
|
|
|
loader=FileSystemLoader(template_root / "main/java"),
|
2022-07-01 06:41:44 -07:00
|
|
|
autoescape=False,
|
|
|
|
|
keep_trailing_newline=True,
|
|
|
|
|
)
|
2020-08-29 15:06:49 -07:00
|
|
|
|
2021-09-17 00:10:29 -07:00
|
|
|
template = env.get_template("GenericNumber.java.jinja")
|
2025-11-07 19:56:21 -05:00
|
|
|
rootPath = output_directory / "main/java/org/wpilib/math/numbers"
|
2020-08-29 15:06:49 -07:00
|
|
|
|
2021-09-17 00:10:29 -07:00
|
|
|
for i in range(MAX_NUM + 1):
|
|
|
|
|
contents = template.render(num=i)
|
|
|
|
|
output(rootPath, f"N{i}.java", contents)
|
2020-08-29 15:06:49 -07:00
|
|
|
|
2021-09-17 00:10:29 -07:00
|
|
|
template = env.get_template("Nat.java.jinja")
|
2025-11-07 19:56:21 -05:00
|
|
|
rootPath = output_directory / "main/java/org/wpilib/math/util"
|
2021-09-17 00:10:29 -07:00
|
|
|
contents = template.render(nums=range(MAX_NUM + 1))
|
|
|
|
|
output(rootPath, "Nat.java", contents)
|
2020-08-29 15:06:49 -07:00
|
|
|
|
|
|
|
|
|
2024-11-02 19:09:32 -07:00
|
|
|
def main():
|
2024-06-18 10:40:37 -04:00
|
|
|
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,
|
|
|
|
|
)
|
2024-11-02 19:09:32 -07:00
|
|
|
args = parser.parse_args()
|
2024-06-18 10:40:37 -04:00
|
|
|
|
|
|
|
|
generate_numbers(args.output_directory, args.template_root)
|
|
|
|
|
|
|
|
|
|
|
2020-08-29 15:06:49 -07:00
|
|
|
if __name__ == "__main__":
|
2024-11-02 19:09:32 -07:00
|
|
|
main()
|