Skip to content

Commit 16189b8

Browse files
committed
- Removed typer and moved everything to click, due to easier handling of documentation
1 parent 64bc812 commit 16189b8

File tree

8 files changed

+69
-68
lines changed

8 files changed

+69
-68
lines changed

poetry.lock

Lines changed: 12 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Changelog = "https://github.com/MarcoMuellner/openapi-python-generator/releases"
1818
[tool.poetry.dependencies]
1919
python = "^3.7"
2020
httpx = {extras = ["all"], version = "^0.23.0"}
21-
typer = "^0.4.1"
2221
pydantic = "^1.9.1"
2322
orjson = "^3.7.2"
2423
openapi-schema-pydantic = "^1.2.3"
@@ -53,7 +52,7 @@ myst-parser = {version = ">=0.16.1"}
5352
pytest-cov = "^3.0.0"
5453

5554
[tool.poetry.scripts]
56-
openapi-python-generator = "openapi_python_generator.__main__:app"
55+
openapi-python-generator = "openapi_python_generator.__main__:main"
5756

5857
[tool.coverage.paths]
5958
source = ["src", "*/site-packages"]
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
"""Openapi Python Generator."""
1+
"""The hypermodern Python project."""
2+
try:
3+
from importlib.metadata import version, PackageNotFoundError # type: ignore
4+
except ImportError: # pragma: no cover
5+
from importlib_metadata import version, PackageNotFoundError # type: ignore
6+
7+
8+
try:
9+
__version__ = version(__name__)
10+
except PackageNotFoundError: # pragma: no cover
11+
__version__ = "unknown"
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
from typing import Optional
22

3-
import typer
3+
import click
44

5+
from openapi_python_generator import __version__
56
from openapi_python_generator.common import HTTPLibrary
67
from openapi_python_generator.generate_data import generate_data
78

8-
app = typer.Typer()
99

10-
11-
@app.command()
10+
@click.command()
11+
@click.argument("source")
12+
@click.argument("output")
13+
@click.option(
14+
"--library",
15+
default=HTTPLibrary.httpx,
16+
type=HTTPLibrary,
17+
help="HTTP library to use in the generation of the client. Currently only httpx is supported.",
18+
)
19+
@click.version_option(version=__version__)
1220
def main(
13-
file_name: str, output: str, library: Optional[HTTPLibrary] = HTTPLibrary.httpx
21+
source: str, output: str, library: Optional[HTTPLibrary] = HTTPLibrary.httpx
1422
) -> None:
1523
"""
1624
Generate Python code from an OpenAPI 3.0 specification.
25+
26+
Provide a SOURCE (file or URL) containing the OpenAPI 3 specification and
27+
an OUTPUT path, where the resulting client is created.
1728
"""
18-
generate_data(file_name, output, library)
29+
generate_data(source, output, library)
1930

2031

2132
if __name__ == "__main__": # pragma: no cover
22-
app()
33+
main()

src/openapi_python_generator/generate_data.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from pathlib import Path
22
from typing import Optional, Union
33

4+
import click
45
import httpx
56
from httpx import ConnectError, ConnectTimeout
67
import orjson
7-
import typer
88
from pydantic import ValidationError
99
import autopep8
1010

@@ -25,31 +25,31 @@ def write_code(path: Path, content):
2525
f.write(autopep8.fix_code(content))
2626

2727

