openapi-spec-tools 0.13.0__tar.gz → 0.15.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 (45) hide show
  1. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/PKG-INFO +5 -4
  2. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli/arguments.py +26 -1
  3. openapi_spec_tools-0.15.0/openapi_spec_tools/cli/history.py +255 -0
  4. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli/oas.py +2 -0
  5. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/pyproject.toml +5 -4
  6. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/LICENSE +0 -0
  7. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/README.md +0 -0
  8. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/__init__.py +0 -0
  9. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/api_gen/_environment.py +0 -0
  10. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/api_gen/api_generator.py +0 -0
  11. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/api_gen/files.py +0 -0
  12. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/api_gen/flat_generator.py +0 -0
  13. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/api_gen/opaque_generator.py +0 -0
  14. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/api_gen/property_generator.py +0 -0
  15. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/base_gen/_logging.py +0 -0
  16. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/base_gen/_requests.py +0 -0
  17. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/base_gen/base_generator.py +0 -0
  18. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/base_gen/constants.py +0 -0
  19. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/base_gen/files.py +0 -0
  20. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/base_gen/utils.py +0 -0
  21. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli/api_gen.py +0 -0
  22. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli/cli_gen.py +0 -0
  23. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli/layout.py +0 -0
  24. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli/utils.py +0 -0
  25. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli_gen/_arguments.py +0 -0
  26. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli_gen/_display.py +0 -0
  27. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli_gen/_exceptions.py +0 -0
  28. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli_gen/_tree.py +0 -0
  29. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli_gen/cli_generator.py +0 -0
  30. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli_gen/constants.py +0 -0
  31. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/cli_gen/files.py +0 -0
  32. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/layout/layout_generator.py +0 -0
  33. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/layout/merge.py +0 -0
  34. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/layout/types.py +0 -0
  35. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/layout/utils.py +0 -0
  36. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/types.py +0 -0
  37. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/openapi_spec_tools/utils.py +0 -0
  38. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/tests/cli_gen/__init__.py +0 -0
  39. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/tests/cli_gen/helpers.py +0 -0
  40. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/tests/cli_gen/test_cli_generator.py +0 -0
  41. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/tests/cli_gen/test_display.py +0 -0
  42. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/tests/cli_gen/test_exceptions.py +0 -0
  43. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/tests/cli_gen/test_files.py +0 -0
  44. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/tests/cli_gen/test_main.py +0 -0
  45. {openapi_spec_tools-0.13.0 → openapi_spec_tools-0.15.0}/tests/cli_gen/test_tree.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: openapi-spec-tools
3
- Version: 0.13.0
3
+ Version: 0.15.0
4
4
  Summary: OpenAPI specification tools for analyzing, updating, and generating a CLI.
5
5
  License-File: LICENSE
6
6
  Author: Rick Porter
@@ -24,12 +24,13 @@ Classifier: Topic :: Software Development :: Libraries
24
24
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
25
25
  Classifier: Topic :: Utilities
26
26
  Classifier: Typing :: Typed
27
- Requires-Dist: pydantic (>=2.12.5,<3.0.0)
27
+ Requires-Dist: gitpython (>=3.1.50,<4.0.0)
28
+ Requires-Dist: pydantic (>=2.13.3,<3.0.0)
28
29
  Requires-Dist: pyyaml (>=6.0.3,<7.0.0)
29
- Requires-Dist: requests (>=2.32.5,<3.0.0)
30
+ Requires-Dist: requests (>=2.33.1,<3.0.0)
30
31
  Requires-Dist: rich (>=14.3.3,<15.0.0)
31
32
  Requires-Dist: rich-objects (>=0.2.3,<0.3.0)
32
- Requires-Dist: typer (>=0.24.1,<0.25.0)
33
+ Requires-Dist: typer (>=0.24.2,<0.25.0)
33
34
  Project-URL: Homepage, https://github.com/rickwporter/openapi-spec-tools
34
35
  Project-URL: Repository, https://github.com/rickwporter/openapi-spec-tools
35
36
  Description-Content-Type: text/markdown
@@ -4,6 +4,8 @@ from typing import Annotated
4
4
  from typing import Optional
5
5
 
6
6
  import typer
7
+ from rich_objects import OutputFormat
8
+ from rich_objects import OutputStyle
7
9
 
8
10
  DIRECTORY = "DIRECTORY"
9
11
  FILENAME = "FILENAME"
@@ -61,10 +63,34 @@ LogLevelOption = Annotated[
61
63
  help="Log level",
62
64
  ),
63
65
  ]
