diff --git a/interface_gen/generate.py b/interface_gen/generate.py index 6c1398a..8a51d02 100644 --- a/interface_gen/generate.py +++ b/interface_gen/generate.py @@ -1,43 +1,43 @@ import argparse import os -from pathlib import Path import subprocess +from pathlib import Path + from avrotize.avrotoproto import convert_avro_to_proto, json -from . import docs -from . import toolchain +from . import docs, toolchain # Main script to generate all the things from the Avro IDL definitions. def namespace(schema_file: Path) -> str | None: - """ Extract namespace from Avro schema file. """ + """Extract namespace from Avro schema file.""" with open(schema_file, "r") as f: obj = json.load(f) return obj.get("namespace", None) class Schemas: - """ Helper class for enumerating schemas in this repo. Provides a - builder-style API (methods that return reference to self to allow - chaining calls). """ + """Helper class for enumerating schemas in this repo. Provides a + builder-style API (methods that return reference to self to allow + chaining calls).""" def __init__(self, protocol_dir: Path | str): self.schemas: list[Path] = [] # path to root of protocol directory self.proto_dir = Path(protocol_dir) - # --> Buider methods + # --> Builder methods - def with_version(self, version: str) -> 'Schemas': + def with_version(self, version: str) -> "Schemas": return self.find_in_path(os.path.join(self.proto_dir, f"v{version}")) - def all(self) -> 'Schemas': + def all(self) -> "Schemas": return self.find_in_path() - def find_in_path(self, path: str | None = None) -> 'Schemas': - """ Find all Avro schemas in given `path`. If path is None, use - top-level project default. """ + def find_in_path(self, path: str | None = None) -> "Schemas": + """Find all Avro schemas in given `path`. If path is None, use + top-level project default.""" if path: start_path = Path(path) else: @@ -46,9 +46,9 @@ def find_in_path(self, path: str | None = None) -> 'Schemas': self.schemas = list(start_path.rglob("*.avsc")) return self - def from_avro_idl(self, output_dir: Path) -> 'Schemas': - """ Generate all schemas from main Avro IDL files, and select all - schemas for future operations on this object. """ + def from_avro_idl(self, output_dir: Path) -> "Schemas": + """Generate all schemas from main Avro IDL files, and select all + schemas for future operations on this object.""" protocol_path = self.proto_dir avdl_files = protocol_path.rglob("*.avdl") for avdl in avdl_files: @@ -57,7 +57,7 @@ def from_avro_idl(self, output_dir: Path) -> 'Schemas': sdir = output_dir / version_dir / "schema" script = toolchain.script_dir().parent / "avro" / "bin" / "avro-idl.sh" subprocess.check_call([str(script), str(avdl), str(sdir)]) - return self.all() + return self.find_in_path(output_dir) # --> Action methods @@ -87,16 +87,23 @@ def main(): Output will be written to the output directory (-o): Avro schemas, proto3 definitions, markdown documentation. """ - parser = argparse.ArgumentParser(prog="generate.py", - description="Generate docs and definitions from Avro IDL.", - epilog=epilog) - parser.add_argument('-p', '--protocol-dir', - help="Path w/ Avro IDL files in version subdirs") - parser.add_argument('-o', '--output-dir', required=True, - help="Path where to generate markdown docs") - parser.add_argument('-i', '--install-toolchain', - help="Install toolchain dependencies if needed.", - action="store_true") + parser = argparse.ArgumentParser( + prog="generate.py", + description="Generate docs and definitions from Avro IDL.", + epilog=epilog, + ) + parser.add_argument( + "-p", "--protocol-dir", help="Path w/ Avro IDL files in version subdirs" + ) + parser.add_argument( + "-o", "--output-dir", required=True, help="Path where to generate markdown docs" + ) + parser.add_argument( + "-i", + "--install-toolchain", + help="Install toolchain dependencies if needed.", + action="store_true", + ) args = parser.parse_args() if args.install_toolchain: