Skip to content

Commit

Permalink
generator: accept multiple .py files as input
Browse files Browse the repository at this point in the history
The types.py file in the Subiquity tree is getting difficult to maintain
and it would be ideal to split it into multiple files going forward.

Ensure that the generator script can parse content from multiple files.

NOTE: Previously the syntax `./generator.py file1 file2` would parse
"file1" and output to "file2". Now, this syntax would parse "file1" and
"file2" ; and would output to the standard output. The generator will
look for specific file extensions when reading (i.e., *.py) and writing
(i.e., *.dart) so there should be no risk of overwriting important files
if using the generator incorrectly.

Signed-off-by: Olivier Gayot <[email protected]>
  • Loading branch information
ogayot committed Jun 11, 2024
1 parent 3098703 commit 5cc47e0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/subiquity_client/generator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ types_py = ../subiquity/subiquity/common/types.py
types_dart = ../lib/src/types.dart

generate:
python3 generator.py $(types_py) $(types_dart)
python3 generator.py $(types_py) --output $(types_dart)
dart format $(types_dart)

check:
Expand Down
39 changes: 33 additions & 6 deletions packages/subiquity_client/generator/generator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3

import argparse
import ast
import fileinput
import os
Expand Down Expand Up @@ -355,17 +356,43 @@ def __str__(self):
}}
"""

class PythonInputFile(argparse.FileType):
def __init__(self):
super().__init__("r", encoding="utf-8")

def __call__(self, path: str):
if path != "-" and not path.endswith(".py"):
raise argparse.ArgumentTypeError(f"{path} does not have the .py extension")

return super().__call__(path)


def DartOutputFile(path: str | None) -> str | None:
if path is None or path == "-":
return None

if not path.endswith(".dart"):
raise argparse.ArgumentTypeError(f"{path} does not have a .dart extension")
return path


def main():
input = sys.argv[1] if len(sys.argv) > 1 else ""
output = sys.argv[2] if len(sys.argv) == 3 else None
parser = argparse.ArgumentParser()

# FileType is supposed to work with default="-" but it does not when used
# with positional arguments.
parser.add_argument("python-input-files", nargs="+",
type=PythonInputFile(), metavar="input.py")
parser.add_argument("--output", default="-", metavar="output.dart",
type=DartOutputFile)

args = vars(parser.parse_args())

if not input.endswith(".py") or (output is not None and not output.endswith(".dart")):
print("usage: generator <input.py> (<output.dart>)")
return
input_files = args["python-input-files"]
output = args["output"]

generator = Generator()
with open(sys.argv[1], "r") as file:
for file in input_files:
generator.parse(file.read())

data = generator.to_dart()
Expand Down

0 comments on commit 5cc47e0

Please sign in to comment.