Skip to content

Commit

Permalink
[build] Use pathlib in wpiunits generation script (wpilibsuite#7306)
Browse files Browse the repository at this point in the history
Makes wpiunits more consistent with the other generation scripts.

Also sort imports, remove args from main.
  • Loading branch information
rzblue authored Oct 30, 2024
1 parent defcc02 commit 89c5d98
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions wpiunits/generate_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,18 @@
# those interfaces.
# Generated files will be located in wpiunits/src/generated/main/

import argparse
import inspect
import os
import re
from jinja2 import Environment, FileSystemLoader


def output(outPath, outfn, contents):
if not os.path.exists(outPath):
os.makedirs(outPath)
from pathlib import Path

outpathname = f"{outPath}/{outfn}"
from jinja2 import Environment, FileSystemLoader

if os.path.exists(outpathname):
with open(outpathname, "r") as f:
if f.read() == contents:
return

# File either doesn't exist or has different contents
with open(outpathname, "w", newline="\n") as f:
f.write(contents)
def output(output_dir, outfn: str, contents: str):
output_dir.mkdir(parents=True, exist_ok=True)
output_file = output_dir / outfn
output_file.write_text(contents, encoding="utf-8", newline="\n")


# The units for which multiply and divide mathematical operations are defined
Expand Down Expand Up @@ -352,19 +344,17 @@ def indent(multiline_string, indentation):
)


def main():
dirname, _ = os.path.split(os.path.abspath(__file__))

def generate_units(output_directory: Path, template_directory: Path):
env = Environment(
loader=FileSystemLoader(f"{dirname}/src/generate/main/java"),
loader=FileSystemLoader(template_directory / "main/java"),
autoescape=False,
keep_trailing_newline=True,
)

interfaceTemplate = env.get_template("Measure-interface.java.jinja")
immutableTemplate = env.get_template("Measure-immutable.java.jinja")
mutableTemplate = env.get_template("Measure-mutable.java.jinja")
rootPath = f"{dirname}/src/generated/main/java/edu/wpi/first/units"
rootPath = output_directory / "main/java/edu/wpi/first/units"

helpers = {
"type_decl": type_decl,
Expand Down Expand Up @@ -395,9 +385,31 @@ def main():
helpers=helpers,
)

output(f"{rootPath}/measure", f"{unit_name}.java", interfaceContents)
output(f"{rootPath}/measure", f"Immutable{unit_name}.java", immutableContents)
output(f"{rootPath}/measure", f"Mut{unit_name}.java", mutableContents)
output(rootPath / "measure", f"{unit_name}.java", interfaceContents)
output(rootPath / "measure", f"Immutable{unit_name}.java", immutableContents)
output(rootPath / "measure", f"Mut{unit_name}.java", mutableContents)


def main():
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()

generate_units(args.output_directory, args.template_root)


if __name__ == "__main__":
Expand Down

0 comments on commit 89c5d98

Please sign in to comment.