Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 3 | # Copyright (c) FIRST and other WPILib contributors. |
| 4 | # Open Source Software; you can modify and/or share it under the terms of |
| 5 | # the WPILib BSD license file in the root directory of this project. |
| 6 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 7 | import os |
| 8 | import sys |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 9 | from jinja2 import Environment, FileSystemLoader |
| 10 | |
| 11 | |
| 12 | def output(outPath, outfn, contents): |
| 13 | if not os.path.exists(outPath): |
| 14 | os.makedirs(outPath) |
| 15 | |
| 16 | outpathname = f"{outPath}/{outfn}" |
| 17 | |
| 18 | if os.path.exists(outpathname): |
| 19 | with open(outpathname, "r") as f: |
| 20 | if f.read() == contents: |
| 21 | return |
| 22 | |
| 23 | # File either doesn't exist or has different contents |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame] | 24 | with open(outpathname, "w", newline="\n") as f: |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 25 | f.write(contents) |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 26 | |
| 27 | |
| 28 | def main(): |
| 29 | MAX_NUM = 20 |
| 30 | |
| 31 | dirname, _ = os.path.split(os.path.abspath(__file__)) |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 32 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 33 | env = Environment( |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame] | 34 | loader=FileSystemLoader(f"{dirname}/src/generate/main/java"), |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 35 | autoescape=False, |
| 36 | keep_trailing_newline=True, |
| 37 | ) |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 38 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 39 | template = env.get_template("GenericNumber.java.jinja") |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame] | 40 | rootPath = f"{dirname}/src/generated/main/java/edu/wpi/first/math/numbers" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 41 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 42 | for i in range(MAX_NUM + 1): |
| 43 | contents = template.render(num=i) |
| 44 | output(rootPath, f"N{i}.java", contents) |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 45 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 46 | template = env.get_template("Nat.java.jinja") |
Maxwell Henderson | 80bec32 | 2024-01-09 15:48:44 -0800 | [diff] [blame] | 47 | rootPath = f"{dirname}/src/generated/main/java/edu/wpi/first/math" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 48 | contents = template.render(nums=range(MAX_NUM + 1)) |
| 49 | output(rootPath, "Nat.java", contents) |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 50 | |
| 51 | |
| 52 | if __name__ == "__main__": |
| 53 | main() |