generate_docstrings.py 4.6 KB

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