Skip to content

Commit 0bef909

Browse files
committed
enable custom template path cli option
1 parent 3408c1f commit 0bef909

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

openapi_python_client/__init__.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import httpcore
1010
import httpx
1111
import yaml
12-
from jinja2 import Environment, PackageLoader
12+
from jinja2 import Environment, PackageLoader, ChoiceLoader, FileSystemLoader
1313

1414
from openapi_python_client import utils
1515

@@ -30,8 +30,15 @@ class Project:
3030
project_name_override: Optional[str] = None
3131
package_name_override: Optional[str] = None
3232

33-
def __init__(self, *, openapi: GeneratorData) -> None:
33+
def __init__(self, *, openapi: GeneratorData, custom_template_path: Optional[str] = None) -> None:
3434
self.openapi: GeneratorData = openapi
35+
36+
package_loader = PackageLoader(__package__)
37+
if custom_template_path is not None:
38+
loader = ChoiceLoader([FileSystemLoader(custom_template_path), package_loader,])
39+
else:
40+
loader = package_loader
41+
self.env: Environment = Environment(loader=loader, trim_blocks=True, lstrip_blocks=True)
3542
self.env: Environment = Environment(loader=PackageLoader(__package__), trim_blocks=True, lstrip_blocks=True)
3643

3744
self.project_name: str = self.project_name_override or f"{utils.kebab_case(openapi.title).lower()}-client"
@@ -191,37 +198,37 @@ def _build_api(self) -> None:
191198
module_path.write_text(endpoint_template.render(endpoint=endpoint))
192199

193200

194-
def _get_project_for_url_or_path(url: Optional[str], path: Optional[Path]) -> Union[Project, GeneratorError]:
201+
def _get_project_for_url_or_path(url: Optional[str], path: Optional[Path], custom_template_path: Optional[str] = None) -> Union[Project, GeneratorError]:
195202
data_dict = _get_document(url=url, path=path)
196203
if isinstance(data_dict, GeneratorError):
197204
return data_dict
198205
openapi = GeneratorData.from_dict(data_dict)
199206
if isinstance(openapi, GeneratorError):
200207
return openapi
201-
return Project(openapi=openapi)
208+
return Project(openapi=openapi, custom_template_path=custom_template_path)
202209

203210

204-
def create_new_client(*, url: Optional[str], path: Optional[Path]) -> Sequence[GeneratorError]:
211+
def create_new_client(*, url: Optional[str], path: Optional[Path], custom_template_path: Optional[str] = None) -> Sequence[GeneratorError]:
205212
"""
206213
Generate the client library
207214
208215
Returns:
209216
A list containing any errors encountered when generating.
210217
"""
211-
project = _get_project_for_url_or_path(url=url, path=path)
218+
project = _get_project_for_url_or_path(url=url, path=path, custom_template_path=custom_template_path)
212219
if isinstance(project, GeneratorError):
213220
return [project]
214221
return project.build()
215222

216223

217-
def update_existing_client(*, url: Optional[str], path: Optional[Path]) -> Sequence[GeneratorError]:
224+
def update_existing_client(*, url: Optional[str], path: Optional[Path], custom_template_path: Optional[str] = None) -> Sequence[GeneratorError]:
218225
"""
219226
Update an existing client library
220227
221228
Returns:
222229
A list containing any errors encountered when generating.
223230
"""
224-
project = _get_project_for_url_or_path(url=url, path=path)
231+
project = _get_project_for_url_or_path(url=url, path=path, custom_template_path=custom_template_path)
225232
if isinstance(project, GeneratorError):
226233
return [project]
227234
return project.update()

openapi_python_client/cli.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def handle_errors(errors: Sequence[GeneratorError]) -> None:
100100
def generate(
101101
url: Optional[str] = typer.Option(None, help="A URL to read the JSON from"),
102102
path: Optional[pathlib.Path] = typer.Option(None, help="A path to the JSON file"),
103+
custom_template_path: Optional[str] = typer.Option(None, help="A path to a custom template directory"),
103104
) -> None:
104105
""" Generate a new OpenAPI Client library """
105106
from . import create_new_client
@@ -110,14 +111,15 @@ def generate(
110111
if url and path:
111112
typer.secho("Provide either --url or --path, not both", fg=typer.colors.RED)
112113
raise typer.Exit(code=1)
113-
errors = create_new_client(url=url, path=path)
114+
errors = create_new_client(url=url, path=path, custom_template_path=custom_template_path)
114115
handle_errors(errors)
115116

116117

117118
@app.command()
118119
def update(
119120
url: Optional[str] = typer.Option(None, help="A URL to read the JSON from"),
120121
path: Optional[pathlib.Path] = typer.Option(None, help="A path to the JSON file"),
122+
custom_template_path: Optional[str] = typer.Option(None, help="A path to a custom template directory"),
121123
) -> None:
122124
""" Update an existing OpenAPI Client library """
123125
from . import update_existing_client
@@ -129,5 +131,5 @@ def update(
129131
typer.secho("Provide either --url or --path, not both", fg=typer.colors.RED)
130132
raise typer.Exit(code=1)
131133

132-
errors = update_existing_client(url=url, path=path)
134+
errors = update_existing_client(url=url, path=path, custom_template_path=custom_template_path)
133135
handle_errors(errors)

0 commit comments

Comments
 (0)