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 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")
|
|
|
|
if len(m.__all__) > 0:
|
|
|
|
fout.write(" :members: " + ", ".join(m.__all__) + "\n")
|
|
|
|
fout.write(" :inherited-members:\n")
|
|
|
|
fout.write("\n")
|
|
|
|
if name in children:
|
|
|
|
fout.write("子模块\n------\n\n.. toctree::\n\n")
|
|
|
|
for module in children[name]:
|
|
|
|
fout.write(" " + module + "\n")
|
2019-08-19 21:48:41 +08:00
|
|
|
|
|
|
|
|
2019-08-26 10:17:43 +08:00
|
|
|
def main():
|
|
|
|
modules, to_doc, children = find_all_modules()
|
|
|
|
for name in to_doc:
|
|
|
|
create_rst_file(modules, name, children)
|
2019-08-19 21:48:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-08-26 10:17:43 +08:00
|
|
|
main()
|