1818
1919from params import IntegerParam , StringParam
2020
21+ LOG_PREFIX = "[libparams-doc]"
22+
2123logger = logging .getLogger (__name__ )
2224
2325LANGUAGE_C = 0
@@ -82,6 +84,18 @@ def write_parameters(file, all_params : list) -> None:
8284 else :
8385 file .write ("The node doesn't have registers:\n \n " )
8486
87+ def _write_if_changed (path , content : str ) -> str :
88+ if os .path .exists (path ):
89+ with open (path , 'r' , encoding = "utf-8" ) as existing_file :
90+ if existing_file .read () == content :
91+ return "unchanged"
92+ status = "updated"
93+ else :
94+ status = "added"
95+ with open (path , 'w' , encoding = "utf-8" ) as out_file :
96+ out_file .write (content )
97+ return status
98+
8599def generate_markdown_doc (cyphal_pubs : dict ,
86100 cyphal_subs : dict ,
87101 all_params : list ,
@@ -91,21 +105,53 @@ def generate_markdown_doc(cyphal_pubs : dict,
91105 assert isinstance (all_params , list )
92106 assert isinstance (output_markdown_filename , str )
93107
94- with open (output_markdown_filename , 'w' , encoding = "utf-8" ) as file :
95- if len (cyphal_pubs ) + len (cyphal_subs ) >= 1 :
96- file .write ("The node has the following interface:\n \n " )
108+ content = ""
109+ if len (cyphal_pubs ) + len (cyphal_subs ) >= 1 :
110+ content += "The node has the following interface:\n \n "
111+
112+ if len (cyphal_pubs ) >= 1 :
113+ content += "Cyphal Publishers:\n "
114+ content += _render_cyphal_topics (cyphal_pubs )
115+
116+ if len (cyphal_subs ) >= 1 :
117+ content += "Cyphal Subscribers:\n "
118+ content += _render_cyphal_topics (cyphal_subs )
97119
98- if len (cyphal_pubs ) >= 1 :
99- file .write ("Cyphal Publishers:\n " )
100- write_cyphal_topics (file , cyphal_pubs )
120+ content += _render_parameters (all_params )
101121
102- if len (cyphal_subs ) >= 1 :
103- file .write ("Cyphal Subscribers:\n " )
104- write_cyphal_topics (file , cyphal_subs )
122+ content += "> This docs was automatically generated. Do not edit it manually.\n \n "
123+ return _write_if_changed (output_markdown_filename , content )
105124
106- write_parameters (file , all_params )
125+ def _render_cyphal_topics (cyphal_topics : dict ) -> str :
126+ assert isinstance (cyphal_topics , dict )
107127
108- file .write ("> This docs was automatically generated. Do not edit it manually.\n \n " )
128+ content = "| Data type and topic name | Description |\n "
129+ content += "| ------------------------- | ----------- |\n "
130+ for port_name , port_info in cyphal_topics .items ():
131+ data_type = port_data_type_to_md (port_info ['data_type' ])
132+ topic_name = port_name [11 :]
133+ note = port_info .get ('note' )
134+ note = note .replace ('\n ' , '</br>' ) if note is not None else ""
135+ content += f"| { data_type } </br> { topic_name } | { note } |\n "
136+ content += "\n "
137+ return content
138+
139+ def _render_parameters (all_params : list ) -> str :
140+ assert isinstance (all_params , list )
141+
142+ content = ""
143+ if len (all_params ) >= 1 :
144+ content += "The node has the following registers:\n \n "
145+ content += "| Register name | Description |\n "
146+ content += "| ----------------------- | ----------- |\n "
147+
148+ for param in all_params :
149+ param .name = param .name .replace ('"' , '' )
150+ content += f"| { param .name .ljust (23 )} | { param .note } |\n "
151+ content += "\n "
152+ else :
153+ content += "The node doesn't have registers:\n \n "
154+ return content
109155
110156def parse_yaml_files (input_yaml_files : list ) -> Tuple [dict , dict , list ]:
111157 assert isinstance (input_yaml_files , list ) and len (input_yaml_files ) != 0
@@ -147,16 +193,17 @@ def parse_yaml_files_and_generate_doc(input_yaml_files : list, output_markdown_p
147193 os .makedirs (directory )
148194
149195 cyphal_pubs , cyphal_subs , all_params = parse_yaml_files (input_yaml_files )
150- generate_markdown_doc (cyphal_pubs , cyphal_subs , all_params , output_markdown_path )
196+ return generate_markdown_doc (cyphal_pubs , cyphal_subs , all_params , output_markdown_path )
151197
152198if __name__ == "__main__" :
153- logging .basicConfig (level = logging .INFO )
199+ logging .basicConfig (level = logging .INFO , format = f" { LOG_PREFIX } [%(levelname)s] %(message)s" )
154200 logger .setLevel (logging .INFO )
155201
156202 parser = ArgumentParser (description = __doc__ )
157203 parser .add_argument ('files' , nargs = '+' , help = 'YAML files to process' )
158204 parser .add_argument ("--output" , type = str , required = False , default = 'README.md' )
159205 args = parser .parse_args ()
160206
161- print ("Params docs generator:" )
162- parse_yaml_files_and_generate_doc (args .files , args .output )
207+ logger .info ("Generating docs: output=%s, files=%d" , args .output , len (args .files ))
208+ status = parse_yaml_files_and_generate_doc (args .files , args .output )
209+ logger .info ("Docs %s" , status )
0 commit comments