openapi-spec-tools 0.15.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.15.0 → openapi_spec_tools-0.16.0}/PKG-INFO +1 -1
  2. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/utils.py +2 -11
  3. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/history.py +141 -44
  4. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/layout/types.py +23 -0
  5. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/utils.py +1 -1
  6. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/pyproject.toml +1 -1
  7. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/LICENSE +0 -0
  8. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/README.md +0 -0
  9. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/__init__.py +0 -0
  10. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/_environment.py +0 -0
  11. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/api_generator.py +0 -0
  12. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/files.py +0 -0
  13. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/flat_generator.py +0 -0
  14. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/opaque_generator.py +0 -0
  15. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/api_gen/property_generator.py +0 -0
  16. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/_logging.py +0 -0
  17. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/_requests.py +0 -0
  18. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/base_generator.py +0 -0
  19. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/constants.py +0 -0
  20. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/base_gen/files.py +0 -0
  21. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/api_gen.py +0 -0
  22. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/arguments.py +0 -0
  23. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/cli_gen.py +0 -0
  24. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/layout.py +0 -0
  25. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/oas.py +0 -0
  26. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli/utils.py +0 -0
  27. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/_arguments.py +0 -0
  28. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/_display.py +0 -0
  29. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/_exceptions.py +0 -0
  30. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/_tree.py +0 -0
  31. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/cli_generator.py +0 -0
  32. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/constants.py +0 -0
  33. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/cli_gen/files.py +0 -0
  34. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/layout/layout_generator.py +0 -0
  35. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/layout/merge.py +0 -0
  36. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/layout/utils.py +0 -0
  37. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/openapi_spec_tools/types.py +0 -0
  38. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/__init__.py +0 -0
  39. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/helpers.py +0 -0
  40. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/test_cli_generator.py +0 -0
  41. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/test_display.py +0 -0
  42. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/test_exceptions.py +0 -0
  43. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/test_files.py +0 -0
  44. {openapi_spec_tools-0.15.0 → openapi_spec_tools-0.16.0}/tests/cli_gen/test_main.py +0 -0
  45. {openapi_spec_tools-0.15.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.15.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
@@ -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,46 +161,6 @@ def _assert_exists(file: str) -> None:
106
161
  error_out("Unable to find file")
107
162
 
108
163
 
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
164
  @app.command("changes", short_help="Show OAS changes over git history.")
150
165
  def commit_changes(
151
166
  oas_file: OpenApiFilenameArgument,
@@ -180,7 +195,7 @@ def commit_changes(
180
195
  data.append(
181
196
  {
182
197
  "date": prev_comm.authored_datetime.date().isoformat(),
183
- "commit": prev_comm.hexsha[:7],
198
+ "commit": _shorthash(prev_comm),
184
199
  "changes": changes,
185
200
  }
186
201
  )
@@ -202,19 +217,101 @@ def commit_changes(
202
217
  return
203
218
 
204
219
 
220
+ @app.command("commits", short_help="Show git history of the specified OAS file.")
221
+ def commit_history(
222
+ oas_file: OpenApiFilenameArgument,
223
+ start: StartDateOption = None,
224
+ end: EndDateOption = None,
225
+ author: AuthorOption = None,
226
+ max_count: MaxCountOption = 30,
227
+ out_fmt: OutputFormatOption = OutputFormat.TABLE,
228
+ out_style: OutputStyleOption = OutputStyle.ALL,
229
+ ):
230
+ """Show git history of the specific OAS file over with the provided search parameters."""
231
+ _assert_exists(oas_file)
232
+ start = _with_timezone(start)
233
+ end = _with_timezone(end)
234
+ commits = _find_commits(
235
+ oas_file=oas_file,
236
+ start=start,
237
+ end=end,
238
+ author=author,
239
+ max_count=max_count,
240
+ )
241
+ console = console_factory()
242
+ if not commits:
243
+ console.print("No commits found.")
244
+ return
245
+
246
+ columns = ["date", "commit", "*"]
247
+ data = [
248
+ {
249
+ "date": _.authored_datetime.date().isoformat(),
250
+ "commit": _shorthash(_),
251
+ "author": _.author.name,
252
+ "message": _.message.strip()[:100],
253
+ }
254
+ for _ in commits
255
+ ]
256
+ display(data, fmt=out_fmt, style=out_style, columns=columns, console=console)
257
+ return
258
+
259
+
260
+ @app.command("diff", short_help="Show difference between two points in OAS history.")
261
+ def commit_diff(
262
+ oas_file: OpenApiFilenameArgument,
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
+
205
302
  @app.command("show", short_help="Show single OAS change in git history.")
206
303
  def commit_show(
207
304
  oas_file: OpenApiFilenameArgument,
208
305
  commit: Annotated[
209
306
  str | None,
210
- typer.Argument(help="Commit hash"),
307
+ typer.Argument(metavar="HASH", help="Commit hash"),
211
308
  ],
212
309
  out_fmt: OutputFormatOption = OutputFormat.TABLE,
213
310
  out_style: OutputStyleOption = OutputStyle.ALL,
214
311
  ):
215
312
  """Show OAS changes over the history of the provided search. The ."""
216
313
  _assert_exists(oas_file)
217
- _commit_id = commit.lower()
314
+ _commit_id = _commithash(commit)
218
315
  # NOTE: do not filter out commits by other authors, or before start, or will get odd comparisons
219
316
  commits = _find_commits(oas_file=oas_file)
220
317
  columns = ["date", "commit", "changes"]
@@ -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.15.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"