generate_docstrings.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python
  2. #
  3. # Syntax: generate_docstrings.py <path to libigl C++ header_files> <path to python binding C++ 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 = os.path.relpath(path, basepath)
  33. f_clean = f_clean.replace(postfix, "")
  34. f_clean = f_clean.replace(prefix, "")
  35. f_clean = f_clean.replace("/", "_")
  36. f_clean = f_clean.replace("\\", "_")
  37. f_clean = f_clean.replace(" ", "_")
  38. f_clean = f_clean.replace(".", "_")
  39. return f_clean
  40. if __name__ == '__main__':
  41. if len(sys.argv) != 3:
  42. print('Syntax: %s generate_docstrings.py <path to libigl C++ header_files> <path to python binding C++ files>' % sys.argv[0])
  43. exit(-1)
  44. # List all files in the given folder and subfolders
  45. cpp_base_path = sys.argv[1]
  46. py_base_path = sys.argv[2]
  47. cpp_file_paths, cpp_root_file_paths = get_filepaths(cpp_base_path)
  48. py_file_paths, py_root_file_paths = get_filepaths(py_base_path)
  49. # Add all the .h filepaths to a dict
  50. mapping = {}
  51. for f in cpp_file_paths:
  52. if f.endswith(".h"):
  53. name = get_name_from_path(f, cpp_base_path, "", ".h")
  54. mapping[name] = f
  55. # Add all python binding files to a list
  56. implemented_names = []
  57. core_implemented_names = []
  58. for f in py_file_paths:
  59. if f.endswith(".cpp"):
  60. name = get_name_from_path(f, py_base_path, "py_", ".cpp")
  61. implemented_names.append(name)
  62. if f in py_root_file_paths:
  63. core_implemented_names.append(name)
  64. implemented_names.sort()
  65. core_implemented_names.sort()
  66. # Create a list of cpp header files for which a python binding file exists
  67. files_to_parse = []
  68. for n in implemented_names:
  69. if n not in mapping:
  70. print("No cpp header file for python function %s found." % n)
  71. continue
  72. files_to_parse.append(mapping[n])
  73. # print(mapping[n])
  74. # Parse c++ header files
  75. job_count = cpu_count()
  76. dicts = Parallel(n_jobs=job_count)(delayed(parse)(path) for path in files_to_parse)
  77. hpplines = []
  78. cpplines = []
  79. for idx, n in enumerate(implemented_names):
  80. d = dicts[idx]
  81. contained_elements = sum(map(lambda x: len(x), d.values()))
  82. # Check for files that don't contain functions/enums/classes
  83. if contained_elements == 0:
  84. print("Function %s contains no parseable content in cpp header. Something might be wrong." % n)
  85. continue
  86. else:
  87. names = []
  88. namespaces = "_".join(d["namespaces"]) # Assumption that all entities lie in deepest namespace
  89. for f in d["functions"]:
  90. h_string = "extern const char *__doc_" + namespaces + "_" + f.name + ";\n"
  91. docu_string = "See " + f.name + " for the documentation."
  92. if f.documentation:
  93. docu_string = f.documentation
  94. cpp_string = "const char *__doc_" + namespaces + "_" + f.name + " = R\"igl_Qu8mg5v7(" + docu_string + ")igl_Qu8mg5v7\";\n"
  95. if f.name not in names: # Prevent multiple additions of declarations, TODO: Possible fix is to merge comments and add them to all functions
  96. hpplines.append(h_string)
  97. cpplines.append(cpp_string)
  98. names.append(f.name)
  99. # Change directory to become independent of execution directory
  100. path = os.path.dirname(__file__)
  101. if path != "":
  102. os.chdir(path)
  103. # Update the two files py_doc.h and py_doc.cpp
  104. with open('../py_doc.h', 'w') as fh:
  105. fh.writelines(hpplines)
  106. with open('../py_doc.cpp', 'w') as fc:
  107. fc.writelines(cpplines)
  108. # Write python_shared_cpp file
  109. tpl = Template(filename='python_shared.mako')
  110. rendered = tpl.render(functions=implemented_names)
  111. with open("../python_shared.cpp", 'w') as fs:
  112. fs.write(rendered)
  113. # Write py_igl_cpp file with all core library files
  114. tpl = Template(filename='py_igl.mako')
  115. rendered = tpl.render(functions=core_implemented_names)
  116. with open("../py_igl.cpp", 'w') as fs:
  117. fs.write(rendered)