28-
def get_open_api(path: Union[str, Path]) -> OpenAPI:
28+
def get_open_api(source: Union[str, Path]) -> OpenAPI:
2929
"""
3030
Tries to fetch the openapi.json file from the web or load from a local file. Returns the according OpenAPI object.
31-
:param path:
31+
:param source:
3232
:return:
3333
"""
3434
try:
35-
if not isinstance(path, Path) and (
36-
path.startswith("http://") or path.startswith("https://")
35+
if not isinstance(source, Path) and (
36+
source.startswith("http://") or source.startswith("https://")
3737
):
38-
return OpenAPI(**orjson.loads(httpx.get(path).text))
38+
return OpenAPI(**orjson.loads(httpx.get(source).text))
3939

40-
with open(path, "r") as f:
40+
with open(source, "r") as f:
4141
return OpenAPI(**orjson.loads(f.read()))
4242
except FileNotFoundError:
43-
typer.echo(
44-
f"File {path} not found. Please make sure to pass the path to the OpenAPI 3.0 specification."
43+
click.echo(
44+
f"File {source} not found. Please make sure to pass the path to the OpenAPI 3.0 specification."
4545
)
4646
raise
4747
except (ConnectError, ConnectTimeout):
48-
typer.echo(f"Could not connect to {path}.")
49-
raise ConnectError(f"Could not connect to {path}.") from None
48+
click.echo(f"Could not connect to {source}.")
49+
raise ConnectError(f"Could not connect to {source}.") from None
5050
except (ValidationError, orjson.JSONDecodeError):
51-
typer.echo(
52-
f"File {path} is not a valid OpenAPI 3.0 specification, or there may be a problem with your JSON."
51+
click.echo(
52+
f"File {source} is not a valid OpenAPI 3.0 specification, or there may be a problem with your JSON."
5353
)
5454
raise
5555

@@ -114,14 +114,14 @@ def write_data(data: ConversionResult, output: Union[str, Path]):
114114

115115

116116
def generate_data(
117-
file_name: Union[str, Path],
117+
source: Union[str, Path],
118118
output: Union[str, Path],
119119
library: Optional[HTTPLibrary] = HTTPLibrary.httpx,
120120
) -> None:
121121
"""
122122
Generate Python code from an OpenAPI 3.0 specification.
123123
"""
124-
data = get_open_api(file_name)
125-
typer.echo(f"Generating data from {file_name}")
124+
data = get_open_api(source)
125+
click.echo(f"Generating data from {source}")
126126
result = generator(data)
127127
write_data(result, output)

src/openapi_python_generator/language_converters/python/model_generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Optional, Tuple
22

3-
import typer
3+
import click
44
from openapi_schema_pydantic import Components, Reference, Schema
55

66
from openapi_python_generator.language_converters.python.jinja_config import (
@@ -146,7 +146,7 @@ def generate_models(components: Components) -> List[Model]:
146146
compile(m.content, "<string>", "exec")
147147
models.append(m)
148148
except SyntaxError as e: # pragma: no cover
149-
typer.echo(f"Error in model {name}: {e}")
149+
click.echo(f"Error in model {name}: {e}")
150150

151151
continue # pragma: no cover
152152

@@ -174,7 +174,7 @@ def generate_models(components: Components) -> List[Model]:
174174
try:
175175
compile(generated_content, "<string>", "exec")
176176
except SyntaxError as e: # pragma: no cover
177-
typer.echo(f"Error in model {name}: {e}") # pragma: no cover
177+
click.echo(f"Error in model {name}: {e}") # pragma: no cover
178178

179179
models.append(
180180
Model(

src/openapi_python_generator/language_converters/python/service_generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Dict, List, Tuple, Union
22

3-
import typer
3+
import click
44
from openapi_schema_pydantic import (
55
PathItem,
66
Operation,
@@ -182,14 +182,14 @@ def generate_services(paths: Dict[str, PathItem]) -> List[Service]:
182182
try:
183183
compile(sync_so.content, "<string>", "exec")
184184
except SyntaxError as e: # pragma: no cover
185-
typer.echo(
185+
click.echo(
186186
f"Error in service {sync_so.operation_id}: {e}"
187187
) # pragma: no cover
188188

189189
try:
190190
compile(async_so.content, "<string>", "exec")
191191
except SyntaxError as e: # pragma: no cover
192-
typer.echo(
192+
click.echo(
193193
f"Error in service {async_so.operation_id}: {e}"
194194
) # pragma: no cover
195195

tests/test_main.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""Test cases for the __main__ module."""
22
import pytest
3-
from typer.testing import CliRunner
3+
from click.testing import CliRunner
44

5-
from openapi_python_generator.__main__ import app
5+
from openapi_python_generator.__main__ import main
6+
from tests.conftest import test_data_path, test_result_path
67

78

89
@pytest.fixture
@@ -13,4 +14,5 @@ def runner() -> CliRunner:
1314

1415
def test_main_succeeds(runner: CliRunner, model_data_with_cleanup) -> None:
1516
"""It exits with a status code of zero."""
16-
runner.invoke(app, ["test_data/test_api.json", "test_result"])
17+
result = runner.invoke(main, [str(test_data_path), str(test_result_path)])
18+
assert result.exit_code == 0

0 commit comments

Comments
 (0)