2019-08-26 14:58:36 +08:00
|
|
|
import inspect
|
2019-08-19 21:48:41 +08:00
|
|
|
import os
|
2019-08-26 10:17:43 +08:00
|
|
|
import sys
|
2019-08-19 21:48:41 +08:00
|
|
|
|
|
|
|
|
2019-08-26 14:58:36 +08:00
|
|
|
def _colored_string(string: str, color: str or int) -> str:
|
|
|
|
"""在终端中显示一串有颜色的文字
|
|
|
|
:param string: 在终端中显示的文字
|
|
|
|
:param color: 文字的颜色
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if isinstance(color, str):
|
|
|
|
color = {
|
|
|
|
"black": 30, "Black": 30, "BLACK": 30,
|
|
|
|
"red": 31, "Red": 31, "RED": 31,
|
|
|
|
"green": 32, "Green": 32, "GREEN": 32,
|
|
|
|
"yellow": 33, "Yellow": 33, "YELLOW": 33,
|
|
|
|
"blue": 34, "Blue": 34, "BLUE": 34,
|
|
|
|
"purple": 35, "Purple": 35, "PURPLE": 35,
|
|
|
|
"cyan": 36, "Cyan": 36, "CYAN": 36,
|
|
|
|
"white": 37, "White": 37, "WHITE": 37
|
|
|
|
}[color]
|
|
|
|
return "\033[%dm%s\033[0m" % (color, string)
|
|
|
|
|
|
|
|
|
2019-08-26 10:17:43 +08:00
|
|
|
def find_all_modules():
|
|
|
|
modules = {}
|
|
|
|
children = {}
|
|
|
|
to_doc = set()
|
|
|
|
root = '../fastNLP'
|
|
|
|
for path, dirs, files in os.walk(root):
|
2019-08-19 21:48:41 +08:00
|
|
|
for file in files:
|
|
|
|
if file.endswith('.py'):
|
|
|
|
name = ".".join(path.split('/')[1:])
|
|
|
|
if file.split('.')[0] != "__init__":
|
|
|
|
name = name + '.' + file.split('.')[0]
|
2019-08-26 10:17:43 +08:00
|
|
|
__import__(name)
|
|
|
|
m = sys.modules[name]
|
|
|
|
modules[name] = m
|
|
|
|
try:
|
|
|
|
m.__all__
|
|
|
|
except:
|
|
|
|
print(name, "__all__ missing")
|
|
|
|
continue
|
|
|
|
if m.__doc__ is None:
|
|
|
|
print(name, "__doc__ missing")
|
|
|
|
continue
|
|
|
|
if "undocumented" not in m.__doc__:
|
|
|
|
to_doc.add(name)
|
|
|
|
for module in to_doc:
|
|
|
|
t = ".".join(module.split('.')[:-1])
|
|
|
|
if t in to_doc:
|
|
|
|
if t not in children:
|
|
|
|
children[t] = set()
|
|
|
|
children[t].add(module)
|
|
|
|
for m in children:
|
|
|
|
children[m] = sorted(children[m])
|
|
|
|
return modules, to_doc, children
|
2019-08-19 21:48:41 +08:00
|
|
|
|
|
|
|
|
2019-08-26 10:17:43 +08:00
|
|
|
def create_rst_file(modules, name, children):
|
|
|
|
m = modules[name]
|
|
|
|
with open("./source/" + name + ".rst", "w") as fout:
|
|
|
|
t = "=" * len(name)
|
|
|
|
fout.write(name + "\n")
|
|
|
|
fout.write(t + "\n")
|
|
|
|
fout.write("\n")
|
|
|
|
fout.write(".. automodule:: " + name + "\n")
|
2019-08-27 20:46:05 +08:00
|
|
|
if name != "fastNLP.core" and len(m.__all__) > 0:
|
2019-08-26 10:17:43 +08:00
|
|
|
fout.write(" :members: " + ", ".join(m.__all__) + "\n")
|
2019-08-27 20:46:05 +08:00
|
|
|
if not (name.startswith('fastNLP.models') or name.startswith('fastNLP.modules')):
|
|
|
|
fout.write(" :inherited-members:\n")
|
2019-08-26 10:17:43 +08:00
|
|
|
fout.write("\n")
|
|
|
|
if name in children:
|
2019-08-27 20:46:05 +08:00
|
|
|
fout.write("子模块\n------\n\n.. toctree::\n :maxdepth: 1\n\n")
|
2019-08-26 10:17:43 +08:00
|
|
|
for module in children[name]:
|
|
|
|
fout.write(" " + module + "\n")
|
2019-08-19 21:48:41 +08:00
|
|
|
|
|
|
|
|
2019-08-26 14:58:36 +08:00
|
|
|
def check_file(m, name):
|
|
|
|
for item, obj in inspect.getmembers(m):
|
|
|
|
if inspect.isclass(obj) and obj.__module__ == name:
|
|
|
|
print(obj)
|
|
|
|
if inspect.isfunction(obj) and obj.__module__ == name:
|
|
|
|
print("FUNC", obj)
|
|
|
|
|
|
|
|
|
|
|
|
def check_files(modules):
|
|
|
|
for name in sorted(modules.keys()):
|
|
|
|
if name == 'fastNLP.core.utils':
|
|
|
|
check_file(modules[name], name)
|
|
|
|
|
|
|
|
|
2019-08-26 10:17:43 +08:00
|
|
|
def main():
|
2019-08-26 14:58:36 +08:00
|
|
|
print(_colored_string('Getting modules...', "Blue"))
|
2019-08-26 10:17:43 +08:00
|
|
|
modules, to_doc, children = find_all_modules()
|
2019-08-26 14:58:36 +08:00
|
|
|
print(_colored_string('Done!', "Green"))
|
|
|
|
print(_colored_string('Creating rst files...', "Blue"))
|
2019-08-26 10:17:43 +08:00
|
|
|
for name in to_doc:
|
|
|
|
create_rst_file(modules, name, children)
|
2019-08-26 14:58:36 +08:00
|
|
|
print(_colored_string('Done!', "Green"))
|
|
|
|
print(_colored_string('Checking all files...', "Blue"))
|
|
|
|
check_files(modules)
|
|
|
|
print(_colored_string('Done!', "Green"))
|
2019-08-19 21:48:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-08-26 10:17:43 +08:00
|
|
|
main()
|