openapi-spec-tools 0.14.0__tar.gz → 0.16.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.14.0 → openapi_spec_tools-0.16.0}/PKG-INFO +2 -2
  2. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/utils.py +2 -11
  3. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/history.py +167 -21
  4. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/layout/types.py +23 -0
  5. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/utils.py +1 -1
  6. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/pyproject.toml +2 -2
  7. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/LICENSE +0 -0
  8. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/README.md +0 -0
  9. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/__init__.py +0 -0
  10. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/_environment.py +0 -0
  11. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/api_generator.py +0 -0
  12. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/files.py +0 -0
  13. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/flat_generator.py +0 -0
  14. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/opaque_generator.py +0 -0
  15. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/property_generator.py +0 -0
  16. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/_logging.py +0 -0
  17. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/_requests.py +0 -0
  18. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/base_generator.py +0 -0
  19. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/constants.py +0 -0
  20. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/files.py +0 -0
  21. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/api_gen.py +0 -0
  22. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/arguments.py +0 -0
  23. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/cli_gen.py +0 -0
  24. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/layout.py +0 -0
  25. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/oas.py +0 -0
  26. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/utils.py +0 -0
  27. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/_arguments.py +0 -0
  28. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/_display.py +0 -0
  29. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/_exceptions.py +0 -0
  30. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/_tree.py +0 -0
  31. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/cli_generator.py +0 -0
  32. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/constants.py +0 -0
  33. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/files.py +0 -0
  34. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/layout/layout_generator.py +0 -0
  35. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/layout/merge.py +0 -0
  36. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/layout/utils.py +0 -0
  37. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/types.py +0 -0
  38. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/__init__.py +0 -0
  39. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/helpers.py +0 -0
  40. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/test_cli_generator.py +0 -0
  41. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/test_display.py +0 -0
  42. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/test_exceptions.py +0 -0
  43. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/test_files.py +0 -0
  44. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/test_main.py +0 -0
  45. {openapi_spec_tools-0.14.0 → openapi_spec_tools-0.16.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.14.0
3
+ Version: 0.16.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,7 +24,7 @@ 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: gitpython (>=3.1.47,<4.0.0)
27
+ Requires-Dist: gitpython (>=3.1.50,<4.0.0)
28
28
  Requires-Dist: pydantic (>=2.13.3,<3.0.0)
29
29
  Requires-Dist: pyyaml (>=6.0.3,<7.0.0)
30
30
  Requires-Dist: requests (>=2.33.1,<3.0.0)
@@ -9,14 +9,7 @@ SIMPLE_TRANSLATIONS = str.maketrans(
9
9
  '"': r"\"",
10
10
  },
11
11
  )
12
- SPECIAL_CHARS = [
13
- # mathematical
14
- '/', '*', '+', '-', '^', '%', '&', '|', '~', '<', '>','=',
15
- # operators/punctional
16
- '.', '@', ' ', ':', ';', '#', ',', '!', '`', '?', '\'', '"',
17
- # parenthesis/brackets
18
- '(', ')', '{', '}', '[', ']',
19
- ]
12
+ SPECIAL_CHARS = re.compile(r'([^a-zA-Z0-9])+')
20
13
 
21
14
 
22
15
  def to_snake_case(text: str) -> str:
@@ -47,9 +40,7 @@ def quoted(s: str) -> str:
47
40
  def replace_special(value: str, replacement: str = '_') -> str:
48
41
  """Replace the "special" characters with the replacement."""
49
42
  _replacement = '' if replacement is None else replacement
50
- for v in SPECIAL_CHARS:
51
- value = value.replace(v, _replacement)
52
- return value
43
+ return SPECIAL_CHARS.sub(_replacement, value)
53
44
 
54
45
 
55
46
  def simple_escape(text: str) -> str:
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env python3
2
2
  """Implement the 'oas-history' CLI commands."""
3
+ import re
3
4
  from datetime import datetime
4
5
  from datetime import timezone
5
6
  from pathlib import Path
@@ -23,12 +24,21 @@ from openapi_spec_tools.cli.arguments import OutputStyleOption
23
24
  from openapi_spec_tools.cli.utils import error_out