66
+ MaxCountOption = Annotated[
67
+ Optional[int],
68
+ typer.Option(
69
+ "--max",
70
+ "--max-count",
71
+ help="Maximum number of items to get (if any)."
72
+ )
73
+ ]
64
74
  OpenApiFilenameArgument = Annotated[
65
75
  str,
66
76
  typer.Argument(metavar=FILENAME, show_default=False, help="OpenAPI specification filename"),
67
77
  ]
78
+ OutputFormatOption = Annotated[
79
+ OutputFormat,
80
+ typer.Option(
81
+ "--format",
82
+ case_sensitive=False,
83
+ help="Output format style",
84
+ ),
85
+ ]
86
+ OutputStyleOption = Annotated[
87
+ OutputStyle,
88
+ typer.Option(
89
+ "--style",
90
+ case_sensitive=False,
91
+ help="Style for output",
92
+ ),
93
+ ]
68
94
  PackageNameArgument = Annotated[str, typer.Argument(metavar="PACKAGE", show_default=False, help="Base package name")]
69
95
  PathPrefixOption = Annotated[
70
96
  str,
@@ -80,4 +106,3 @@ UpdatedOpenApiFilenameOption = Annotated[
80
106
  help="Filename for updated OpenAPI spec, overwrites original of not specified.",
81
107
  ),
82
108
  ]
