docstring_generator.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import doxy2swig # local file
4 import glob
5 import os
6 import sys
7 import subprocess
8 import argparse
9 
10 
11 def file_name(path):
12  """Returns the file name part of a path/file string."""
13  head, tail = os.path.split(path)
14  return tail
15 
16 
17 def clean_errors(file):
18  """Removes any instance of ::throw in a file that doxy2swig produces."""
19  # doxy2swig.py has a minor bug where it can make documentation
20  # slightly incorrect. For example, if Cat is a class which has a
21  # constructor that can throw an exception; (i.e. Cat::Cat() throws())
22  # doxy2swig may create documentation for Cat::Cat::throws, which does
23  # not exist. Therefore, they are simply removed to prevent SWIG
24  # from crashing.
25 
26  # read in
27  f = open(file, 'r')
28  data = f.read()
29  f.close()
30 
31  # remove all ::throw
32  data = data.replace('::throw', '')
33 
34  # write out
35  f = open(file, 'w')
36  f.write(data)
37  f.close()
38 
39 
40 def generate_docs(args):
41  # This present script should live in '$gnsstk_root/swig',
42  # so to get the $gnsstk_root, just strip off the file name
43  # and use split() to pop off the 'swig' directory from the
44  # remaining file path.
45  script_fullpath_name = os.path.realpath(__file__)
46  path_gnsstk_swig = os.path.dirname( script_fullpath_name )
47  (gnsstk_root, swig_dir) = os.path.split( path_gnsstk_swig )
48 
49  # Build a list of all the XML files that dOxygen output previously
50  xml_glob_pattern = os.path.sep.join((args.src_dir, 'xml', '**', '*.xml'))
51  xml_files = glob.glob( xml_glob_pattern, recursive=True)
52  num_files = len( xml_files )
53  if num_files == 0:
54  print('WARNING: No doxygen-xml files found, docstrings cannot be generated.')
55  return
56 
57  # create directories for swig doc files
58  path_gnsstk_swig_doc = args.dst_dir
59  if not os.path.exists( path_gnsstk_swig_doc ):
60  os.makedirs( path_gnsstk_swig_doc )
61 
62  # for each doxygen xml file, create a converted swig .i file for associated docstrings
63  for f_xml in xml_files:
64  (f, xml_ext) = os.path.splitext( f_xml ) # remove the .xml ending
65  name_root = file_name(f)
66  output_file = path_gnsstk_swig_doc + os.path.sep + name_root + '.i'
67  if ('index' not in name_root): # don't try to use index.xml
68  try:
69  doxy2swig.convert(f_xml, output_file, False, False)
70  clean_errors(output_file)
71  print('Parsed ', f, 'to', output_file)
72  except Exception as e:
73  print('ERROR:', f_xml, 'can not be parsed')
74  print('\t', e)
75 
76  # Add the includes for each converted xml file.i to the doc.i file
77  out_file = open(path_gnsstk_swig_doc + os.path.sep + 'doc.i', 'w')
78  out_file.write('// This is an AUTO-GENERATED file by docstring_generator.py.\n')
79  out_file.write('// Do not modify it unless you know what you are doing.\n')
80  doc_files = glob.glob(path_gnsstk_swig_doc + os.path.sep + '*.i')
81  for f_i in doc_files:
82  if 'doc.i' not in f_i:
83  out_file.write('%include ' + f_i + '\n')
84  out_file.close()
85  print('\nFinished with documentation.')
86 
87 
88 if __name__ == '__main__':
89  parser = argparse.ArgumentParser(description="""
90  GNSSTk python-swig binding documentation generator.
91  This reads every file in the $doc_dir/xml (where doxygen places its xml)
92  Then uses $gnsstk_root/swig/doxy2swig.py to create docstring output for a SWIG .i file.
93  These .i files are placed in $gnsstk_root/swig/doc.
94  Then doc.i is auto-generated to include all of these new files in the doc/ folder.
95  """)
96 
97  parser.add_argument("-d", default=0, dest="debug", action="count",
98  help="Increase the level of debug output.")
99 
100  parser.add_argument(dest="src_dir",
101  help="Directory where the doxygen xml files are.")
102 
103  parser.add_argument(dest="dst_dir",
104  help="Directory to write swig.i files to.")
105 
106  args = parser.parse_args()
107 
108  generate_docs(args)
docstring_generator.generate_docs
def generate_docs(args)
Definition: docstring_generator.py:40
docstring_generator.file_name
def file_name(path)
Definition: docstring_generator.py:11
doxy2swig.convert
def convert(input, output, include_function_definition=True, quiet=False)
Definition: doxy2swig.py:430
docstring_generator.clean_errors
def clean_errors(file)
Definition: docstring_generator.py:17


gnsstk
Author(s):
autogenerated on Wed Oct 25 2023 02:40:38