24
25
  from openapi_spec_tools.utils import find_diffs
25
26
 
27
+ MIN_HASH_LENGTH = 4
28
+ NON_HEX_RE = re.compile(r'[^a-fA-F0-9]')
29
+
30
+
26
31
  app = typer.Typer(name="history", no_args_is_help=True, short_help="Git history of OAS file")
27
32
 
28
33
 
29
34
  AuthorOption = Annotated[
30
35
  Optional[str],
31
- typer.Option("--author", show_default=False, help="Name, or partial name, of author or coauthor (case insensitive)")
36
+ typer.Option(
37
+ "--author",
38
+ metavar="NAME",
39
+ show_default=False,
40
+ help="Name, or partial name, of author or coauthor (case insensitive)",
41
+ )
32
42
  ]
33
43
  EndDateOption = Annotated[
34
44
  Optional[datetime],
@@ -72,6 +82,24 @@ def _author_match(commit: git.Commit, author: str) -> bool:
72
82
  return False
73
83
 
74
84
 
85
+ def _shorthash(commit: git.Commit) -> str:
86
+ """Get a shortened version of the commit hash."""
87
+ return commit.hexsha[:7]
88
+
89
+
90
+ def _commithash(hash: str) -> str:
91
+ """Perform basic checks on the provided hash.
92
+
93
+ If the hash does NOT meet the standard, an exception is thrown and a meaningful
94
+ message provided.
95
+ """
96
+ if len(hash) < MIN_HASH_LENGTH:
97
+ error_out(f"Hash must be at least {MIN_HASH_LENGTH} characters")
98
+ if NON_HEX_RE.search(hash):
99
+ error_out("Hash must be all hexidecimal characters")
100
+ return hash.lower()
101
+
102
+
75
103
  def _find_commits(
76
104
  oas_file: str,
77
105
  start: Optional[datetime] = None,
@@ -95,6 +123,33 @@ def _find_commits(
95
123
  return commits
96
124
 
97
125
 
126
+ def _get_commit(
127
+ oas_file: str,
128
+ commit_id: str,
129
+ ) -> git.Commit | None:
130
+ """Search the commits looking for a match."""
131
+ repo = git.Repo(oas_file, search_parent_directories=True)
132
+ for tag in repo.tags:
133
+ if commit_id == tag.name:
134
+ return tag.commit
135
+
136
+ for branch in repo.branches:
137
+ if commit_id == branch.name:
138
+ # unfortunatley, testing in CI pipeline complicates covering this too much
139
+ return branch.commit # pragma: no cover
140
+
141
+
142
+ for commit in repo.iter_commits(paths=oas_file):
143
+ if not commit_id:
144
+ # gets the latest commit if no commit is specified
145
+ return commit
146
+
147
+ if commit_id.lower() in commit.hexsha:
148
+ return commit
149
+
150
+ error_out(f"Unable to find specified commit for '{commit_id}'")
151
+
152
+
98
153
  def _assert_exists(file: str) -> None:
99
154
  """Assert that the file exists in the git repo, or throw an exception."""
100
155
  try:
@@ -106,6 +161,62 @@ def _assert_exists(file: str) -> None:
106
161
  error_out("Unable to find file")
107
162
 
108
163
 
164
+ @app.command("changes", short_help="Show OAS changes over git history.")
165
+ def commit_changes(
166
+ oas_file: OpenApiFilenameArgument,
167
+ start: StartDateOption = None,
168
+ end: EndDateOption = None,
169
+ author: AuthorOption = None,
170
+ max_count: MaxCountOption = 30,
171
+ out_fmt: OutputFormatOption = OutputFormat.TABLE,
172
+ out_style: OutputStyleOption = OutputStyle.ALL,
173
+ ):
174
+ """Show OAS changes over the history of the provided search. The ."""
175
+ _assert_exists(oas_file)
176
+ start = _with_timezone(start)
177
+ end = _with_timezone(end)
178
+ # NOTE: do not filter out commits by other authors, or before start, or will get odd comparisons
179
+ commits = _find_commits(
180
+ oas_file=oas_file,
181
+ end=end,
182
+ )
183
+ columns = ["date", "commit", "changes"]
184
+ data = []
185
+ prev_comm = commits[0]
186
+ prev_data = _read_data(prev_comm, oas_file)
187
+ for curr_comm in commits[1:]:
188
+ curr_data = _read_data(curr_comm, oas_file)
189
+ if not author or _author_match(prev_comm, author):
190
+ # because we're walking backward chronologicallly, the prev/curr are different order
191
+ changes = find_diffs(curr_data, prev_data)
192
+ if out_fmt == OutputFormat.TABLE:
193
+ # YAML format is the best looking format for the diffs in a table, so force it
194
+ changes = yaml.dump(changes).strip()
195
+ data.append(
196
+ {
197
+ "date": prev_comm.authored_datetime.date().isoformat(),
198
+ "commit": _shorthash(prev_comm),
199
+ "changes": changes,
200
+ }
201
+ )
202
+
203
+ # check whether we've hit the last entry
204
+ if len(data) == max_count or (start and curr_comm.authored_datetime < start):
205
+ break
206
+
207
+ prev_data = curr_data
208
+ prev_comm = curr_comm
209
+
210
+ console = console_factory()
211
+ if not data:
212
+ console.print("No differences found with those parameters.")
213
+ return
214
+
215
+ config = TableConfig(value_max_len=1000)
216
+ display(data, fmt=out_fmt, style=out_style, columns=columns, config=config, console=console)
217
+ return
218
+
219
+
109
220
  @app.command("commits", short_help="Show git history of the specified OAS file.")
110
221
  def commit_history(
111
222
  oas_file: OpenApiFilenameArgument,
@@ -136,7 +247,7 @@ def commit_history(
136
247
  data = [
137
248
  {
138
249
  "date": _.authored_datetime.date().isoformat(),
139
- "commit": _.hexsha[:7],
250
+ "commit": _shorthash(_),
140
251
  "author": _.author.name,
141
252
  "message": _.message.strip()[:100],
142
253
  }
@@ -146,33 +257,72 @@ def commit_history(
146
257
  return
147
258
 
148
259
 
149
- @app.command("changes", short_help="Show OAS changes over git history.")
150
- def commit_changes(
260
+ @app.command("diff", short_help="Show difference between two points in OAS history.")
261
+ def commit_diff(
151
262
  oas_file: OpenApiFilenameArgument,
152
- start: StartDateOption = None,
153
- end: EndDateOption = None,
154
- author: AuthorOption = None,
155
- max_count: MaxCountOption = 30,
263
+ commit_id: Annotated[
264
+ str,
265
+ typer.Argument(metavar="COMMIT[..COMMIT]", help="Commit hash(es), tag(s), or branch(es)"),
266
+ ],
267
+ out_fmt: OutputFormatOption = OutputFormat.TABLE,
268
+ out_style: OutputStyleOption = OutputStyle.ALL,
269
+ ):
270
+ _assert_exists(oas_file)
271
+
272
+ parts = commit_id.split("..", maxsplit=1)
273
+ start_comm = _get_commit(oas_file, parts[0])
274
+ end_comm = _get_commit(oas_file, parts[1] if len(parts) > 1 else None)
275
+
276
+ console = console_factory()
277
+
278
+ start_data = _read_data(start_comm, oas_file)
279
+ end_data = _read_data(end_comm, oas_file)
280
+ changes = find_diffs(start_data, end_data)
281
+ if not changes:
282
+ console.print("Unable to determine differences.")
283
+ return
284
+
285
+ if out_fmt == OutputFormat.TABLE:
286
+ # YAML format is the best looking format for the diffs in a table, so force it
287
+ changes = yaml.dump(changes).strip()
288
+
289
+ columns = ["commits", "changes"]
290
+ data = [
291
+ {
292
+ "commits": f"{_shorthash(start_comm)}..{_shorthash(end_comm)}",
293
+ "changes": changes,
294
+ }
295
+ ]
296
+
297
+ config = TableConfig(value_max_len=1000)
298
+ display(data, fmt=out_fmt, style=out_style, columns=columns, config=config, console=console)
299
+ return
300
+
301
+
302
+ @app.command("show", short_help="Show single OAS change in git history.")
303
+ def commit_show(
304
+ oas_file: OpenApiFilenameArgument,
305
+ commit: Annotated[
306
+ str | None,
307
+ typer.Argument(metavar="HASH", help="Commit hash"),
308
+ ],
156
309
  out_fmt: OutputFormatOption = OutputFormat.TABLE,
157
310
  out_style: OutputStyleOption = OutputStyle.ALL,
158
311
  ):
159
312
  """Show OAS changes over the history of the provided search. The ."""
160
313
  _assert_exists(oas_file)
161
- start = _with_timezone(start)
162
- end = _with_timezone(end)
314
+ _commit_id = _commithash(commit)
163
315
  # 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
- )
316
+ commits = _find_commits(oas_file=oas_file)
168
317
  columns = ["date", "commit", "changes"]
169
318
  data = []
170
319
  prev_comm = commits[0]
171
320
  prev_data = _read_data(prev_comm, oas_file)
172
321
  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):
322
+ if _commit_id in prev_comm.hexsha:
175
323
  # because we're walking backward chronologicallly, the prev/curr are different order
324
+ curr_data = _read_data(curr_comm, oas_file)
325
+ prev_data = _read_data(prev_comm, oas_file)
176
326
  changes = find_diffs(curr_data, prev_data)
177
327
  if out_fmt == OutputFormat.TABLE:
178
328
  # YAML format is the best looking format for the diffs in a table, so force it
@@ -184,17 +334,13 @@ def commit_changes(
184
334
  "changes": changes,
185
335
  }
186
336
  )
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
337
  break
191
338
 
192
- prev_data = curr_data
193
339
  prev_comm = curr_comm
194
340
 
195
341
  console = console_factory()
196
342
  if not data:
197
- console.print("No differences found with those parameters.")
343
+ console.print("No matching commit found.")
198
344
  return
199
345
 
200
346
  config = TableConfig(value_max_len=1000)
@@ -103,6 +103,29 @@ class LayoutNode(BaseModel):
103
103
  display_columns: list[str] = Field(default_factory=list)
104
104
  ignore: Optional[bool] = None
105
105
 
106
+ def __str__(self):
107
+ """Display shorter version of LayoutNode."""
108
+ return self.display()
109
+
110
+ def __repr__(self):
111
+ """Display shorter version of LayoutNode."""
112
+ return self.display()
113
+
114
+ def display(self, depth: int = 2, max_children: int = 3) -> str:
115
+ """Display a shorter version of the node."""
116
+ class_name = self.__class__.__name__
117
+ items = [
118
+ self.command,
119
+ f"id={self.identifier}",
120
+ ]
121
+ if self.children:
122
+ if not depth or len(self.children) > max_children:
123
+ items.append(f'children={len(self.children)}')
124
+ else:
125
+ child_disp = [child.display(depth-1) for child in self.children]
126
+ items.append(f"children=[{', '.join(child_disp)}]")
127
+ return f"{class_name}({', '.join(items)})"
128
+
106
129
  def as_dict(self, sparse: bool = True) -> dict[str, Any]:
107
130
  """Convert object to dictionary."""
108
131
  return self.model_dump(exclude_none=sparse, exclude_unset=sparse, exclude_defaults=sparse)
@@ -179,7 +179,7 @@ def find_diffs(lhs: dict[str, Any], rhs: dict[str, Any]) -> dict[str, Any]:
179
179
  elif left != right:
180
180
  result[k] = f"{shorten_text(str(left))} != {shorten_text(str(right))}"
181
181
 
182
- return result
182
+ return {k: result[k] for k in sorted(result.keys())}
183
183
 
184
184
 
185
185
  def count_values(obj: dict[str, Any]) -> int:
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "openapi-spec-tools"
3
- version = "0.14.0"
3
+ version = "0.16.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"
@@ -41,7 +41,7 @@ rich = "^14.3.3"
41
41
  requests = "^2.33.1"
42
42
  pydantic = "^2.13.3"
43
43
  rich-objects = "^0.2.3"
44
- gitpython = "^3.1.47"
44
+ gitpython = "^3.1.50"
45
45
 
46
46
  [tool.poetry.scripts]
47
47
  oas = "openapi_spec_tools.cli.oas:app"