mk_py_doc.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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')
  45. tmp_string = tmp.read()
  46. tmp.close()
  47. h_string = "extern const char *__doc_" + f_clean + ";\n"
  48. cpp_string = "const char *__doc_" + f_clean + " = R\"igl_Qu8mg5v7(" + tmp_string + ")igl_Qu8mg5v7\";\n"
  49. fh.write(h_string)
  50. fc.write(cpp_string)
  51. # Close files
  52. fh.close()
  53. fc.close()