Skip to content

Commit cfdac44

Browse files
committed
- Added flake settings file
- Added pre-commit settings file - Added mypy settings file - Fixed some code structuring
1 parent a82b921 commit cfdac44

26 files changed

+748
-286
lines changed

.coveragerc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
[run]
2-
omit = src/openapi_python_generator/language_converters/python/templates/*
2+
omit = src/openapi_python_generator/language_converters/python/templates/*
3+
[report]
4+
show_missing = true

.flake8

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[flake8]
2+
select = B,B9,BLK,C,E,F,I,W,S
3+
max-complexity = 15
4+
application-import-names = openapi_python_generator
5+
import-order-style = google
6+
ignore = E203,E501,W503
7+
max-line-length = 120
8+
per-file-ignores = tests/*:S101

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ name: Release
33
on:
44
release:
55
types: [published]
6-
6+
77
workflow_dispatch:
8-
8+
99
jobs:
1010
release:
1111
runs-on: ubuntu-latest

.pre-commit-config.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v2.3.0
4+
hooks:
5+
- id: check-yaml
6+
- id: end-of-file-fixer
7+
- id: trailing-whitespace
8+
- repo: local
9+
hooks:
10+
- id: black
11+
name: black
12+
entry: poetry run black
13+
language: system
14+
types: [python]
15+
- id: flake8
16+
name: flake8
17+
entry: poetry run flake8
18+
language: system
19+
types: [ python ]

mypy.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[mypy]
2+
3+
[mypy-nox.*,pytest]
4+
ignore_missing_imports = True
5+
6+
[mypy-openapi_schema_pydantic.*]
7+
ignore_missing_imports = True
8+
9+
[mypy-autopep8.*]
10+
ignore_missing_imports = True

noxfile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
raise SystemExit(dedent(message)) from None
2222

2323
package = "openapi_python_generator"
24-
python_versions = ["3.10", "3.9", "3.8", "3.7"]
24+
python_versions = ["3.8"]
2525
nox.needs_version = ">= 2021.6.6"
2626
nox.options.sessions = (
2727
"pre-commit",
@@ -94,8 +94,8 @@ def activate_virtualenv_in_precommit_hooks(session: Session) -> None:
9494
text = hook.read_text()
9595

9696
if not any(
97-
Path("A") == Path("a") and bindir.lower() in text.lower() or bindir in text
98-
for bindir in bindirs
97+
Path("A") == Path("a") and bindir.lower() in text.lower() or bindir in text
98+
for bindir in bindirs
9999
):
100100
continue
101101

src/openapi_python_generator/__main__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010

1111
@app.command()
12-
def main(file_name :str, output : str, library : Optional[HTTPLibrary] = HTTPLibrary.httpx) -> None:
12+
def main(
13+
file_name: str, output: str, library: Optional[HTTPLibrary] = HTTPLibrary.httpx
14+
) -> None:
1315
"""
1416
Generate Python code from an OpenAPI 3.0 specification.
1517
"""
1618
generate_data(file_name, output, library)
1719

1820

19-
20-
if __name__ == "__main__": #pragma: no cover
21+
if __name__ == "__main__": # pragma: no cover
2122
app()
22-
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from enum import Enum
22

3+
34
class HTTPLibrary(str, Enum):
45
"""
56
Enum for the available HTTP libraries.
67
"""
7-
httpx = 'httpx'
8+
9+
httpx = "httpx"

src/openapi_python_generator/generate_data.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import httpx
55
from httpx import ConnectError, ConnectTimeout
6-
from orjson import orjson, JSONDecodeError
6+
import orjson
77
import typer
88
from pydantic import ValidationError
99
import autopep8
@@ -21,34 +21,40 @@ def write_code(path: Path, content):
2121
:param path: The path to the file.
2222
:param content: The content to write.
2323
"""
24-
with open(path, 'w') as f:
24+
with open(path, "w") as f:
2525
f.write(autopep8.fix_code(content))
2626

2727

28-
def get_open_api(path: Union[str,Path]) -> OpenAPI:
28+
def get_open_api(path: 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.
3131
:param path:
3232
:return:
3333
"""
3434
try:
35-
if not isinstance(path,Path) and (path.startswith("http://") or path.startswith("https://")):
35+
if not isinstance(path, Path) and (
36+
path.startswith("http://") or path.startswith("https://")
37+
):
3638
return OpenAPI(**orjson.loads(httpx.get(path).text))
3739

38-
with open(path, 'r') as f:
40+
with open(path, "r") as f:
3941
return OpenAPI(**orjson.loads(f.read()))
4042
except FileNotFoundError:
41-
typer.echo(f"File {path} not found. Please make sure to pass the path to the OpenAPI 3.0 specification.")
43+
typer.echo(
44+
f"File {path} not found. Please make sure to pass the path to the OpenAPI 3.0 specification."
45+
)
4246
raise
43-
except (ConnectError,ConnectTimeout):
47+
except (ConnectError, ConnectTimeout):
4448
typer.echo(f"Could not connect to {path}.")
45-
raise ConnectError(f"Could not connect to {path}.")
46-
except (ValidationError,JSONDecodeError):
47-
typer.echo(f"File {path} is not a valid OpenAPI 3.0 specification, or there may be a problem with your JSON.")
49+
raise ConnectError(f"Could not connect to {path}.") from None
50+
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."
53+
)
4854
raise
4955

5056

51-
def write_data(data: ConversionResult, output: str):
57+
def write_data(data: ConversionResult, output: Union[str, Path]):
5258
"""
5359
This function will firstly create the folderstrucutre of output, if it doesn't exist. Then it will create the
5460
models from data.models into the models sub module of the output folder. After this, the services will be created
@@ -76,26 +82,42 @@ def write_data(data: ConversionResult, output: str):
7682
write_code(models_path / f"{model.file_name}.py", model.content)
7783

7884
# Create models.__init__.py file containing imports to all models.
79-
write_code(models_path / "__init__.py","\n".join([f'from {file} import *' for file in files]))
85+
write_code(
86+
models_path / "__init__.py",
87+
"\n".join([f"from {file} import *" for file in files]),
88+
)
8089

8190
files = []
8291

8392
# Write the services.
8493
for service in data.services:
8594
files.append(service.file_name)
86-
write_code(services_path / f"{service.file_name}.py", JINJA_ENV.get_template(SERVICE_TEMPLATE).render(**service.dict()))
95+
write_code(
96+
services_path / f"{service.file_name}.py",
97+
JINJA_ENV.get_template(SERVICE_TEMPLATE).render(**service.dict()),
98+
)
8799

88100
# Create services.__init__.py file containing imports to all services.
89-
write_code(services_path / "__init__.py", "\n".join([f'from {file} import *' for file in files]))
101+
write_code(
102+
services_path / "__init__.py",
103+
"\n".join([f"from {file} import *" for file in files]),
104+
)
90105

91106
# Write the api_config.py file.
92107
write_code(Path(output) / "api_config.py", data.api_config.content)
93108

94109
# Write the __init__.py file.
95-
write_code(Path(output) / "__init__.py", "from models import *\nfrom services import *\nfrom api_config import *")
110+
write_code(
111+
Path(output) / "__init__.py",
112+
"from models import *\nfrom services import *\nfrom api_config import *",
113+
)
96114

97115

98-
def generate_data(file_name: str, output: str, library: Optional[HTTPLibrary] = HTTPLibrary.httpx) -> None:
116+
def generate_data(
117+
file_name: Union[str, Path],
118+
output: Union[str, Path],
119+
library: Optional[HTTPLibrary] = HTTPLibrary.httpx,
120+
) -> None:
99121
"""
100122
Generate Python code from an OpenAPI 3.0 specification.
101123
"""

src/openapi_python_generator/language_converters/python/api_config_generator.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from openapi_schema_pydantic import OpenAPI
22

3-
from openapi_python_generator.language_converters.python.jinja_config import JINJA_ENV, API_CONFIG_TEMPLATE
3+
from openapi_python_generator.language_converters.python.jinja_config import (
4+
JINJA_ENV,
5+
API_CONFIG_TEMPLATE,
6+
)
47
from openapi_python_generator.models import APIConfig
58

69

0 commit comments

Comments
 (0)