mk_py_doc.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python3
  2. #
  3. # Syntax: mkdoc.py <path_of_header_files>
  4. #
  5. # Extract documentation from C++ header files to use it in libiglPython bindings
  6. #
  7. import os, sys, glob
  8. #http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python
  9. def get_filepaths(directory):
  10. """
  11. This function will generate the file names in a directory
  12. tree by walking the tree either top-down or bottom-up. For each
  13. directory in the tree rooted at directory top (including top itself),
  14. it yields a 3-tuple (dirpath, dirnames, filenames).
  15. """
  16. file_paths = [] # List which will store all of the full filepaths.
  17. # Walk the tree.
  18. for root, directories, files in os.walk(directory):
  19. for filename in files:
  20. # Join the two strings in order to form the full filepath.
  21. filepath = os.path.join(root, filename)
  22. file_paths.append(filepath) # Add it to the list.
  23. return file_paths # Self-explanatory.
  24. if __name__ == '__main__':
  25. if len(sys.argv) != 2:
  26. print('Syntax: %s <path_of_header_files>' % sys.argv[0])
  27. exit(-1)
  28. # Open two files, py_doc.h and py_doc.cpp
  29. fh = open('py_doc.h', 'w')
  30. fc = open('py_doc.cpp', 'w')
  31. # List all files in the given folder and subfolders
  32. base_path = sys.argv[1]
  33. full_file_paths = get_filepaths(base_path)
  34. # Add all the .h files
  35. for f in full_file_paths:
  36. if f.endswith(".h"):
  37. f_clean = f[len(base_path):]
  38. f_clean = f_clean.replace(base_path, "")
  39. f_clean = f_clean.replace(".h", "")
  40. f_clean = f_clean.replace("/", "_")
  41. f_clean = f_clean.replace("\\", "_")
  42. f_clean = f_clean.replace(" ", "_")
  43. f_clean = f_clean.replace(".", "_")
  44. #tmp = open(f, 'r', encoding="utf8")
  45. tmp_string = f.replace("../include/", "libigl/") # " " # tmp.read()
  46. tmp_string = "See " + tmp_string + " for the documentation."
  47. #tmp.close()
  48. h_string = "extern const char *__doc_" + f_clean + ";\n"
  49. cpp_string = "const char *__doc_" + f_clean + " = R\"igl_Qu8mg5v7(" + tmp_string + ")igl_Qu8mg5v7\";\n"
  50. fh.write(h_string)
  51. fc.write(cpp_string)
  52. # Close files
  53. fh.close()
  54. fc.close()