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 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import inspect
  2. import os
  3. import sys
  4. def _colored_string(string: str, color: str or int) -> str:
  5. """在终端中显示一串有颜色的文字
  6. :param string: 在终端中显示的文字
  7. :param color: 文字的颜色
  8. :return:
  9. """
  10. if isinstance(color, str):
  11. color = {
  12. "black": 30, "Black": 30, "BLACK": 30,
  13. "red": 31, "Red": 31, "RED": 31,
  14. "green": 32, "Green": 32, "GREEN": 32,
  15. "yellow": 33, "Yellow": 33, "YELLOW": 33,
  16. "blue": 34, "Blue": 34, "BLUE": 34,
  17. "purple": 35, "Purple": 35, "PURPLE": 35,
  18. "cyan": 36, "Cyan": 36, "CYAN": 36,
  19. "white": 37, "White": 37, "WHITE": 37
  20. }[color]
  21. return "\033[%dm%s\033[0m" % (color, string)
  22. def find_all_modules():
  23. modules = {}
  24. children = {}
  25. to_doc = set()
  26. root = '../fastNLP'
  27. for path, dirs, files in os.walk(root):
  28. for file in files:
  29. if file.endswith('.py'):
  30. name = ".".join(path.split('/')[1:])
  31. if file.split('.')[0] != "__init__":
  32. name = name + '.' + file.split('.')[0]
  33. __import__(name)
  34. m = sys.modules[name]
  35. modules[name] = m
  36. try:
  37. m.__all__
  38. except:
  39. print(name, "__all__ missing")
  40. continue
  41. if m.__doc__ is None:
  42. print(name, "__doc__ missing")
  43. continue
  44. if "undocumented" not in m.__doc__:
  45. to_doc.add(name)
  46. for module in to_doc:
  47. t = ".".join(module.split('.')[:-1])
  48. if t in to_doc:
  49. if t not in children:
  50. children[t] = set()
  51. children[t].add(module)
  52. for m in children:
  53. children[m] = sorted(children[m])
  54. return modules, to_doc, children
  55. def create_rst_file(modules, name, children):
  56. m = modules[name]
  57. with open("./source/" + name + ".rst", "w") as fout:
  58. t = "=" * len(name)
  59. fout.write(name + "\n")
  60. fout.write(t + "\n")
  61. fout.write("\n")
  62. fout.write(".. automodule:: " + name + "\n")
  63. if len(m.__all__) > 0:
  64. fout.write(" :members: " + ", ".join(m.__all__) + "\n")
  65. fout.write(" :inherited-members:\n")
  66. fout.write("\n")
  67. if name in children:
  68. fout.write("子模块\n------\n\n.. toctree::\n\n")
  69. for module in children[name]:
  70. fout.write(" " + module + "\n")
  71. def check_file(m, name):
  72. for item, obj in inspect.getmembers(m):
  73. if inspect.isclass(obj) and obj.__module__ == name:
  74. print(obj)
  75. if inspect.isfunction(obj) and obj.__module__ == name:
  76. print("FUNC", obj)
  77. def check_files(modules):
  78. for name in sorted(modules.keys()):
  79. if name == 'fastNLP.core.utils':
  80. check_file(modules[name], name)
  81. def main():
  82. print(_colored_string('Getting modules...', "Blue"))
  83. modules, to_doc, children = find_all_modules()
  84. print(_colored_string('Done!', "Green"))
  85. print(_colored_string('Creating rst files...', "Blue"))
  86. for name in to_doc:
  87. create_rst_file(modules, name, children)
  88. print(_colored_string('Done!', "Green"))
  89. print(_colored_string('Checking all files...', "Blue"))
  90. check_files(modules)
  91. print(_colored_string('Done!', "Green"))
  92. if __name__ == "__main__":
  93. main()