-
Notifications
You must be signed in to change notification settings - Fork 256
/
_summarize.py
83 lines (66 loc) · 2.31 KB
/
_summarize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import ast
import tempfile
import webbrowser
from collections import defaultdict
from glob import glob
from textwrap import indent
from jinja2 import Template
FOUND = defaultdict(dict)
NeedRefactor = object()
class Visitor1pass(ast.NodeVisitor):
def __init__(self, module):
self.module = module
def visit_Call(self, call):
if hasattr(call.func, 'attr') and call.func.attr == "extend":
if len(call.args) == 2:
if call.func.attr == "extend" and call.func.value.id == "cmd":
funcname = call.args[0].value
FOUND[self.module][funcname] = None
class Visitor2Pass(ast.NodeVisitor):
def __init__(self, module):
self.module = module
def visit_FunctionDef(self, functionDef):
if functionDef.col_offset == 0 and functionDef.name == "__init__":
FOUND[self.module] = NeedRefactor
class Visitor3pass(ast.NodeVisitor):
def __init__(self, module):
self.module = module
def visit_FunctionDef(self, functiondef):
if functiondef.name in FOUND[self.module]:
try:
if isinstance(functiondef.body[0].value.value, str):
docstring = functiondef.body[0].value.value
docstring = indent(docstring, " " * 4)
FOUND[self.module][functiondef.name] = docstring
except Exception as exc:
pass
for module in [*glob("*.py"), *glob("plugins/*.py")]:
if module.startswith('_'):
continue
with open(module) as module_file:
src = ''.join(module_file.readlines())
tree = ast.parse(src, filename=module)
v = Visitor1pass(module)
v.visit(tree)
v = Visitor2Pass(module)
v.visit(tree)
if FOUND[module] is not NeedRefactor:
v = Visitor3pass(module)
v.visit(tree)
else:
del FOUND[module]
print("%s:: NeedRefactor" % module)
tmpl = Template("""
# PyMOL Script Repo: Plugin List
{% for module in commands %}
* ## {{ module }}
{% for command in commands[module] %}
* # {{ command }}
```{{ commands[module][command] }}```
{% endfor %}
{% endfor %}
""")
_, path = tempfile.mkstemp(suffix=".md")
with open(path, "w") as fd:
fd.write(tmpl.render(commands=FOUND))
print("DOCUMENTATION: %s" % path)