You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

count.py 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import os
  2. import sys
  3. def find_all_modules():
  4. modules = {}
  5. children = {}
  6. to_doc = set()
  7. root = '../fastNLP'
  8. for path, dirs, files in os.walk(root):
  9. for file in files:
  10. if file.endswith('.py'):
  11. name = ".".join(path.split('/')[1:])
  12. if file.split('.')[0] != "__init__":
  13. name = name + '.' + file.split('.')[0]
  14. __import__(name)
  15. m = sys.modules[name]
  16. modules[name] = m
  17. try:
  18. m.__all__
  19. except:
  20. print(name, "__all__ missing")
  21. continue
  22. if m.__doc__ is None:
  23. print(name, "__doc__ missing")
  24. continue
  25. if "undocumented" not in m.__doc__:
  26. to_doc.add(name)
  27. for module in to_doc:
  28. t = ".".join(module.split('.')[:-1])
  29. if t in to_doc:
  30. if t not in children:
  31. children[t] = set()
  32. children[t].add(module)
  33. for m in children:
  34. children[m] = sorted(children[m])
  35. return modules, to_doc, children
  36. def create_rst_file(modules, name, children):
  37. m = modules[name]
  38. with open("./source/" + name + ".rst", "w") as fout:
  39. t = "=" * len(name)
  40. fout.write(name + "\n")
  41. fout.write(t + "\n")
  42. fout.write("\n")
  43. fout.write(".. automodule:: " + name + "\n")
  44. if len(m.__all__) > 0:
  45. fout.write(" :members: " + ", ".join(m.__all__) + "\n")
  46. fout.write(" :inherited-members:\n")
  47. fout.write("\n")
  48. if name in children:
  49. fout.write("子模块\n------\n\n.. toctree::\n\n")
  50. for module in children[name]:
  51. fout.write(" " + module + "\n")
  52. def main():
  53. modules, to_doc, children = find_all_modules()
  54. for name in to_doc:
  55. create_rst_file(modules, name, children)
  56. if __name__ == "__main__":
  57. main()