83
-
@@ -0,0 +1,255 @@
1
+ #!/usr/bin/env python3
2
+ """Implement the 'oas-history' CLI commands."""
3
+ from datetime import datetime
4
+ from datetime import timezone
5
+ from pathlib import Path
6
+ from typing import Annotated
7
+ from typing import Any
8
+ from typing import Optional
9
+
10
+ import git
11
+ import typer
12
+ import yaml
13
+ from rich_objects import TableConfig
14
+ from rich_objects import console_factory
15
+ from rich_objects import display
16
+
17
+ from openapi_spec_tools.cli.arguments import MaxCountOption
18
+ from openapi_spec_tools.cli.arguments import OpenApiFilenameArgument
19
+ from openapi_spec_tools.cli.arguments import OutputFormat
20
+ from openapi_spec_tools.cli.arguments import OutputFormatOption
21
+ from openapi_spec_tools.cli.arguments import OutputStyle
22
+ from openapi_spec_tools.cli.arguments import OutputStyleOption
23
+ from openapi_spec_tools.cli.utils import error_out
24
+ from openapi_spec_tools.utils import find_diffs
25
+
26
+ app = typer.Typer(name="history", no_args_is_help=True, short_help="Git history of OAS file")
27
+
28
+
29
+ AuthorOption = Annotated[
30
+ Optional[str],
31
+ typer.Option("--author", show_default=False, help="Name, or partial name, of author or coauthor (case insensitive)")
32
+ ]
33
+ EndDateOption = Annotated[
34
+ Optional[datetime],
35
+ typer.Option("--end", show_default=False, help="Date/time of latest commit.")
36
+ ]
37
+ StartDateOption = Annotated[
38
+ Optional[datetime],
39
+ typer.Option("--start", show_default=False, help="Date/time of first commit.")
40
+ ]
41
+
42
+
43
+
44
+ def _with_timezone(dt: datetime | None) -> datetime | None:
45
+ """Set the timezone to UTC when no timezone it provided.
46
+
47
+ The git datetime values include a timezone, and comparison to datetime objects without a
48
+ timezone does not work.
49
+ """
50
+ return dt if not dt or dt.tzinfo else dt.replace(tzinfo=timezone.utc)
51
+
52
+
53
+ def _read_data(commit: git.Commit, file: str) -> dict[str, Any]:
54
+ """Read the file from the commit into a dictionary."""
55
+ relative_path = Path(file).absolute().relative_to(commit.tree.abspath).as_posix()
56
+ target_file = commit.tree / relative_path
57
+ data = target_file.data_stream.read().decode("utf-8")
58
+ # NOTE: handles when data is JSON, so no special handling necessary
59
+ return yaml.safe_load(data)
60
+
61
+
62
+ def _author_match(commit: git.Commit, author: str) -> bool:
63
+ """Check if the author is part of the commit."""
64
+ needle = author.lower()
65
+ if needle in commit.author.name.lower() or needle in commit.author.email:
66
+ return True
67
+
68
+ for coauth in commit.co_authors: # pragma: no cover
69
+ if needle in coauth.name.lower() or needle in coauth.email:
70
+ return True
71
+
72
+ return False
73
+
74
+
75
+ def _find_commits(
76
+ oas_file: str,
77
+ start: Optional[datetime] = None,
78
+ end: Optional[datetime] = None,
79
+ author: Optional[str] = None,
80
+ max_count: Optional[int] = None,
81
+ ) -> list[git.Commit]:
82
+ """Get a list of commits for the provided file and parameters."""
83
+ repo = git.Repo(oas_file, search_parent_directories=True)
84
+
85
+ commits = list(repo.iter_commits(paths=oas_file))
86
+ if start:
87
+ commits = [_ for _ in commits if _.committed_datetime >= start]
88
+ if end:
89
+ commits = [_ for _ in commits if _.committed_datetime <= end]
90
+ if author:
91
+ commits = [_ for _ in commits if _author_match(_, author=author)]
92
+ if max_count:
93
+ commits = commits[:max_count]
94
+
95
+ return commits
96
+
97
+
98
+ def _assert_exists(file: str) -> None:
99
+ """Assert that the file exists in the git repo, or throw an exception."""
100
+ try:
101
+ repo = git.Repo(file, search_parent_directories=True)
102
+ commit = repo.head.commit
103
+ relative_path = Path(file).absolute().relative_to(commit.tree.abspath).as_posix()
104
+ _ = commit.tree / relative_path
105
+ except Exception:
106
+ error_out("Unable to find file")
107
+
108
+
109
+ @app.command("commits", short_help="Show git history of the specified OAS file.")
110
+ def commit_history(
111
+ oas_file: OpenApiFilenameArgument,
112
+ start: StartDateOption = None,
113
+ end: EndDateOption = None,
114
+ author: AuthorOption = None,
115
+ max_count: MaxCountOption = 30,
116
+ out_fmt: OutputFormatOption = OutputFormat.TABLE,
117
+ out_style: OutputStyleOption = OutputStyle.ALL,
118
+ ):
119
+ """Show git history of the specific OAS file over with the provided search parameters."""
120
+ _assert_exists(oas_file)
121
+ start = _with_timezone(start)
122
+ end = _with_timezone(end)
123
+ commits = _find_commits(
124
+ oas_file=oas_file,
125
+ start=start,
126
+ end=end,
127
+ author=author,
128
+ max_count=max_count,
129
+ )
130
+ console = console_factory()
131
+ if not commits:
132
+ console.print("No commits found.")
133
+ return
134
+
135
+ columns = ["date", "commit", "*"]
136
+ data = [
137
+ {
138
+ "date": _.authored_datetime.date().isoformat(),
139
+ "commit": _.hexsha[:7],
140
+ "author": _.author.name,
141
+ "message": _.message.strip()[:100],
142
+ }
143
+ for _ in commits
144
+ ]
145
+ display(data, fmt=out_fmt, style=out_style, columns=columns, console=console)
146
+ return
147
+
148
+
149
+ @app.command("changes", short_help="Show OAS changes over git history.")
150
+ def commit_changes(
151
+ oas_file: OpenApiFilenameArgument,
152
+ start: StartDateOption = None,
153
+ end: EndDateOption = None,
154
+ author: AuthorOption = None,
155
+ max_count: MaxCountOption = 30,
156
+ out_fmt: OutputFormatOption = OutputFormat.TABLE,
157
+ out_style: OutputStyleOption = OutputStyle.ALL,
158
+ ):
159
+ """Show OAS changes over the history of the provided search. The ."""
160
+ _assert_exists(oas_file)
161
+ start = _with_timezone(start)
162
+ end = _with_timezone(end)
163
+ # NOTE: do not filter out commits by other authors, or before start, or will get odd comparisons
164
+ commits = _find_commits(
165
+ oas_file=oas_file,
166
+ end=end,
167
+ )
168
+ columns = ["date", "commit", "changes"]
169
+ data = []
170
+ prev_comm = commits[0]
171
+ prev_data = _read_data(prev_comm, oas_file)
172
+ for curr_comm in commits[1:]:
173
+ curr_data = _read_data(curr_comm, oas_file)
174
+ if not author or _author_match(prev_comm, author):
175
+ # because we're walking backward chronologicallly, the prev/curr are different order
176
+ changes = find_diffs(curr_data, prev_data)
177
+ if out_fmt == OutputFormat.TABLE:
178
+ # YAML format is the best looking format for the diffs in a table, so force it
179
+ changes = yaml.dump(changes).strip()
180
+ data.append(
181
+ {
182
+ "date": prev_comm.authored_datetime.date().isoformat(),
183
+ "commit": prev_comm.hexsha[:7],
184
+ "changes": changes,
185
+ }
186
+ )
187
+
188
+ # check whether we've hit the last entry
189
+ if len(data) == max_count or (start and curr_comm.authored_datetime < start):
190
+ break
191
+
192
+ prev_data = curr_data
193
+ prev_comm = curr_comm
194
+
195
+ console = console_factory()
196
+ if not data:
197
+ console.print("No differences found with those parameters.")
198
+ return
199
+
200
+ config = TableConfig(value_max_len=1000)
201
+ display(data, fmt=out_fmt, style=out_style, columns=columns, config=config, console=console)
202
+ return
203
+
204
+
205
+ @app.command("show", short_help="Show single OAS change in git history.")
206
+ def commit_show(
207
+ oas_file: OpenApiFilenameArgument,
208
+ commit: Annotated[
209
+ str | None,
210
+ typer.Argument(help="Commit hash"),
211
+ ],
212
+ out_fmt: OutputFormatOption = OutputFormat.TABLE,
213
+ out_style: OutputStyleOption = OutputStyle.ALL,
214
+ ):
215
+ """Show OAS changes over the history of the provided search. The ."""
216
+ _assert_exists(oas_file)
217
+ _commit_id = commit.lower()
218
+ # NOTE: do not filter out commits by other authors, or before start, or will get odd comparisons
219
+ commits = _find_commits(oas_file=oas_file)
220
+ columns = ["date", "commit", "changes"]
221
+ data = []
222
+ prev_comm = commits[0]
223
+ prev_data = _read_data(prev_comm, oas_file)
224
+ for curr_comm in commits[1:]:
225
+ if _commit_id in prev_comm.hexsha:
226
+ # because we're walking backward chronologicallly, the prev/curr are different order
227
+ curr_data = _read_data(curr_comm, oas_file)
228
+ prev_data = _read_data(prev_comm, oas_file)
229
+ changes = find_diffs(curr_data, prev_data)
230
+ if out_fmt == OutputFormat.TABLE:
231
+ # YAML format is the best looking format for the diffs in a table, so force it
232
+ changes = yaml.dump(changes).strip()
233
+ data.append(
234
+ {
235
+ "date": prev_comm.authored_datetime.date().isoformat(),
236
+ "commit": prev_comm.hexsha[:7],
237
+ "changes": changes,
238
+ }
239
+ )
240
+ break
241
+
242
+ prev_comm = curr_comm
243
+
244
+ console = console_factory()
245
+ if not data:
246
+ console.print("No matching commit found.")
247
+ return
248
+
249
+ config = TableConfig(value_max_len=1000)
250
+ display(data, fmt=out_fmt, style=out_style, columns=columns, config=config, console=console)
251
+ return
252
+
253
+
254
+ if __name__ == "__main__":
255
+ app()
@@ -14,6 +14,7 @@ from rich_objects import console_factory
14
14
  from openapi_spec_tools.cli.arguments import LogLevelOption
