@@ -48,26 +48,6 @@ def indent(s: str, n: int):
4848 return tw_indent (s , n * ' ' )
4949
5050
51- def clear_dirs (package_path : Path ):
52- ''' Remove generated code. '''
53- def rmdir (path ):
54- for subpath in path .iterdir ():
55- if subpath .is_file ():
56- subpath .unlink ()
57- elif subpath .is_dir ():
58- rmdir (subpath )
59- path .rmdir ()
60-
61- try :
62- (package_path / '__init__.py' ).unlink ()
63- except FileNotFoundError :
64- pass
65-
66- for subpath in package_path .iterdir ():
67- if subpath .is_dir ():
68- rmdir (subpath )
69-
70-
7151def inline_doc (description ) -> str :
7252 ''' Generate an inline doc, e.g. ``#: This type is a ...`` '''
7353 if not description :
@@ -741,6 +721,7 @@ def get_refs(self):
741721class CdpDomain :
742722 ''' A CDP domain contains metadata, types, commands, and events. '''
743723 domain : str
724+ description : typing .Optional [str ]
744725 experimental : bool
745726 dependencies : typing .List [str ]
746727 types : typing .List [CdpType ]
@@ -762,6 +743,7 @@ def from_json(cls, domain: dict):
762743
763744 return cls (
764745 domain_name ,
746+ domain .get ('description' ),
765747 domain .get ('experimental' , False ),
766748 domain .get ('dependencies' , list ()),
767749 [CdpType .from_json (type ) for type in types ],
@@ -826,6 +808,21 @@ def generate_imports(self):
826808
827809 return code
828810
811+ def generate_sphinx (self ) -> str :
812+ '''
813+ Generate a Sphinx document for this domain.
814+ '''
815+ docs = self .domain + '\n '
816+ docs += '=' * len (self .domain ) + '\n \n '
817+
818+ if self .description :
819+ docs += f'{ self .description } \n \n '
820+
821+ docs += f'.. automodule:: cdp.{ self .module } \n '
822+ docs += ' :members:\n '
823+
824+ return docs
825+
829826
830827def parse (json_path , output_path ):
831828 '''
@@ -862,6 +859,22 @@ def generate_init(init_path, domains):
862859 init_file .write ('import cdp.{}\n ' .format (domain .module ))
863860
864861
862+ def generate_docs (docs_path , domains ):
863+ '''
864+ Generate Sphinx documents for each domain.
865+ '''
866+ logger .info ('Generating Sphinx documents' )
867+
868+ # Remove generated documents
869+ for subpath in docs_path .iterdir ():
870+ subpath .unlink ()
871+
872+ # Generate document for each domain
873+ for domain in domains :
874+ doc = docs_path / f'{ domain .module } .rst'
875+ with doc .open ('w' ) as f :
876+ f .write (domain .generate_sphinx ())
877+
865878def main ():
866879 ''' Main entry point. '''
867880 here = Path (__file__ ).parent .resolve ()
@@ -871,8 +884,13 @@ def main():
871884 ]
872885 output_path = here .parent / 'cdp'
873886 output_path .mkdir (exist_ok = True )
874- clear_dirs (output_path )
875887
888+ # Remove generated code
889+ for subpath in output_path .iterdir ():
890+ if subpath .is_file () and subpath .name not in ('py.typed' , 'util.py' ):
891+ subpath .unlink ()
892+
893+ # Parse domains
876894 domains = list ()
877895 for json_path in json_paths :
878896 logger .info ('Parsing JSON file %s' , json_path )
@@ -899,6 +917,9 @@ def main():
899917 init_path = output_path / '__init__.py'
900918 generate_init (init_path , domains )
901919
920+ docs_path = here .parent / 'docs' / 'api'
921+ generate_docs (docs_path , domains )
922+
902923 py_typed_path = output_path / 'py.typed'
903924 py_typed_path .touch ()
904925
0 commit comments