-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtitles.py
44 lines (33 loc) · 1.4 KB
/
titles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from typing import Callable, Tuple
from json_ref_dict import URI
from statham.schema.constants import COMPOSITION_KEYWORDS
def _get_title_from_reference(reference: str) -> str:
"""Convert JSON Schema references to title fields.
If the reference has a pointer, use the final segment, otherwise
use the final segment of the base uri stripping any content type
extension.
:param reference: The JSONPointer reference.
:return: An autogenerated title for the reference target.
"""
uri = URI.from_string(reference)
if not uri.pointer.strip("/"):
return uri.uri_name.split(".")[0]
title = uri.pointer.split("/")[-1]
if title == "items":
return _get_title_from_reference(repr(uri.back())) + "Item"
if title.isdigit():
# Handle anonymous schemas in an array.
# E.G `{"anyOf": [{"type": "object"}]}`
return _get_title_from_reference(repr(uri.back())) + title
if title in COMPOSITION_KEYWORDS:
return _get_title_from_reference(repr(uri.back()))
return title
def title_labeller() -> Callable[[str], Tuple[str, str]]:
"""Create a title labeller, enumerating repeated titles.
Used to assign meaningful names to schemas which have no specified
title.
"""
def _get_title(reference: str) -> Tuple[str, str]:
name = _get_title_from_reference(reference)
return "_x_autotitle", name
return _get_title