2021-02-05 21:03:04 -08:00
|
|
|
import json
|
|
|
|
|
|
|
|
import click
|
2021-02-11 23:14:20 -08:00
|
|
|
from avrogen import write_schema_files
|
2021-02-05 21:03:04 -08:00
|
|
|
|
|
|
|
|
2021-03-18 02:05:05 -04:00
|
|
|
def suppress_checks_in_file(filepath: str) -> None:
|
|
|
|
"""Adds a couple lines to the top of a file to suppress flake8 and black"""
|
|
|
|
|
|
|
|
with open(filepath, "r+") as f:
|
|
|
|
contents = f.read()
|
|
|
|
|
|
|
|
f.seek(0, 0)
|
|
|
|
f.write("# flake8: noqa\n")
|
|
|
|
f.write("# fmt: off\n")
|
|
|
|
f.write(contents)
|
|
|
|
f.write("# fmt: on\n")
|
|
|
|
|
|
|
|
|
2021-02-05 21:03:04 -08:00
|
|
|
@click.command()
|
|
|
|
@click.argument("schema_file", type=click.Path(exists=True))
|
|
|
|
@click.argument("outdir", type=click.Path())
|
2021-04-14 13:40:24 -07:00
|
|
|
def generate(schema_file: str, outdir: str) -> None:
|
2021-02-05 21:03:04 -08:00
|
|
|
# print(f'using {schema_file}')
|
|
|
|
with open(schema_file) as f:
|
|
|
|
raw_schema_text = f.read()
|
|
|
|
|
|
|
|
no_spaces_schema = json.dumps(json.loads(raw_schema_text))
|
2021-02-12 10:46:28 -08:00
|
|
|
schema_json = no_spaces_schema.replace(
|
|
|
|
'{"type": "string", "avro.java.string": "String"}', '"string"'
|
|
|
|
)
|
2021-02-05 21:03:04 -08:00
|
|
|
|
|
|
|
redo_spaces = json.dumps(json.loads(schema_json), indent=2)
|
|
|
|
|
|
|
|
write_schema_files(redo_spaces, outdir)
|
2021-03-18 02:05:05 -04:00
|
|
|
suppress_checks_in_file(f"{outdir}/schema_classes.py")
|
|
|
|
suppress_checks_in_file(f"{outdir}/__init__.py")
|
2021-02-05 21:03:04 -08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
generate()
|