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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 name != "fastNLP.core" and len(m.__all__) > 0:
  64. fout.write(" :members: " + ", ".join(m.__all__) + "\n")
  65. short = name[len("fastNLP."):]
  66. if not (short.startswith('models') or short.startswith('modules') or short.startswith('embeddings')):
  67. fout.write(" :inherited-members:\n")
  68. fout.write("\n")
  69. if name in children:
  70. fout.write("子模块\n------\n\n.. toctree::\n :maxdepth: 1\n\n")
  71. for module in children[name]:
  72. fout.write(" " + module + "\n")
  73. def check_file(m, name):
  74. for item, obj in inspect.getmembers(m):
  75. if inspect.isclass(obj) and obj.__module__ == name:
  76. print(obj)
  77. if inspect.isfunction(obj) and obj.__module__ == name:
  78. print("FUNC", obj)
  79. def check_files(modules):
  80. for name in sorted(modules.keys()):
  81. if name == 'fastNLP.core.utils':
  82. check_file(modules[name], name)
  83. def main():
  84. print(_colored_string('Getting modules...', "Blue"))
  85. modules, to_doc, children = find_all_modules()
  86. print(_colored_string('Done!', "Green"))
  87. print(_colored_string('Creating rst files...', "Blue"))
  88. for name in to_doc:
  89. create_rst_file(modules, name, children)
  90. print(_colored_string('Done!', "Green"))
  91. print(_colored_string('Checking all files...', "Blue"))
  92. check_files(modules)
  93. print(_colored_string('Done!', "Green"))
  94. if __name__ == "__main__":
  95. main()