Skip to content

Commit

Permalink
Merge pull request #19 from phalt/project-paths
Browse files Browse the repository at this point in the history
Autogenerate absolute paths
  • Loading branch information
phalt authored Oct 4, 2023
2 parents 6669b05 + 90617ab commit 65d80d0
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 0 additions & 8 deletions clientele/client_template/client.py

This file was deleted.

34 changes: 24 additions & 10 deletions clientele/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions clientele/templates/client_py.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from __future__ import annotations

import typing # noqa

from {{client_project_directory_path}} import http, schemas # noqa
2 changes: 1 addition & 1 deletion clientele/templates/http_py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions clientele/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import re

from openapi_core import Spec
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions tests/async_test_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/async_test_client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 1 addition & 4 deletions tests/test_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 65d80d0

Please sign in to comment.