forked from NCATSTranslator/ReasonerAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_reference.py
More file actions
69 lines (53 loc) · 1.7 KB
/
Copy pathgenerate_reference.py
File metadata and controls
69 lines (53 loc) · 1.7 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Generate README."""
from pathlib import Path
import re
import sys
from typing import Any, Hashable, override
from jinja2 import Environment, FileSystemLoader
import yaml
from yaml.loader import SafeLoader
# So we can ensure we're going a directory up for the yaml
SCRIPT_DIR = Path(__file__).absolute().parent
def link_refs(string: str) -> str:
"""Hyperlink refs."""
return re.sub(
r"\"\$ref\": \"#/components/schemas/(\w+)\"",
r"\"$ref\": \"(#/components/schemas/\g<1>)[\g<1>]\"",
string,
)
class SafeLineLoader(SafeLoader):
@override
def construct_mapping(
self, node: yaml.MappingNode, deep: bool = False
) -> dict[Hashable, Any]:
mapping = super().construct_mapping(node, deep=deep)
# Add 1 so line numbering starts at 1
mapping["_start"] = node.start_mark.line
mapping["_end"] = node.end_mark.line
return mapping
def main():
"""Generate README."""
yamlpath = SCRIPT_DIR.parent.joinpath("TranslatorReasonerAPI.yaml")
j2_env = Environment(
loader=FileSystemLoader(SCRIPT_DIR),
trim_blocks=False,
keep_trailing_newline=True,
extensions=[
"jinja2.ext.do",
],
)
with yamlpath.open("r") as stream:
data = yaml.load(stream, Loader=SafeLineLoader)
components = data["components"]
schemas = components["schemas"]
sha = "master"
if len(sys.argv) >= 2:
sha = sys.argv[1]
markdown = j2_env.get_template("reference_template.md").render(
schemas=schemas,
sha=sha,
)
with SCRIPT_DIR.joinpath("reference.md").open("w") as stream:
_ = stream.write(markdown)
if __name__ == "__main__":
main()