openapi-spec-tools 0.20.0__tar.gz → 0.22.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.
Files changed (60) hide show
  1. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/PKG-INFO +7 -5
  2. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/README.md +2 -0
  3. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/__init__.py +4 -0
  4. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/api_gen/api_generator.py +8 -5
  5. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/api_gen/flat_generator.py +4 -1
  6. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/api_gen/opaque_generator.py +4 -1
  7. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/api_gen/property_generator.py +28 -54
  8. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/base_gen/_logging.py +6 -4
  9. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/base_gen/base_generator.py +45 -322
  10. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/base_gen/constants.py +33 -0
  11. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/api_gen.py +4 -7
  12. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/arguments.py +4 -2
  13. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/cli_gen.py +24 -27
  14. openapi_spec_tools-0.22.0/openapi_spec_tools/cli/constants.py +6 -0
  15. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/content.py +2 -4
  16. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/layout.py +14 -16
  17. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/models.py +14 -15
  18. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/oas.py +10 -13
  19. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/operations.py +13 -15
  20. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/paths.py +9 -10
  21. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/tags.py +11 -11
  22. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/utils.py +11 -13
  23. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli_gen/cli_generator.py +7 -4
  24. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/constants.py +2 -0
  25. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/expand.py +27 -8
  26. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/flatten.py +154 -23
  27. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/layout/layout_generator.py +11 -6
  28. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/types.py +2 -0
  29. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/utils.py +111 -3
  30. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/pyproject.toml +5 -5
  31. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/tests/cli_gen/test_cli_generator.py +12 -25
  32. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/tests/cli_gen/test_files.py +1 -1
  33. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/.gitignore +0 -0
  34. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/LICENSE +0 -0
  35. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/api_gen/__init__.py +0 -0
  36. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/api_gen/_environment.py +0 -0
  37. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/base_gen/__init__.py +0 -0
  38. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/base_gen/_requests.py +0 -0
  39. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/base_gen/config.py +0 -0
  40. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/base_gen/files.py +0 -0
  41. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/base_gen/utils.py +0 -0
  42. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/__init__.py +0 -0
  43. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/analyze.py +0 -0
  44. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli/history.py +0 -0
  45. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli_gen/__init__.py +0 -0
  46. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli_gen/_arguments.py +0 -0
  47. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli_gen/_display.py +0 -0
  48. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli_gen/_exceptions.py +0 -0
  49. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli_gen/_tree.py +0 -0
  50. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/cli_gen/files.py +0 -0
  51. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/layout/__init__.py +0 -0
  52. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/layout/config.py +0 -0
  53. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/layout/merge.py +0 -0
  54. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/layout/types.py +0 -0
  55. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/openapi_spec_tools/layout/utils.py +0 -0
  56. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/tests/cli_gen/__init__.py +0 -0
  57. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/tests/cli_gen/test_display.py +0 -0
  58. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/tests/cli_gen/test_exceptions.py +0 -0
  59. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.0}/tests/cli_gen/test_main.py +0 -0
  60. {openapi_spec_tools-0.20.0 → openapi_spec_tools-0.22.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.20.0
3
+ Version: 0.22.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
@@ -25,15 +25,17 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
25
25
  Classifier: Topic :: Utilities
26
26
  Classifier: Typing :: Typed
27
27
  Requires-Python: <4,>=3.10
28
- Requires-Dist: gitpython>=3.2.0
29
- Requires-Dist: pydantic<3,>=2.13.3
28
+ Requires-Dist: gitpython>=3.2.1
29
+ Requires-Dist: pydantic<3,>=2.14.3
30
30
  Requires-Dist: pyyaml>=6.0.3
31
31
  Requires-Dist: requests>=2.34.2
32
- Requires-Dist: rich-objects>=0.3.1
32
+ Requires-Dist: rich-objects>=0.4.0
33
33
  Requires-Dist: rich>=15.0.0
34
- Requires-Dist: typer>=0.27.2
34
+ Requires-Dist: typer>=0.29.1
35
35
  Description-Content-Type: text/markdown
36
36
 
37
+ [![codecov](https://codecov.io/gh/rickwporter/openapi-spec-tools/branch/main/graph/badge.svg)](https://codecov.io/gh/rickwporter/openapi-spec-tools)
38
+
37
39
  # openapi-spec-tools
38
40
 
39
41
  This is a collection of tools for using OpenAPI specifications. The OpenAPI community has a plethora of tools, and this is intended to supplement those. The tools here provide functionality that has not been readily available elsewhere.
@@ -1,3 +1,5 @@
1
+ [![codecov](https://codecov.io/gh/rickwporter/openapi-spec-tools/branch/main/graph/badge.svg)](https://codecov.io/gh/rickwporter/openapi-spec-tools)
2
+
1
3
  # openapi-spec-tools
2
4
 
3
5
  This is a collection of tools for using OpenAPI specifications. The OpenAPI community has a plethora of tools, and this is intended to supplement those. The tools here provide functionality that has not been readily available elsewhere.
@@ -1,7 +1,11 @@
1
1
  """Implementation for several utilities for analyzing and modifying OpenAPI specifications."""
2
2
  from openapi_spec_tools.expand import expand_schema
3
3
  from openapi_spec_tools.expand import merge_schema
4
+ from openapi_spec_tools.flatten import apply_any_of
5
+ from openapi_spec_tools.flatten import apply_one_of
6
+ from openapi_spec_tools.flatten import flatten_collection
4
7
  from openapi_spec_tools.flatten import flatten_schema
8
+ from openapi_spec_tools.flatten import is_simple_type
5
9
  from openapi_spec_tools.utils import count_values
6
10
  from openapi_spec_tools.utils import find_diffs
7
11
  from openapi_spec_tools.utils import find_paths
@@ -18,10 +18,13 @@ from openapi_spec_tools.base_gen import simple_escape
18
18
  from openapi_spec_tools.base_gen import to_snake_case
19
19
  from openapi_spec_tools.layout import LayoutNode
20
20
  from openapi_spec_tools.types import OasField
21
+ from openapi_spec_tools.utils import get_logger
21
22
 
22
23
  DEFAULT_VAR_LOG_LEVEL = "API_LOG_LEVEL"
23
24
  DEFAULT_VALUE_LOG_LEVEL = "info"
24
25
 
26
+ logger = get_logger(__name__)
27
+
25
28
 
26
29
  class ApiGenerator(BaseGenerator, ABC):
27
30
  """Provides the majority of the CLI generation functions.
@@ -62,17 +65,17 @@ class ApiGenerator(BaseGenerator, ABC):
62
65
  def property_help(self, prop: dict[str, Any]) -> str:
63
66
  """Get the short help string for the specified property."""
64
67
  help = prop.get(OasField.SUMMARY) or prop.get(OasField.DESCRIPTION) or ""
65
- short_ref = prop.get(OasField.X_REF)
68
+ short_ref = prop.get(OasField.X_ENUM) or prop.get(OasField.X_REF)
66
69
  choices = prop.get(OasField.ENUM)
67
70
  required = prop.get(OasField.REQUIRED)
68
71
 
69
72
  if help:
70
73
  if len(help) > self.max_help_length:
71
74
  help = help.split(". ")[0].strip()[:self.max_help_length] + '...'
72
- elif short_ref:
73
- help = f"see {short_ref} for info"
74
75
  elif choices:
75
76
  help = f"choices: {', '.join([str(_) for _ in choices])}"
77
+ elif short_ref:
78
+ help = f"see {short_ref} for info"
76
79
 
77
80
  if required and "require" not in help.lower():
78
81
  if not help.endswith(" "):
@@ -153,7 +156,7 @@ from {self.package_name} import _requests as _r
153
156
  py_type = self.get_parameter_pytype(prop)
154
157
  if not py_type:
155
158
  # log an error and use 'Any'
156
- self.logger.error(f"Unable to determine Python type for {prop}")
159
+ logger.error(f"Unable to determine Python type for {prop}")
157
160
  py_type = 'Any'
158
161
 
159
162
  if collection:
@@ -201,7 +204,7 @@ from {self.package_name} import _requests as _r
201
204
  """Create a module for the current node, and recursively generate sub-commands."""
202
205
  if node.operations():
203
206
  module_name = to_snake_case(node.identifier)
204
- self.logger.info(f"Generating {module_name} module")
207
+ logger.info(f"Generating {module_name} module")
205
208
  text = self.copyright
206
209
  text += self.standard_imports()
207
210
  for command in node.operations():
@@ -9,6 +9,9 @@ from openapi_spec_tools.base_gen import SEP1
9
9
  from openapi_spec_tools.base_gen import quoted
10
10
  from openapi_spec_tools.layout import LayoutNode
11
11
  from openapi_spec_tools.types import OasField
12
+ from openapi_spec_tools.utils import get_logger
13
+
14
+ logger = get_logger(__name__)
12
15
 
13
16
 
14
17
  class FlatApiGenerator(ApiGenerator):
@@ -59,7 +62,7 @@ class FlatApiGenerator(ApiGenerator):
59
62
  func_args.extend(self.command_infra_arguments(node))
60
63
  args_str = SEP1 + SEP1.join(func_args) + NL
61
64
 
62
- self.logger.debug(f"{func_name}({len(path_params)} path, {len(query_params)} query, {len(body_params)} body)")
65
+ logger.debug(f"{func_name}({len(path_params)} path, {len(query_params)} query, {len(body_params)} body)")
63
66
 
64
67
  user_header_init = ""
65
68
  user_header_arg = ""
@@ -7,6 +7,9 @@ from openapi_spec_tools.base_gen import SEP1
7
7
  from openapi_spec_tools.base_gen import quoted
8
8
  from openapi_spec_tools.layout import LayoutNode
9
9
  from openapi_spec_tools.types import OasField
10
+ from openapi_spec_tools.utils import get_logger
11
+
12
+ logger = get_logger(__name__)
10
13
 
11
14
 
12
15
  class OpaqueApiGenerator(ApiGenerator):
@@ -77,7 +80,7 @@ class OpaqueApiGenerator(ApiGenerator):
77
80
  func_args.extend(self.command_infra_arguments(node))
78
81
  args_str = SEP1 + SEP1.join(func_args) + NL
79
82
 
80
- self.logger.debug(f"{func_name}({len(path_params)} path, {len(query_params)} query")
83
+ logger.debug(f"{func_name}({len(path_params)} path, {len(query_params)} query")
81
84
 
82
85
  user_header_init = ""
83
86
  user_header_arg = ""
@@ -1,14 +1,19 @@
1
1
  """Declares the PropertyApiGenerator class."""
2
- from copy import deepcopy
3
2
  from typing import Any
4
3
 
5
4
  from openapi_spec_tools.api_gen.api_generator import ApiGenerator
6
5
  from openapi_spec_tools.base_gen import NL
7
6
  from openapi_spec_tools.base_gen import SEP1
8
7
  from openapi_spec_tools.base_gen import quoted
9
- from openapi_spec_tools.base_gen import shallow
8
+ from openapi_spec_tools.expand import expand_schema
9
+ from openapi_spec_tools.flatten import apply_any_of
10
+ from openapi_spec_tools.flatten import apply_one_of
11
+ from openapi_spec_tools.flatten import flatten_collection
10
12
  from openapi_spec_tools.layout import LayoutNode
11
13
  from openapi_spec_tools.types import OasField
14
+ from openapi_spec_tools.utils import get_logger
15
+
16
+ logger = get_logger(__name__)
12
17
 
13
18
 
14
19
  class PropertyApiGenerator(ApiGenerator):
@@ -24,57 +29,26 @@ class PropertyApiGenerator(ApiGenerator):
24
29
 
25
30
  def model_properties(self, name: str, schema: dict[str, Any]) -> dict[str, Any]: # noqa: PLR0912
26
31
  """Get a list of settable model properties."""
27
- model = deepcopy(schema)
28
- body_props = {}
29
-
30
- # start with the base-classes in allOf
31
- for parent in model.get(OasField.ALL_OF, []):
32
- reference = parent.get(OasField.REFS, "")
33
- if reference:
34
- sub_model = self.get_model(reference)
35
- else:
36
- sub_model = deepcopy(parent)
37
- required_sub = sub_model.get(OasField.REQUIRED, [])
38
- for sub_name, _sub_data in sub_model.get(OasField.PROPS, {}).items():
39
- if _sub_data.get(OasField.READ_ONLY):
40
- continue
41
-
42
- sub_data = self.update_one_of(sub_name, deepcopy(_sub_data))
43
- sub_data = self.update_collection(sub_data)
44
- sub_data = self.update_reference(sub_data)
45
- sub_data[OasField.REQUIRED.value] = sub_data.get(OasField.REQUIRED) or sub_name in required_sub
46
- body_props[sub_name] = self.update_enum(sub_data)
47
-
48
- any_of = model.pop(OasField.ANY_OF, None)
49
- if any_of:
50
- if len(any_of) != 1:
51
- self.logger.info(f"Grabbing anyOf[0] item from {name}")
52
- self.logger.debug(f"{name} anyOf selected: {shallow(any_of[0])}")
53
- # just grab the first one... not sure this is the best choice, but need to do something
54
- updated = self.update_one_of(name, deepcopy(any_of[0]))
55
- updated = self.update_collection(updated)
56
- updated = self.update_reference(updated)
57
- model.update(self.update_enum(updated))
58
-
59
- model = self.update_one_of(name, model)
60
- model = self.update_collection(model)
61
- model = self.update_reference(model)
62
- required_props = model.get(OasField.REQUIRED, [])
63
-
64
- # copy the individual properties
65
- for prop_name, _prop_data in model.get(OasField.PROPS, {}).items():
66
- if _prop_data.get(OasField.READ_ONLY, False):
67
- continue
68
-
69
- prop_data = deepcopy(_prop_data)
70
- prop_data[OasField.REQUIRED.value] = prop_name in required_props
71
- prop_data = self.update_one_of(prop_name, prop_data)
72
- prop_data = self.update_collection(prop_data)
73
- prop_data = self.update_reference(prop_data)
74
-
75
- body_props[prop_name] = self.update_enum(prop_data)
76
-
77
- return body_props
32
+ expanded = expand_schema(schema, self.components, name)
33
+ while apply_one_of(expanded) or apply_any_of(expanded):
34
+ pass
35
+ flatten_collection(expanded, force_single=False)
36
+ settable = {
37
+ name: data
38
+ for name, data in expanded.get(OasField.PROPS).items()
39
+ if not data.get(OasField.READ_ONLY, False)
40
+ }
41
+
42
+ # update settable fields for easier digestion
43
+ required = expanded.get(OasField.REQUIRED) or []
44
+ for prop_name, prop_data in settable.items():
45
+ while apply_one_of(prop_data) or apply_any_of(prop_data):
46
+ pass
47
+ flatten_collection(prop_data, force_single=False)
48
+ self.update_enum(prop_data)
49
+ prop_data[OasField.REQUIRED.value] = bool(prop_name in required)
50
+
51
+ return settable
78
52
 
79
53
  def op_body_formation(self, properties: dict[str, Any]) -> str:
80
54
  """Create body parameter and poulates it when there are body paramters."""
@@ -142,7 +116,7 @@ class PropertyApiGenerator(ApiGenerator):
142
116
  func_args.extend(self.command_infra_arguments(node))
143
117
  args_str = SEP1 + SEP1.join(func_args) + NL
144
118
 
145
- self.logger.debug(f"{func_name}({len(path_params)} path, {len(query_params)} query")
119
+ logger.debug(f"{func_name}({len(path_params)} path, {len(query_params)} query")
146
120
 
147
121
  user_header_init = ""
148
122
  user_header_arg = ""
@@ -14,12 +14,14 @@ class LogLevel(str, Enum):
14
14
  DEBUG = "debug"
15
15
 
16
16
 
17
- def get_logger(name: str | None = LOG_CLASS) -> logging.Logger:
18
- return logging.getLogger(name=name)
17
+ def get_logger() -> logging.Logger:
18
+ """Get the logger for LOG_CLASS."""
19
+ return logging.getLogger(name=LOG_CLASS)
19
20
 
20
21
 
21
- def init_logging(level: LogLevel, name: str | None = LOG_CLASS) -> logging.Logger:
22
+ def init_logging(level: LogLevel) -> logging.Logger:
23
+ """Initialize the LOG_CLASS logger and sets the level."""
22
24
  logging.basicConfig(format=LOG_FORMAT, datefmt=LOG_DATE_FMT)
23
- logger = get_logger(name)
25
+ logger = get_logger()
24
26
  logger.setLevel(level.upper())
25
27
  return logger