|
| 1 | +import ast |
| 2 | +import tempfile |
| 3 | +import webbrowser |
| 4 | +from collections import defaultdict |
| 5 | +from glob import glob |
| 6 | + |
| 7 | +from jinja2 import Template |
| 8 | + |
| 9 | +FOUND = defaultdict(dict) |
| 10 | + |
| 11 | + |
| 12 | +class Visitor1pass(ast.NodeVisitor): |
| 13 | + def __init__(self, module): |
| 14 | + self.module = module |
| 15 | + |
| 16 | + def visit_Call(self, call): |
| 17 | + if hasattr(call.func, 'attr') and call.func.attr == "extend": |
| 18 | + if len(call.args) == 2: |
| 19 | + if call.func.attr == "extend" and call.func.value.id == "cmd": |
| 20 | + funcname = call.args[0].value |
| 21 | + FOUND[self.module][funcname] = None |
| 22 | + |
| 23 | + |
| 24 | +class Visitor2pass(ast.NodeVisitor): |
| 25 | + def __init__(self, module): |
| 26 | + self.module = module |
| 27 | + |
| 28 | + def visit_FunctionDef(self, functiondef): |
| 29 | + if functiondef.name in FOUND[self.module]: |
| 30 | + try: |
| 31 | + if isinstance(functiondef.body[0].value.value, str): |
| 32 | + FOUND[self.module][functiondef.name] = functiondef.body[0].value.value |
| 33 | + except Exception as exc: |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +for module in [*glob("*.py"), *glob("plugins/*py")]: |
| 38 | + if module.startswith('_'): |
| 39 | + continue |
| 40 | + with open(module) as module_file: |
| 41 | + src = ''.join(module_file.readlines()) |
| 42 | + tree = ast.parse(src) |
| 43 | + |
| 44 | + v = Visitor1pass(module) |
| 45 | + v.visit(tree) |
| 46 | + |
| 47 | + v = Visitor2pass(module) |
| 48 | + v.visit(tree) |
| 49 | + |
| 50 | + # module = ast.parse(src, filename=module) |
| 51 | + |
| 52 | +tmpl = Template( |
| 53 | + """ |
| 54 | + <header> |
| 55 | + <h1>Plugin Commmands for PyMOL</h1> |
| 56 | + <h2>Generated by Pedro Sousa Lacerda ([email protected]>) |
| 57 | + </header> |
| 58 | + <body> |
| 59 | + |
| 60 | + {% for module in commands %} |
| 61 | + <h2>{{ module }}</h2> |
| 62 | + {% for command in commands[module] %} |
| 63 | + <h3>{{ command }}</h3> |
| 64 | + <pre>{{ commands[module][command] }}</pre> |
| 65 | + {% endfor %} |
| 66 | + `{% endfor %}` |
| 67 | + </body> |
| 68 | +""") |
| 69 | +_, path = tempfile.mkstemp(suffix=".html") |
| 70 | +with open(path, "w") as fd: |
| 71 | + fd.write(tmpl.render(commands=FOUND)) |
| 72 | +webbrowser.open(path) |
0 commit comments