15
15
  from openapi_spec_tools.cli.arguments import OpenApiFilenameArgument
16
16
  from openapi_spec_tools.cli.arguments import UpdatedOpenApiFilenameOption
17
+ from openapi_spec_tools.cli.history import app as history_app
17
18
  from openapi_spec_tools.cli.utils import error_out
18
19
  from openapi_spec_tools.cli.utils import init_logging
19
20
  from openapi_spec_tools.cli.utils import open_oas_with_error_handling
@@ -75,6 +76,7 @@ app = typer.Typer(
75
76
  short_help="OpenAPI specification tools",
76
77
  help="Various utilities for inspecting, analyzing and modifying OpenAPI specifications.",
77
78
  )
79
+ app.add_typer(history_app, name="history")
78
80
 
79
81
 
80
82
  @app.command("info", short_help="Display the 'info' from the OpenAPI specification")
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "openapi-spec-tools"
3
- version = "0.13.0"
3
+ version = "0.15.0"
4
4
  description = "OpenAPI specification tools for analyzing, updating, and generating a CLI."
5
5
  authors = ["Rick Porter <rickwporter@gmail.com>"]
6
6
  readme = "README.md"
@@ -36,11 +36,12 @@ Repository = "https://github.com/rickwporter/openapi-spec-tools"
36
36
  [tool.poetry.dependencies]
37
37
  python = "^3.10"
38
38
  pyyaml = "^6.0.3"
39
- typer = "^0.24.1"
39
+ typer = "^0.24.2"
40
40
  rich = "^14.3.3"
41
- requests = "^2.32.5"
42
- pydantic = "^2.12.5"
41
+ requests = "^2.33.1"
42
+ pydantic = "^2.13.3"
43
43
  rich-objects = "^0.2.3"
44
+ gitpython = "^3.1.50"
44
45
 
45
46
  [tool.poetry.scripts]
46
47
  oas = "openapi_spec_tools.cli.oas:app"