|
| 1 | +import json |
| 2 | +import pathlib |
| 3 | + |
| 4 | +import click |
| 5 | +from jinja2 import Environment, FileSystemLoader |
| 6 | + |
| 7 | +from . import openapi |
| 8 | +from . import formatter |
| 9 | + |
| 10 | + |
| 11 | +@click.command() |
| 12 | +@click.option( |
| 13 | + "-i", |
| 14 | + "--input", |
| 15 | + type=click.Path(exists=True, file_okay=True, dir_okay=False, path_type=pathlib.Path), |
| 16 | +) |
| 17 | +@click.option( |
| 18 | + "-o", |
| 19 | + "--output", |
| 20 | + type=click.Path(path_type=pathlib.Path), |
| 21 | +) |
| 22 | +def cli(input, output): |
| 23 | + """ |
| 24 | + Generate a Python code snippet from OpenAPI specification. |
| 25 | + """ |
| 26 | + spec = openapi.load(input) |
| 27 | + |
| 28 | + version = input.parent.name |
| 29 | + with (input.parent.parent.parent / "config" / f"{version}.json").open() as fp: |
| 30 | + config = json.load(fp) |
| 31 | + |
| 32 | + env = Environment(loader=FileSystemLoader(str(pathlib.Path(__file__).parent / "templates"))) |
| 33 | + |
| 34 | + env.filters["accept_headers"] = openapi.accept_headers |
| 35 | + env.filters["attribute_name"] = formatter.attribute_name |
| 36 | + env.filters["camel_case"] = formatter.camel_case |
| 37 | + env.filters["collection_format"] = openapi.collection_format |
| 38 | + env.filters["format_value"] = formatter.format_value |
| 39 | + env.filters["parameter_schema"] = openapi.parameter_schema |
| 40 | + env.filters["parameters"] = openapi.parameters |
| 41 | + env.filters["return_type"] = openapi.return_type |
| 42 | + env.filters["snake_case"] = formatter.snake_case |
| 43 | + |
| 44 | + env.globals["config"] = config |
| 45 | + env.globals["enumerate"] = enumerate |
| 46 | + env.globals["version"] = version |
| 47 | + env.globals["openapi"] = spec |
| 48 | + env.globals["get_name"] = formatter.get_name |
| 49 | + env.globals["get_type_for_attribute"] = openapi.get_type_for_attribute |
| 50 | + env.globals["get_types_for_attribute"] = openapi.get_types_for_attribute |
| 51 | + env.globals["get_type_for_parameter"] = openapi.get_type_for_parameter |
| 52 | + env.globals["get_references_for_model"] = openapi.get_references_for_model |
| 53 | + env.globals["get_oneof_parameters"] = openapi.get_oneof_parameters |
| 54 | + env.globals["get_type_for_items"] = openapi.get_type_for_items |
| 55 | + env.globals["get_api_models"] = openapi.get_api_models |
| 56 | + env.globals["get_enum_type"] = openapi.get_enum_type |
| 57 | + env.globals["get_enum_default"] = openapi.get_enum_default |
| 58 | + env.globals["get_oneof_types"] = openapi.get_oneof_types |
| 59 | + env.globals["get_oneof_models"] = openapi.get_oneof_models |
| 60 | + env.globals["type_to_python"] = openapi.type_to_python |
| 61 | + |
| 62 | + api_j2 = env.get_template("api.j2") |
| 63 | + apis_j2 = env.get_template("apis.j2") |
| 64 | + model_j2 = env.get_template("model.j2") |
| 65 | + models_j2 = env.get_template("models.j2") |
| 66 | + init_j2 = env.get_template("init.j2") |
| 67 | + configuration_j2 = env.get_template("configuration.j2") |
| 68 | + |
| 69 | + extra_files = { |
| 70 | + "api_client.py": env.get_template("api_client.j2"), |
| 71 | + "exceptions.py": env.get_template("exceptions.j2"), |
| 72 | + "model_utils.py": env.get_template("model_utils.j2"), |
| 73 | + "rest.py": env.get_template("rest.j2"), |
| 74 | + } |
| 75 | + |
| 76 | + apis = openapi.apis(spec) |
| 77 | + models = openapi.models(spec) |
| 78 | + |
| 79 | + package = output / config["packageName"].replace(".", "/") |
| 80 | + package.mkdir(parents=True, exist_ok=True) |
| 81 | + |
| 82 | + for name, template in extra_files.items(): |
| 83 | + filename = package / name |
| 84 | + with filename.open("w") as fp: |
| 85 | + fp.write(template.render(apis=apis, models=models)) |
| 86 | + |
| 87 | + for name, model in models.items(): |
| 88 | + filename = formatter.snake_case(name) + ".py" |
| 89 | + model_path = package / "model" / filename |
| 90 | + model_path.parent.mkdir(parents=True, exist_ok=True) |
| 91 | + with model_path.open("w") as fp: |
| 92 | + fp.write(model_j2.render(name=name, model=model)) |
| 93 | + |
| 94 | + model_init_path = package / "model" / "__init__.py" |
| 95 | + with model_init_path.open("w") as fp: |
| 96 | + fp.write("") |
| 97 | + |
| 98 | + models_path = package / "models" / "__init__.py" |
| 99 | + models_path.parent.mkdir(parents=True, exist_ok=True) |
| 100 | + with models_path.open("w") as fp: |
| 101 | + fp.write(models_j2.render(models=sorted(models))) |
| 102 | + |
| 103 | + for name, operations in apis.items(): |
| 104 | + filename = formatter.snake_case(name) + "_api.py" |
| 105 | + api_path = package / "api" / filename |
| 106 | + api_path.parent.mkdir(parents=True, exist_ok=True) |
| 107 | + with api_path.open("w") as fp: |
| 108 | + fp.write(api_j2.render(name=name, operations=operations)) |
| 109 | + |
| 110 | + api_init_path = package / "api" / "__init__.py" |
| 111 | + with api_init_path.open("w") as fp: |
| 112 | + fp.write("") |
| 113 | + |
| 114 | + apis_path = package / "apis" / "__init__.py" |
| 115 | + apis_path.parent.mkdir(parents=True, exist_ok=True) |
| 116 | + with apis_path.open("w") as fp: |
| 117 | + fp.write(apis_j2.render(apis=sorted(apis))) |
| 118 | + |
| 119 | + init_path = package / "__init__.py" |
| 120 | + with init_path.open("w") as fp: |
| 121 | + fp.write(init_j2.render()) |
| 122 | + |
| 123 | + config_path = package / "configuration.py" |
| 124 | + with config_path.open("w") as fp: |
| 125 | + fp.write(configuration_j2.render(apis=apis)) |
0 commit comments