1+ import os
2+ import re
3+ import textwrap
4+ import json
5+
6+ #SIGNED_DOCS_SPECS="signed_doc.json"
7+ SIGNED_DOCS_SPECS = "includes/signed_doc.json"
8+
9+ def uuid_as_cbor (uuid ):
10+ return f"37(h'{ uuid .replace ('-' , '' )} ')"
11+
12+
13+ def get_signed_doc_data (env ):
14+ """
15+ Load the Signed Document Data from its json file.
16+ """
17+ full_filename = os .path .join (env .project_dir , SIGNED_DOCS_SPECS )
18+
19+ with open (full_filename , "r" ) as f :
20+ return json .load (f )
21+
22+ def doc_type_summary (env ):
23+ """
24+ Generate a Document Base Type Summary from the Document Specifications Data
25+ """
26+
27+ try :
28+ doc_data = get_signed_doc_data (env )
29+ doc_types = doc_data ["base_types" ]
30+
31+ doc_type_summary = """
32+ | Base Type | [UUID] | [CBOR] |
33+ | :--- | :--- | :--- |
34+ """
35+
36+ for k in doc_types :
37+ doc_type_summary += f"| { k } | `{ doc_types [k ]} ` | `{ uuid_as_cbor (doc_types [k ])} ` |\n "
38+
39+ return doc_type_summary
40+ except Exception as exc :
41+ return f"{ exc } "
42+
43+
44+ def name_for_uuid (doc_types , uuid ):
45+ """
46+ Get the name for a document base type, given its uuid
47+ """
48+ for k in doc_types :
49+ if doc_types [k ] == uuid :
50+ return k
51+ return "Unknown"
52+
53+
54+ def name_to_spec_link (name ,ref = None ):
55+ """
56+ Create a link to a document type, and an optional ref inside the document.
57+ """
58+ link = "./../catalyst_docs/" + name .lower ().replace (' ' ,'_' ) + ".md"
59+ if ref is not None :
60+ link += f"#{ ref } "
61+ return link
62+
63+ def base_types (docs , doc_types , name ):
64+ types = docs [name ]["type" ]
65+ type_names = ""
66+ for sub_type in types :
67+ type_names += name_for_uuid (doc_types , sub_type ) + "/"
68+ return type_names [:- 1 ]
69+
70+ def types_as_cbor (docs , name ):
71+ types = docs [name ]["type" ]
72+ type_names = "["
73+ for sub_type in types :
74+ type_names += uuid_as_cbor (sub_type ) + ",<br/>"
75+ return type_names [:- 6 ] + "]"
76+
77+
78+ def doc_type_details (env ):
79+ """
80+ Generate a Document Type Detailed Summary from the Document Specifications Data
81+ """
82+
83+ try :
84+ doc_data = get_signed_doc_data (env )
85+ doc_types = doc_data ["base_types" ]
86+ docs = doc_data ["docs" ]
87+
88+ doc_type_details = """
89+ | Document Type | Base Types | [CBOR] | Specification |
90+ | :--- | :--- | :--- | :--- |
91+ """
92+
93+ for k in docs :
94+ doc_type_details += f"| { k } | { base_types (docs ,doc_types ,k )} | { types_as_cbor (docs ,k )} | [Specification]({ name_to_spec_link (k )} ) | \n "
95+
96+ return doc_type_details
97+ except Exception as exc :
98+ return f"{ exc } "
99+
100+ #class env:
101+ # project_dir = "/home/steven/Development/iohk/catalyst-libs/specs"
102+
103+ #if __name__ == '__main__':
104+
105+ # print(doc_type_details(env))
106+ # print(doc_type_summary(env))
0 commit comments