generate_docstrings.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python3
  2. #
  3. # Syntax: generate_docstrings.py <path_to_c++_header_files> <path_to_python_files>
  4. #
  5. # Extract documentation from C++ header files to use it in libiglPython bindings
  6. #
  7. import os, sys, glob
  8. from joblib import Parallel, delayed
  9. from multiprocessing import cpu_count
  10. from mako.template import Template
  11. from parser import parse
  12. # http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python
  13. def get_filepaths(directory):
  14. """
  15. This function will generate the file names in a directory
  16. tree by walking the tree either top-down or bottom-up. For each
  17. directory in the tree rooted at directory top (including top itself),
  18. it yields a 3-tuple (dirpath, dirnames, filenames).
  19. """
  20. file_paths = [] # List which will store all of the full filepaths.
  21. root_file_paths = []
  22. # Walk the tree.
  23. for root, directories, files in os.walk(directory):
  24. for filename in files:
  25. # Join the two strings in order to form the full filepath.
  26. filepath = os.path.join(root, filename)
  27. file_paths.append(filepath) # Add it to the list.
  28. if root.endswith(directory): # Add only the files in the root directory
  29. root_file_paths.append(filepath)
  30. return file_paths, root_file_paths # file_paths contains all file paths, core_file_paths only the ones in <directory>
  31. def get_name_from_path(path, basepath, prefix, postfix):
  32. f_clean = path[len(basepath):]
  33. f_clean = f_clean.replace(basepath, "")
  34. f_clean = f_clean.replace(postfix, "")
  35. f_clean = f_clean.replace(prefix, "")
  36. f_clean = f_clean.replace("/", "_")
  37. f_clean = f_clean.replace("\\", "_")
  38. f_clean = f_clean.replace(" ", "_")
  39. f_clean = f_clean.replace(".", "_")
  40. return f_clean
  41. if __name__ == '__main__':
  42. if len(sys.argv) != 3:
  43. print('Syntax: %s <path_to_c++_header_files> <path_to_python_files>' % sys.argv[0])
  44. exit(-1)
  45. # List all files in the given folder and subfolders
  46. cpp_base_path = sys.argv[1]
  47. py_base_path = sys.argv[2]
  48. cpp_file_paths, cpp_root_file_paths = get_filepaths(cpp_base_path)
  49. py_file_paths, py_root_file_paths = get_filepaths(py_base_path)
  50. # Add all the .h filepaths to a dict
  51. mapping = {}
  52. for f in cpp_file_paths:
  53. if f.endswith(".h"):
  54. name = get_name_from_path(f, cpp_base_path, "", ".h")
  55. mapping[name] = f
  56. # Add all python binding files to a list
  57. implemented_names = []
  58. core_implemented_names = []
  59. for f in py_file_paths:
  60. if f.endswith(".cpp"):
  61. name = get_name_from_path(f, py_base_path, "py_", ".cpp")
  62. implemented_names.append(name)
  63. if f in py_root_file_paths:
  64. core_implemented_names.append(name)
  65. implemented_names.sort()
  66. core_implemented_names.sort()
  67. # Create a list of cpp header files for which a python binding file exists
  68. files_to_parse = []
  69. for n in implemented_names:
  70. if n not in mapping:
  71. print("No cpp header file for python function %s found." % n)
  72. continue
  73. files_to_parse.append(mapping[n])
  74. # print(mapping[n])
  75. # Parse c++ header files
  76. job_count = cpu_count()
  77. dicts = Parallel(n_jobs=job_count)(delayed(parse)(path) for path in files_to_parse)
  78. hpplines = []
  79. cpplines = []
  80. for idx, n in enumerate(implemented_names):
  81. d = dicts[idx]
  82. contained_elements = sum(map(lambda x: len(x), d.values()))
  83. # Check for files that don't contain functions/enums/classes
  84. if contained_elements == 0:
  85. print("Function %s contains no parseable content in cpp header. Something might be wrong." % n)
  86. continue
  87. else:
  88. names = []
  89. namespaces = "_".join(d["namespaces"]) # Assumption that all entities lie in deepest namespace
  90. for f in d["functions"]:
  91. h_string = "extern const char *__doc_" + namespaces + "_" + f.name + ";\n"
  92. docu_string = "See " + f.name + " for the documentation."
  93. if f.documentation != "":
  94. docu_string = f.documentation
  95. cpp_string = "const char *__doc_" + namespaces + "_" + f.name + " = R\"igl_Qu8mg5v7(" + docu_string + ")igl_Qu8mg5v7\";\n"
  96. if f.name not in names: # Prevent multiple additions of declarations, TODO: Possible fix is to merge comments and add them to all functions
  97. hpplines.append(h_string)
  98. cpplines.append(cpp_string)
  99. names.append(f.name)
  100. # Change directory to become independent of execution directory
  101. path = os.path.dirname(__file__)
  102. if path != "":
  103. os.chdir(path)
  104. # Update the two files py_doc.h and py_doc.cpp
  105. with open('../py_doc.h', 'w') as fh:
  106. fh.writelines(hpplines)
  107. with open('../py_doc.cpp', 'w') as fc:
  108. fc.writelines(cpplines)
  109. # Write python_shared_cpp file
  110. tpl = Template(filename='python_shared.mako')
  111. rendered = tpl.render(functions=implemented_names)
  112. with open("../python_shared.cpp", 'w') as fs:
  113. fs.write(rendered)
  114. # Write py_igl_cpp file with all core library files
  115. tpl = Template(filename='py_igl.mako')
  116. rendered = tpl.render(functions=core_implemented_names)
  117. with open("../py_igl.cpp", 'w') as fs:
  118. fs.write(rendered)