From 90617abd59fd5f459623fda87e5ac5fc3edc6e36 Mon Sep 17 00:00:00 2001 From: Paul Hallett Date: Thu, 5 Oct 2023 11:20:46 +1300 Subject: [PATCH] Autogenerate absolute paths --- CHANGELOG.md | 1 + clientele/client_template/client.py | 8 ------- clientele/generator.py | 34 ++++++++++++++++++++-------- clientele/templates/client_py.jinja2 | 5 ++++ clientele/templates/http_py.jinja2 | 2 +- clientele/utils.py | 11 +++++++++ docs/CHANGELOG.md | 1 + tests/async_test_client/client.py | 5 +--- tests/async_test_client/http.py | 2 +- tests/test_client/client.py | 5 +--- tests/test_client/http.py | 2 +- 11 files changed, 47 insertions(+), 29 deletions(-) delete mode 100644 clientele/client_template/client.py create mode 100644 clientele/templates/client_py.jinja2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 06a279b..9169d2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * If you are using Python 3.10 or later, the `typing.Unions` types will generate as the short hand `|` instead. This required a big rework of the code to enable templating. * To regenerate a client (and to prevent accidental overrides) you must now pass `--regen t` or `-r t` to the `generate` command. This is automatically added to the line in `MANIFEST.md` to help. * Clientele will now automatically run [black](https://black.readthedocs.io/en/stable/) code formatter once a client is generated or regenerated. +* Clientele will now generate absolute paths to refer to adjacent files in the generated client, instead of relative paths. This assumes you are running the `clientele` command in the root directory of your project. ## 0.6.3 diff --git a/clientele/client_template/client.py b/clientele/client_template/client.py deleted file mode 100644 index dd2bc80..0000000 --- a/clientele/client_template/client.py +++ /dev/null @@ -1,8 +0,0 @@ -from __future__ import annotations - -import typing # noqa - -from . import ( - http, # noqa - schemas, # noqa -) diff --git a/clientele/generator.py b/clientele/generator.py index e10b465..3a5212c 100644 --- a/clientele/generator.py +++ b/clientele/generator.py @@ -18,7 +18,8 @@ PY_VERSION, templates, ) -from clientele.writer import write_to_manifest, write_to_http +from clientele.writer import write_to_manifest, write_to_http, write_to_client +from clientele.utils import get_client_project_directory_path console = Console() @@ -65,13 +66,32 @@ def __init__( self.file = file self.url = url - def generate_http_py(self): + def generate_templates_files(self): + new_unions = PY_VERSION[1] > 10 + client_project_directory_path = get_client_project_directory_path( + output_dir=self.output_dir + ) + copy_tree(src=CLIENT_TEMPLATE_ROOT, dst=self.output_dir) + if not exists(f"{self.output_dir}/config.py"): + copyfile( + f"{CONSTANTS_ROOT}/config_template.py", + f"{self.output_dir}/config.py", + ) + # client file + if exists(f"{self.output_dir}/client.py"): + remove(f"{self.output_dir}/client.py") + template = templates.get_template("client_py.jinja2") + content = template.render( + client_project_directory_path=client_project_directory_path + ) + write_to_client(content, output_dir=self.output_dir) + # http file if exists(f"{self.output_dir}/http.py"): remove(f"{self.output_dir}/http.py") - new_unions = PY_VERSION[1] > 10 template = templates.get_template("http_py.jinja2") content = template.render( new_unions=new_unions, + client_project_directory_path=client_project_directory_path, ) write_to_http(content, output_dir=self.output_dir) @@ -113,13 +133,7 @@ def format_client(self) -> None: ) def generate(self) -> None: - copy_tree(src=CLIENT_TEMPLATE_ROOT, dst=self.output_dir) - if not exists(f"{self.output_dir}/config.py"): - copyfile( - f"{CONSTANTS_ROOT}/config_template.py", - f"{self.output_dir}/config.py", - ) - self.generate_http_py() + self.generate_templates_files() self.generate_manifest() self.schemas_generator.generate_schema_classes() self.clients_generator.generate_paths() diff --git a/clientele/templates/client_py.jinja2 b/clientele/templates/client_py.jinja2 new file mode 100644 index 0000000..9e941a6 --- /dev/null +++ b/clientele/templates/client_py.jinja2 @@ -0,0 +1,5 @@ +from __future__ import annotations + +import typing # noqa + +from {{client_project_directory_path}} import http, schemas # noqa diff --git a/clientele/templates/http_py.jinja2 b/clientele/templates/http_py.jinja2 index 9f7aefc..36e5974 100644 --- a/clientele/templates/http_py.jinja2 +++ b/clientele/templates/http_py.jinja2 @@ -5,7 +5,7 @@ import types {% endif %} import httpx # noqa -from . import config as c # noqa +from {{client_project_directory_path}} import config as c # noqa class APIException(Exception): diff --git a/clientele/utils.py b/clientele/utils.py index b274213..36af798 100644 --- a/clientele/utils.py +++ b/clientele/utils.py @@ -1,3 +1,4 @@ +import os import re from openapi_core import Spec @@ -117,3 +118,13 @@ def union_for_py_ver(union_items: list) -> str: return " | ".join(union_items) else: return f"typing.Union[{', '.join(union_items)}]" + + +def get_client_project_directory_path(output_dir: str) -> str: + """ + Returns a dot-notation path for the client directory. + Assumes that the `clientele` command is being run in the + project root directory. + """ + dot_notation = ".".join(os.path.join(output_dir).split("/")[:-1]) + return dot_notation diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 06a279b..9169d2a 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -6,6 +6,7 @@ * If you are using Python 3.10 or later, the `typing.Unions` types will generate as the short hand `|` instead. This required a big rework of the code to enable templating. * To regenerate a client (and to prevent accidental overrides) you must now pass `--regen t` or `-r t` to the `generate` command. This is automatically added to the line in `MANIFEST.md` to help. * Clientele will now automatically run [black](https://black.readthedocs.io/en/stable/) code formatter once a client is generated or regenerated. +* Clientele will now generate absolute paths to refer to adjacent files in the generated client, instead of relative paths. This assumes you are running the `clientele` command in the root directory of your project. ## 0.6.3 diff --git a/tests/async_test_client/client.py b/tests/async_test_client/client.py index be02741..7d9314c 100644 --- a/tests/async_test_client/client.py +++ b/tests/async_test_client/client.py @@ -2,10 +2,7 @@ import typing # noqa -from . import ( - http, # noqa - schemas, # noqa -) +from tests.async_test_client import http, schemas # noqa async def complex_model_request_complex_model_request_get() -> schemas.ComplexModelResponse: diff --git a/tests/async_test_client/http.py b/tests/async_test_client/http.py index 26a2ca5..996138e 100644 --- a/tests/async_test_client/http.py +++ b/tests/async_test_client/http.py @@ -4,7 +4,7 @@ import httpx # noqa -from . import config as c # noqa +from tests.async_test_client import config as c # noqa class APIException(Exception): diff --git a/tests/test_client/client.py b/tests/test_client/client.py index 86b0a70..7775dce 100644 --- a/tests/test_client/client.py +++ b/tests/test_client/client.py @@ -2,10 +2,7 @@ import typing # noqa -from . import ( - http, # noqa - schemas, # noqa -) +from tests.test_client import http, schemas # noqa def complex_model_request_complex_model_request_get() -> schemas.ComplexModelResponse: diff --git a/tests/test_client/http.py b/tests/test_client/http.py index 13a0e1f..e1598cf 100644 --- a/tests/test_client/http.py +++ b/tests/test_client/http.py @@ -4,7 +4,7 @@ import httpx # noqa -from . import config as c # noqa +from tests.test_client import config as c # noqa class APIException(Exception):