openapi-spec-tools 0.22.0__tar.gz → 0.23.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/PKG-INFO +1 -1
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/api_gen/api_generator.py +4 -1
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/api_gen/flat_generator.py +1 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/api_gen/opaque_generator.py +1 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/api_gen/property_generator.py +1 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/__init__.py +2 -0
- openapi_spec_tools-0.23.0/openapi_spec_tools/base_gen/_constants.py +3 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/base_generator.py +63 -5
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/config.py +10 -0
- openapi_spec_tools-0.23.0/openapi_spec_tools/base_gen/exceptions.py +5 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/api_gen.py +2 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/cli_gen.py +3 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/utils.py +11 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/cli_generator.py +8 -5
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/pyproject.toml +1 -1
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/tests/cli_gen/test_cli_generator.py +11 -3
- openapi_spec_tools-0.23.0/tests/cli_gen/test_main.py +51 -0
- openapi_spec_tools-0.22.0/tests/cli_gen/test_main.py +0 -34
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/.gitignore +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/LICENSE +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/README.md +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/__init__.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/api_gen/__init__.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/api_gen/_environment.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/_logging.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/_requests.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/constants.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/files.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/utils.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/__init__.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/analyze.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/arguments.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/constants.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/content.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/history.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/layout.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/models.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/oas.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/operations.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/paths.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/tags.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/__init__.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/_arguments.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/_display.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/_exceptions.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/_tree.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/files.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/constants.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/expand.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/flatten.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/layout/__init__.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/layout/config.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/layout/layout_generator.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/layout/merge.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/layout/types.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/layout/utils.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/types.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/utils.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/tests/cli_gen/__init__.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/tests/cli_gen/test_display.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/tests/cli_gen/test_exceptions.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/tests/cli_gen/test_files.py +0 -0
- {openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/tests/cli_gen/test_tree.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: openapi-spec-tools
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.23.0
|
|
4
4
|
Summary: OpenAPI specification tools for analyzing, updating, and generating a CLI.
|
|
5
5
|
Project-URL: Homepage, https://github.com/rickwporter/openapi-spec-tools
|
|
6
6
|
Project-URL: Repository, https://github.com/rickwporter/openapi-spec-tools
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/api_gen/api_generator.py
RENAMED
|
@@ -49,6 +49,7 @@ class ApiGenerator(BaseGenerator, ABC):
|
|
|
49
49
|
api_gen = Path(__file__).parent
|
|
50
50
|
base_gen = Path(__file__).parent.parent / "base_gen"
|
|
51
51
|
return {
|
|
52
|
+
**super().default_infra_files(),
|
|
52
53
|
base_gen / "_logging.py": "_logging.py",
|
|
53
54
|
base_gen / "_requests.py": "_requests.py",
|
|
54
55
|
api_gen / "_environment.py": "_environment.py",
|
|
@@ -57,6 +58,7 @@ class ApiGenerator(BaseGenerator, ABC):
|
|
|
57
58
|
def default_infra_replacements(self) -> dict[str, str]:
|
|
58
59
|
"""Default infrastructure line replacements."""
|
|
59
60
|
return {
|
|
61
|
+
**super().default_infra_replacements(),
|
|
60
62
|
"openapi_spec_tools.api_gen": self.package_name,
|
|
61
63
|
"openapi_spec_tools.base_gen": self.package_name,
|
|
62
64
|
__package__: self.package_name,
|
|
@@ -104,6 +106,7 @@ from datetime import datetime # noqa: F401
|
|
|
104
106
|
from enum import Enum # noqa: F401
|
|
105
107
|
from typing import Any
|
|
106
108
|
|
|
109
|
+
from {self.package_name} import _constants as _c
|
|
107
110
|
from {self.package_name} import _environment as _e
|
|
108
111
|
from {self.package_name} import _logging as _l
|
|
109
112
|
from {self.package_name} import _requests as _r
|
|
@@ -113,7 +116,7 @@ from {self.package_name} import _requests as _r
|
|
|
113
116
|
"""Provide initialization of standard arguments inside body."""
|
|
114
117
|
host_default = (
|
|
115
118
|
f'_e.env_string({maybe_quoted(self.env_host)}, '
|
|
116
|
-
f'default=
|
|
119
|
+
f'default=_c.DEFAULT_HOST, except_missing=True)'
|
|
117
120
|
)
|
|
118
121
|
key_default = f'_e.env_string({maybe_quoted(self.env_key)}, except_missing=True)'
|
|
119
122
|
timeout_default = f'_e.env_int({maybe_quoted(self.env_timeout)}, default={self.default_timeout})'
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/api_gen/flat_generator.py
RENAMED
|
@@ -63,6 +63,7 @@ class FlatApiGenerator(ApiGenerator):
|
|
|
63
63
|
args_str = SEP1 + SEP1.join(func_args) + NL
|
|
64
64
|
|
|
65
65
|
logger.debug(f"{func_name}({len(path_params)} path, {len(query_params)} query, {len(body_params)} body)")
|
|
66
|
+
self.check_duplicates(node.identifier, func_args)
|
|
66
67
|
|
|
67
68
|
user_header_init = ""
|
|
68
69
|
user_header_arg = ""
|
|
@@ -81,6 +81,7 @@ class OpaqueApiGenerator(ApiGenerator):
|
|
|
81
81
|
args_str = SEP1 + SEP1.join(func_args) + NL
|
|
82
82
|
|
|
83
83
|
logger.debug(f"{func_name}({len(path_params)} path, {len(query_params)} query")
|
|
84
|
+
self.check_duplicates(node.identifier, func_args)
|
|
84
85
|
|
|
85
86
|
user_header_init = ""
|
|
86
87
|
user_header_arg = ""
|
|
@@ -117,6 +117,7 @@ class PropertyApiGenerator(ApiGenerator):
|
|
|
117
117
|
args_str = SEP1 + SEP1.join(func_args) + NL
|
|
118
118
|
|
|
119
119
|
logger.debug(f"{func_name}({len(path_params)} path, {len(query_params)} query")
|
|
120
|
+
self.check_duplicates(node.identifier, func_args)
|
|
120
121
|
|
|
121
122
|
user_header_init = ""
|
|
122
123
|
user_header_arg = ""
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/__init__.py
RENAMED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
"""Base generator which is the base class for API and CLI generoators."""
|
|
2
2
|
from .base_generator import BaseGenerator
|
|
3
|
+
from .config import ErrorHandling
|
|
3
4
|
from .config import GeneratorConfig
|
|
4
5
|
from .constants import COLLECTIONS
|
|
5
6
|
from .constants import NL
|
|
6
7
|
from .constants import SEP1
|
|
7
8
|
from .constants import SEP2
|
|
9
|
+
from .exceptions import CodeGeneration
|
|
8
10
|
from .utils import is_case_sensitive
|
|
9
11
|
from .utils import maybe_quoted
|
|
10
12
|
from .utils import prepend
|
|
@@ -4,6 +4,7 @@ from copy import deepcopy
|
|
|
4
4
|
from pathlib import Path
|
|
5
5
|
from typing import Any
|
|
6
6
|
|
|
7
|
+
from openapi_spec_tools.base_gen.config import ErrorHandling
|
|
7
8
|
from openapi_spec_tools.base_gen.config import GeneratorConfig
|
|
8
9
|
from openapi_spec_tools.base_gen.constants import COLLECTIONS
|
|
9
10
|
from openapi_spec_tools.base_gen.constants import DEFAULT_CONFLICT_SUFFIX
|
|
@@ -20,6 +21,7 @@ from openapi_spec_tools.base_gen.constants import DEFAULT_VAR_TIMEOUT
|
|
|
20
21
|
from openapi_spec_tools.base_gen.constants import NL
|
|
21
22
|
from openapi_spec_tools.base_gen.constants import SEP1
|
|
22
23
|
from openapi_spec_tools.base_gen.constants import SEP2
|
|
24
|
+
from openapi_spec_tools.base_gen.exceptions import CodeGeneration
|
|
23
25
|
from openapi_spec_tools.base_gen.files import copy_and_update
|
|
24
26
|
from openapi_spec_tools.base_gen.utils import maybe_quoted
|
|
25
27
|
from openapi_spec_tools.base_gen.utils import quoted
|
|
@@ -70,6 +72,7 @@ class BaseGenerator:
|
|
|
70
72
|
default_host: str | None = None,
|
|
71
73
|
default_log_level: str | None = None,
|
|
72
74
|
default_timeout: int | None = None,
|
|
75
|
+
error_handling: ErrorHandling | None = None,
|
|
73
76
|
):
|
|
74
77
|
"""Initialize with the OpenAPI spec and other data for generating multiple modules.
|
|
75
78
|
|
|
@@ -95,6 +98,7 @@ class BaseGenerator:
|
|
|
95
98
|
* default_host - empty string
|
|
96
99
|
* default_log_level - DEFAULT_VALUE_LOG_LEVEL
|
|
97
100
|
* default_timeout - DEFAULT_VALUE_TIMEOUT
|
|
101
|
+
* error_handling - ErrorHandling.DEFER
|
|
98
102
|
"""
|
|
99
103
|
config = config or GeneratorConfig() # reduce logic by creating a default (if no config provided)
|
|
100
104
|
self.operations = map_operations(oas.get(OasField.PATHS, {}))
|
|
@@ -117,6 +121,8 @@ class BaseGenerator:
|
|
|
117
121
|
self.env_log_level = _or(env_log_level, config.env_log_level, DEFAULT_VAR_LOG_LEVEL)
|
|
118
122
|
self.default_log = _or(default_log_level, config.default_log_level, DEFAULT_VALUE_LOG_LEVEL)
|
|
119
123
|
self.default_timeout = _or(default_timeout, config.default_timeout, DEFAULT_VALUE_TIMEOUT)
|
|
124
|
+
self.error_handling = _or(error_handling, config.error_handling, ErrorHandling.DEFER)
|
|
125
|
+
self.errors: list[str] = []
|
|
120
126
|
|
|
121
127
|
if not self.default_host:
|
|
122
128
|
servers = oas.get(OasField.SERVERS)
|
|
@@ -142,11 +148,20 @@ class BaseGenerator:
|
|
|
142
148
|
|
|
143
149
|
def default_infra_files(self) -> dict[Path, str]:
|
|
144
150
|
"""Default map of infrastructure files to copy."""
|
|
145
|
-
|
|
151
|
+
base_gen = Path(__file__).parent
|
|
152
|
+
return {
|
|
153
|
+
base_gen / "_constants.py": "_constants.py",
|
|
154
|
+
}
|
|
146
155
|
|
|
147
156
|
def default_infra_replacements(self) -> dict[str, str]:
|
|
148
157
|
"""Default map of infrastructure file line replacements."""
|
|
149
|
-
return
|
|
158
|
+
return self._constants_replacements()
|
|
159
|
+
|
|
160
|
+
def _constants_replacements(self) -> dict[str, str]:
|
|
161
|
+
"""Assign default_host when copying _constants.py."""
|
|
162
|
+
return {
|
|
163
|
+
'DEFAULT_HOST = ""': f'DEFAULT_HOST = {quoted(self.default_host or "")}',
|
|
164
|
+
}
|
|
150
165
|
|
|
151
166
|
def default_test_files(self) -> dict[Path, str]:
|
|
152
167
|
"""Default map of test files to copy."""
|
|
@@ -220,7 +235,9 @@ class BaseGenerator:
|
|
|
220
235
|
if not line:
|
|
221
236
|
result += NL
|
|
222
237
|
else:
|
|
223
|
-
|
|
238
|
+
# NOTE: SIMPLE_TRANSLATIONS would escape all the quotes, and we don't want that
|
|
239
|
+
translations = str.maketrans({"\\": r"\\"})
|
|
240
|
+
_line = line.translate(translations).replace("'''", r"\'''") # avoid closing docstring
|
|
224
241
|
inner = textwrap.wrap(_line, width=self.max_help_length, replace_whitespace=False)
|
|
225
242
|
result += SEP1 + SEP1.join(inner)
|
|
226
243
|
result += f"{SEP1}'''{SEP1}"
|
|
@@ -536,6 +553,8 @@ class BaseGenerator:
|
|
|
536
553
|
lines = ["body = {}"]
|
|
537
554
|
found = set()
|
|
538
555
|
forced_single = set()
|
|
556
|
+
# The body schema itself is a list of one complex item.
|
|
557
|
+
body_forced_single = False
|
|
539
558
|
lineage = []
|
|
540
559
|
for prop_data in body_params.values():
|
|
541
560
|
parents = prop_data.get(OasField.X_PARENTS, [])
|
|
@@ -543,7 +562,10 @@ class BaseGenerator:
|
|
|
543
562
|
lineage.append(parents)
|
|
544
563
|
|
|
545
564
|
if prop_data.get(OasField.X_SINGLE):
|
|
546
|
-
|
|
565
|
+
if parents:
|
|
566
|
+
forced_single.add(parents[-1])
|
|
567
|
+
else:
|
|
568
|
+
body_forced_single = True
|
|
547
569
|
|
|
548
570
|
for parent in parents:
|
|
549
571
|
if parent not in found:
|
|
@@ -605,6 +627,10 @@ class BaseGenerator:
|
|
|
605
627
|
for r in removal:
|
|
606
628
|
depends.pop(r)
|
|
607
629
|
|
|
630
|
+
if body_forced_single:
|
|
631
|
+
lines.append("if body:")
|
|
632
|
+
lines.append(" body = [body] # convert to list")
|
|
633
|
+
|
|
608
634
|
return SEP1 + SEP1.join(lines)
|
|
609
635
|
|
|
610
636
|
def clean_enum_name(self, value: str) -> bool:
|
|
@@ -749,7 +775,10 @@ class BaseGenerator:
|
|
|
749
775
|
)
|
|
750
776
|
for src, dst in files.items():
|
|
751
777
|
dfile = dpath / dst
|
|
752
|
-
|
|
778
|
+
file_replacements = replacements
|
|
779
|
+
if Path(dst).name == "_constants.py":
|
|
780
|
+
file_replacements = {**replacements, **self._constants_replacements()}
|
|
781
|
+
copy_and_update(src.as_posix(), dfile.as_posix(), file_replacements, self.copyright)
|
|
753
782
|
|
|
754
783
|
def copy_test_files(self, dst_dir: str) -> None:
|
|
755
784
|
"""Iterate over the test_files and copy from local to dst."""
|
|
@@ -775,3 +804,32 @@ class BaseGenerator:
|
|
|
775
804
|
return
|
|
776
805
|
|
|
777
806
|
dfile.write_text(content, encoding="utf-8", newline="\n")
|
|
807
|
+
|
|
808
|
+
def add_error(self, message: str) -> None:
|
|
809
|
+
"""Log a generation error and apply the configured error handling."""
|
|
810
|
+
logger.error(message)
|
|
811
|
+
|
|
812
|
+
if self.error_handling == ErrorHandling.IGNORE:
|
|
813
|
+
return
|
|
814
|
+
|
|
815
|
+
if self.error_handling == ErrorHandling.IMMEDIATE:
|
|
816
|
+
raise CodeGeneration(message)
|
|
817
|
+
|
|
818
|
+
self.errors.append(message)
|
|
819
|
+
|
|
820
|
+
def check_duplicates(self, op_id: str, arguments: list[str]) -> None:
|
|
821
|
+
"""Record duplicate argument names for one generated function.
|
|
822
|
+
|
|
823
|
+
Every mode logs the error. Ignore then continues. Immediate raises
|
|
824
|
+
CodeGeneration. Defer stores the message for the caller to report.
|
|
825
|
+
"""
|
|
826
|
+
names = [
|
|
827
|
+
line.split(":", 1)[0].strip()
|
|
828
|
+
for line in arguments
|
|
829
|
+
]
|
|
830
|
+
duplicates = sorted({name for name in names if names.count(name) > 1})
|
|
831
|
+
if not duplicates:
|
|
832
|
+
return
|
|
833
|
+
|
|
834
|
+
message = f"Duplicate arguments for {op_id}: {', '.join(duplicates)}"
|
|
835
|
+
self.add_error(message)
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/config.py
RENAMED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""Configuration data used to construct a base generator."""
|
|
2
2
|
from __future__ import annotations
|
|
3
3
|
|
|
4
|
+
from enum import Enum
|
|
4
5
|
from pathlib import Path
|
|
5
6
|
|
|
6
7
|
import yaml
|
|
@@ -9,6 +10,14 @@ from pydantic import BaseModel
|
|
|
9
10
|
from openapi_spec_tools.types import ContentType
|
|
10
11
|
|
|
11
12
|
|
|
13
|
+
class ErrorHandling(str, Enum):
|
|
14
|
+
"""How generation treats errors such as duplicate arguments."""
|
|
15
|
+
|
|
16
|
+
IGNORE = "ignore"
|
|
17
|
+
DEFER = "defer"
|
|
18
|
+
IMMEDIATE = "immediate"
|
|
19
|
+
|
|
20
|
+
|
|
12
21
|
class GeneratorConfig(BaseModel):
|
|
13
22
|
"""Data elements accepted by the BaseGenerator initializer.
|
|
14
23
|
|
|
@@ -33,6 +42,7 @@ class GeneratorConfig(BaseModel):
|
|
|
33
42
|
default_host: str | None = None
|
|
34
43
|
default_log_level: str | None = None
|
|
35
44
|
default_timeout: int | None = None
|
|
45
|
+
error_handling: ErrorHandling | None = None
|
|
36
46
|
|
|
37
47
|
@classmethod
|
|
38
48
|
def from_yaml(cls, filename: str | Path) -> GeneratorConfig:
|
|
@@ -17,6 +17,7 @@ from openapi_spec_tools.cli.arguments import OpenApiFilenameArgument
|
|
|
17
17
|
from openapi_spec_tools.cli.arguments import PackageNameOption
|
|
18
18
|
from openapi_spec_tools.cli.arguments import PathPrefixOption
|
|
19
19
|
from openapi_spec_tools.cli.arguments import StartPointOption
|
|
20
|
+
from openapi_spec_tools.cli.utils import check_errors
|
|
20
21
|
from openapi_spec_tools.cli.utils import gen_config_from_file
|
|
21
22
|
from openapi_spec_tools.cli.utils import init_logging
|
|
22
23
|
from openapi_spec_tools.cli.utils import layout_tree_with_error_handling
|
|
@@ -90,6 +91,7 @@ def generate_api(
|
|
|
90
91
|
generator.add_init_file(code_dir)
|
|
91
92
|
generator.copy_infrastructure_files(code_dir)
|
|
92
93
|
generator.generate_files(commands, code_dir)
|
|
94
|
+
check_errors(generator)
|
|
93
95
|
|
|
94
96
|
typer.echo("Generated API files")
|
|
95
97
|
|
|
@@ -22,6 +22,7 @@ from openapi_spec_tools.cli.arguments import StartPointOption
|
|
|
22
22
|
from openapi_spec_tools.cli.arguments import UpdatedOpenApiFilenameOption
|
|
23
23
|
from openapi_spec_tools.cli.constants import DIRECTORY
|
|
24
24
|
from openapi_spec_tools.cli.constants import SEP
|
|
25
|
+
from openapi_spec_tools.cli.utils import check_errors
|
|
25
26
|
from openapi_spec_tools.cli.utils import gen_config_from_file
|
|
26
27
|
from openapi_spec_tools.cli.utils import init_logging
|
|
27
28
|
from openapi_spec_tools.cli.utils import layout_tree_with_error_handling
|
|
@@ -135,6 +136,7 @@ def generate_cli(
|
|
|
135
136
|
generator.add_init_file(code_dir)
|
|
136
137
|
generator.copy_infrastructure_files(code_dir)
|
|
137
138
|
generator.generate_files(commands, code_dir)
|
|
139
|
+
check_errors(generator)
|
|
138
140
|
|
|
139
141
|
if include_tests:
|
|
140
142
|
os.makedirs(test_dir, exist_ok=True)
|
|
@@ -295,6 +297,7 @@ def show_cli_tree(
|
|
|
295
297
|
generator = CliGenerator(package_name="", oas=oas)
|
|
296
298
|
|
|
297
299
|
tree = generate_tree_node(generator, layout)
|
|
300
|
+
check_errors(generator)
|
|
298
301
|
if not tree.children:
|
|
299
302
|
typer.echo("No operations or sub-commands found")
|
|
300
303
|
return
|
|
@@ -11,6 +11,7 @@ import typer
|
|
|
11
11
|
from rich import print
|
|
12
12
|
from rich.console import Console
|
|
13
13
|
|
|
14
|
+
from openapi_spec_tools.base_gen import BaseGenerator
|
|
14
15
|
from openapi_spec_tools.base_gen import GeneratorConfig
|
|
15
16
|
from openapi_spec_tools.cli.arguments import ExpansionType
|
|
16
17
|
from openapi_spec_tools.constants import LOG_CLASS
|
|
@@ -57,6 +58,16 @@ def error_out(message: str, exit_code: int = 1) -> None:
|
|
|
57
58
|
raise typer.Exit(exit_code)
|
|
58
59
|
|
|
59
60
|
|
|
61
|
+
def check_errors(generator: BaseGenerator) -> None:
|
|
62
|
+
"""Print collected generation errors and exit when any are present."""
|
|
63
|
+
if not generator.errors:
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
for message in generator.errors:
|
|
67
|
+
typer.echo(f"ERROR: {message}")
|
|
68
|
+
raise typer.Exit(1)
|
|
69
|
+
|
|
70
|
+
|
|
60
71
|
def open_oas_with_error_handling(filename: str) -> Any:
|
|
61
72
|
"""Perform error handling around opening an OpenAPI spec.
|
|
62
73
|
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/cli_generator.py
RENAMED
|
@@ -36,6 +36,7 @@ class CliGenerator(BaseGenerator):
|
|
|
36
36
|
base_gen = Path(__file__).parent.parent / "base_gen"
|
|
37
37
|
cli_gen = Path(__file__).parent
|
|
38
38
|
return {
|
|
39
|
+
**super().default_infra_files(),
|
|
39
40
|
base_gen / "_logging.py": "_logging.py",
|
|
40
41
|
base_gen / "_requests.py": "_requests.py",
|
|
41
42
|
cli_gen / "_arguments.py": "_arguments.py",
|
|
@@ -47,6 +48,7 @@ class CliGenerator(BaseGenerator):
|
|
|
47
48
|
def default_infra_replacements(self) -> dict[str, str]:
|
|
48
49
|
"""Default infrastructure line replacements."""
|
|
49
50
|
return {
|
|
51
|
+
**super().default_infra_replacements(),
|
|
50
52
|
"openapi_spec_tools.base_gen": self.package_name,
|
|
51
53
|
__package__: self.package_name,
|
|
52
54
|
# NOTE: these will only really impact _arguments.py -- use same mechanism for simplicity
|
|
@@ -101,10 +103,10 @@ from pathlib import Path
|
|
|
101
103
|
text += "from rich_objects import display" + NL
|
|
102
104
|
|
|
103
105
|
# local
|
|
104
|
-
text += f""
|
|
105
|
-
|
|
106
|
-
from {self.package_name} import
|
|
107
|
-
""
|
|
106
|
+
text += f"\nfrom {self.package_name} import _arguments as _a\n"
|
|
107
|
+
if operations:
|
|
108
|
+
text += f"from {self.package_name} import _constants as _c\n"
|
|
109
|
+
text += f"from {self.package_name} import _display as _d # noqa: F401\n"
|
|
108
110
|
if operations:
|
|
109
111
|
text += f"""\
|
|
110
112
|
from {self.package_name} import _exceptions as _e
|
|
@@ -158,7 +160,7 @@ if __name__ == "__main__":
|
|
|
158
160
|
def command_infra_arguments(self, command: LayoutNode) -> list[str]:
|
|
159
161
|
"""Get the standard CLI function arguments to the command."""
|
|
160
162
|
args = [
|
|
161
|
-
|
|
163
|
+
'_api_host: _a.ApiHostOption = _c.DEFAULT_HOST',
|
|
162
164
|
'_api_key: _a.ApiKeyOption = None',
|
|
163
165
|
f'_api_timeout: _a.ApiTimeoutOption = {self.default_timeout}',
|
|
164
166
|
f'_log_level: _a.LogLevelOption = _a.LogLevel.{self.default_log.upper()}',
|
|
@@ -432,6 +434,7 @@ if __name__ == "__main__":
|
|
|
432
434
|
|
|
433
435
|
command_args.append(f'short_help="{self.op_short_help(op)}"')
|
|
434
436
|
logger.debug(f"{func_name}({len(path_params)} path, {len(query_params)} query, {len(body_params)} body)")
|
|
437
|
+
self.check_duplicates(node.identifier, func_args)
|
|
435
438
|
|
|
436
439
|
hardcoded_values = self.initialize_hardcoded(node.hardcoded)
|
|
437
440
|
user_header_init = ""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "openapi-spec-tools"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.23.0"
|
|
4
4
|
description = "OpenAPI specification tools for analyzing, updating, and generating a CLI."
|
|
5
5
|
authors = [{ name = "Rick Porter", email = "rickwporter@gmail.com" }]
|
|
6
6
|
requires-python = ">=3.10,<4"
|
|
@@ -25,11 +25,13 @@ def test_standard_imports():
|
|
|
25
25
|
assert "import typer" in text
|
|
26
26
|
assert annotated in text
|
|
27
27
|
assert requests in text
|
|
28
|
+
assert "from cli_package import _constants as _c" in text
|
|
28
29
|
|
|
29
30
|
text = uut.standard_imports(operations=False)
|
|
30
31
|
assert "import typer" in text
|
|
31
32
|
assert annotated not in text
|
|
32
33
|
assert requests not in text
|
|
34
|
+
assert "_constants" not in text
|
|
33
35
|
|
|
34
36
|
|
|
35
37
|
def test_subcommand_imports():
|
|
@@ -369,8 +371,9 @@ def test_op_infra_arguments(command, has_details):
|
|
|
369
371
|
else:
|
|
370
372
|
assert details_option not in text
|
|
371
373
|
|
|
372
|
-
#
|
|
373
|
-
assert '=
|
|
374
|
+
# the host default comes from the generated constants module
|
|
375
|
+
assert '= _c.DEFAULT_HOST' in text
|
|
376
|
+
assert uut.default_host == "http://petstore.swagger.io/v1"
|
|
374
377
|
|
|
375
378
|
|
|
376
379
|
def test_op_check_missing():
|
|
@@ -503,6 +506,7 @@ def test_function_definition_forced_single():
|
|
|
503
506
|
assert 'attachments["id"] = attachments_id' in text
|
|
504
507
|
assert 'attachments["edgeColor"] = attachments_edge_color' in text
|
|
505
508
|
assert 'body["attachments"] = [attachments] # convert to list' in text
|
|
509
|
+
assert 'body = [body] # convert to list' not in text
|
|
506
510
|
|
|
507
511
|
# check the body of the function
|
|
508
512
|
assert "_l.init_logging(_log_level)" in text
|
|
@@ -780,8 +784,12 @@ def test_env_vars():
|
|
|
780
784
|
assert 'ENV_API_TIME = "TIMEOUT"' in content
|
|
781
785
|
assert 'ENV_LOG_LEVEL = "LEVEL"' in content
|
|
782
786
|
|
|
787
|
+
content = (directory / "_constants.py").read_text()
|
|
788
|
+
assert 'DEFAULT_HOST = "random-url"' in content
|
|
789
|
+
|
|
783
790
|
# check default values are updated
|
|
784
791
|
content = (directory / "main.py").read_text()
|
|
785
|
-
assert
|
|
792
|
+
assert "from pets import _constants as _c" in content
|
|
793
|
+
assert '_api_host: _a.ApiHostOption = _c.DEFAULT_HOST' in content
|
|
786
794
|
assert '_api_timeout: _a.ApiTimeoutOption = 10' in content
|
|
787
795
|
assert '_log_level: _a.LogLevelOption = _a.LogLevel.DEBUG' in content
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from unittest import mock
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
import typer
|
|
6
|
+
from rich_objects import to_ascii
|
|
7
|
+
from typer.testing import CliRunner
|
|
8
|
+
|
|
9
|
+
from tests.assets.arg_test import app as program
|
|
10
|
+
|
|
11
|
+
MAX_WIDTH = 1000
|
|
12
|
+
|
|
13
|
+
@pytest.fixture
|
|
14
|
+
def terminal_width_env():
|
|
15
|
+
env = {
|
|
16
|
+
"TERMINAL_WIDTH": str(MAX_WIDTH),
|
|
17
|
+
}
|
|
18
|
+
with mock.patch.dict(os.environ, env, clear=True):
|
|
19
|
+
yield
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.mark.usefixtures("terminal_width_env")
|
|
23
|
+
def test_main_help():
|
|
24
|
+
runner = CliRunner(charset="ascii")
|
|
25
|
+
app = typer.Typer()
|
|
26
|
+
app.add_typer(program)
|
|
27
|
+
result = runner.invoke(app, ["--help"], terminal_width=MAX_WIDTH)
|
|
28
|
+
assert result.exit_code == 0
|
|
29
|
+
help = to_ascii(result.stdout)
|
|
30
|
+
assert "--install-completion" in help
|
|
31
|
+
assert "--show-completion" in help
|
|
32
|
+
# NOTE: this is not updated with the "actual" parent name, so it is shorter than would be nice
|
|
33
|
+
assert "Display commands tree for " in help
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@pytest.mark.usefixtures("terminal_width_env")
|
|
37
|
+
def test_main_commands():
|
|
38
|
+
runner = CliRunner(charset="ascii")
|
|
39
|
+
app = typer.Typer()
|
|
40
|
+
app.add_typer(program)
|
|
41
|
+
result = runner.invoke(app, ["commands", "--help"], terminal_width=MAX_WIDTH)
|
|
42
|
+
|
|
43
|
+
assert result.exit_code == 0
|
|
44
|
+
help = to_ascii(result.stdout)
|
|
45
|
+
assert "Details of the CLI" in help
|
|
46
|
+
assert "--max-depth" in help
|
|
47
|
+
|
|
48
|
+
result = runner.invoke(app, ["commands", "--depth", 0], terminal_width=MAX_WIDTH)
|
|
49
|
+
assert result.exit_code == 0
|
|
50
|
+
text = to_ascii(result.stdout)
|
|
51
|
+
assert "Command Tree" in text
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import typer
|
|
2
|
-
from rich_objects import to_ascii
|
|
3
|
-
from typer.testing import CliRunner
|
|
4
|
-
|
|
5
|
-
from tests.assets.arg_test import app as program
|
|
6
|
-
|
|
7
|
-
runner = CliRunner(charset="ascii")
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def test_main_help():
|
|
11
|
-
app = typer.Typer()
|
|
12
|
-
app.add_typer(program)
|
|
13
|
-
result = runner.invoke(app, ["--help"])
|
|
14
|
-
assert result.exit_code == 0
|
|
15
|
-
help = to_ascii(result.stdout)
|
|
16
|
-
# assert "--install-completion" in help
|
|
17
|
-
# assert "--show-completion" in help
|
|
18
|
-
# NOTE: this is not updated with the "actual" parent name, so it is shorter than would be nice
|
|
19
|
-
assert "Display commands tree for " in help
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def test_main_commands():
|
|
23
|
-
app = typer.Typer()
|
|
24
|
-
app.add_typer(program)
|
|
25
|
-
result = runner.invoke(app, ["commands", "--help"])
|
|
26
|
-
assert result.exit_code == 0
|
|
27
|
-
help = to_ascii(result.stdout)
|
|
28
|
-
assert "Details of the CLI" in help
|
|
29
|
-
# assert "--max-depth" in help
|
|
30
|
-
|
|
31
|
-
result = runner.invoke(app, ["commands", "--depth", 1])
|
|
32
|
-
assert result.exit_code == 0
|
|
33
|
-
text = to_ascii(result.stdout)
|
|
34
|
-
assert "Command Tree" in text
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/api_gen/__init__.py
RENAMED
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/api_gen/_environment.py
RENAMED
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/_logging.py
RENAMED
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/_requests.py
RENAMED
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/constants.py
RENAMED
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/files.py
RENAMED
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/base_gen/utils.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli/operations.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/__init__.py
RENAMED
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/_arguments.py
RENAMED
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/_display.py
RENAMED
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/cli_gen/_exceptions.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{openapi_spec_tools-0.22.0 → openapi_spec_tools-0.23.0}/openapi_spec_tools/layout/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|