11"""Generate README."""
2- import os
2+
3+ from pathlib import Path
34import re
5+ import sys
6+ from typing import Any , Hashable , override
47
58from jinja2 import Environment , FileSystemLoader
69import yaml
710from yaml .loader import SafeLoader
811
9- THIS_DIR = os .path .dirname (os .path .abspath (__file__ ))
12+ # So we can ensure we're going a directory up for the yaml
13+ SCRIPT_DIR = Path (__file__ ).absolute ().parent
1014
1115
12- def link_refs (string ) :
16+ def link_refs (string : str ) -> str :
1317 """Hyperlink refs."""
1418 return re .sub (
1519 r"\"\$ref\": \"#/components/schemas/(\w+)\"" ,
@@ -19,44 +23,46 @@ def link_refs(string):
1923
2024
2125class SafeLineLoader (SafeLoader ):
22- def construct_mapping (self , node , deep = False ):
26+ @override
27+ def construct_mapping (
28+ self , node : yaml .MappingNode , deep : bool = False
29+ ) -> dict [Hashable , Any ]:
2330 mapping = super ().construct_mapping (node , deep = deep )
2431 # Add 1 so line numbering starts at 1
25- mapping [' _start' ] = node .start_mark .line
26- mapping [' _end' ] = node .end_mark .line
32+ mapping [" _start" ] = node .start_mark .line
33+ mapping [" _end" ] = node .end_mark .line
2734 return mapping
2835
2936
3037def main ():
3138 """Generate README."""
32- yamlpath = os .path .join (
33- THIS_DIR ,
34- "TranslatorReasonerAPI.yaml" ,
35- )
39+ yamlpath = SCRIPT_DIR .parent .joinpath ("TranslatorReasonerAPI.yaml" )
3640
3741 j2_env = Environment (
38- loader = FileSystemLoader (THIS_DIR ),
42+ loader = FileSystemLoader (SCRIPT_DIR ),
3943 trim_blocks = False ,
4044 keep_trailing_newline = True ,
4145 extensions = [
4246 "jinja2.ext.do" ,
43- ]
47+ ],
4448 )
4549
46- with open (yamlpath , 'r' ) as stream :
50+ with yamlpath . open ("r" ) as stream :
4751 data = yaml .load (stream , Loader = SafeLineLoader )
48- components = data [' components' ]
49- schemas = components [' schemas' ]
52+ components = data [" components" ]
53+ schemas = components [" schemas" ]
5054
5155 sha = "master"
56+ if len (sys .argv ) >= 2 :
57+ sha = sys .argv [1 ]
5258
53- markdown = j2_env .get_template (' reference_template.md' ).render (
59+ markdown = j2_env .get_template (" reference_template.md" ).render (
5460 schemas = schemas ,
5561 sha = sha ,
5662 )
5763
58- with open ( ' reference.md' , 'w' ) as stream :
59- stream .write (markdown . encode ( 'cp850' , 'replace' ). decode ( 'utf-8' , 'replace' ) )
64+ with SCRIPT_DIR . joinpath ( " reference.md" ). open ( "w" ) as stream :
65+ _ = stream .write (markdown )
6066
6167
6268if __name__ == "__main__" :
0 commit comments