-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyls
executable file
·137 lines (103 loc) · 3.4 KB
/
pyls
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Utility to give a high level overview of a python script.
Usage:
pyls <filename>
Dependencies:
astunparse or meta
Unsupported:
astor, astmonkey, macropy
Bugs:
meta (v0.4.1) can't handle some assignments. Use astunparse instead.
"""
__author__ = __maintainer__ = "Laurent Verweijen"
__license__ = "GPL3"
import sys
import ast
INDENT_WIDTH = 2
def main():
"""Program entry point."""
with open(sys.argv[1]) as code_file:
parsed = ast.parse(code_file.read())
t = TopCollector()
t.visit(parsed)
print(t)
try:
import astunparse
def unparse(node):
"""Undo the effect of ast.parse using astunparse."""
return astunparse.unparse(node).strip()
except ImportError:
import meta
def unparse(node):
"""Undo the effect of ast.parse using meta.
Install astunparse for a stabler version.
"""
try:
return meta.dump_python_source(node).strip()
except Exception as e:
print('oo', repr(e))
if hasattr(node, 'targets'):
targets = ", ".join(target.id for target in node.targets)
return "{} = BUG <{}>".format(targets, repr(e))
else:
return "BUG <{}>".format(repr(e))
def indent(level):
"""Insert an indent of level."""
return level * INDENT_WIDTH * " "
def output_list(lst, level=0):
"""Convert items in list to str and join them by a new line."""
output = []
for item in lst:
output.append(indent(level) + str(item))
return "\n".join(output)
class TopCollector(ast.NodeVisitor):
def __init__(self):
self.functions = []
self.routines = []
self.classes = []
self.imports = []
self.import_froms = []
self.variables = []
def __str__(self):
output = [
"Routines:", output_list(self.functions, 1), "",
"Classes:", output_list(self.classes, 1), "",
"Modules:",
output_list(self.imports, 1),
output_list(self.import_froms, 1), "",
"Variables:", output_list(self.variables, 1)]
return "\n".join(output)
def visit_FunctionDef(self, node):
self.functions.append("{}({})".format(
node.name,
unparse(node.args)))
def visit_ClassDef(self, node):
collector = ClassCollector(node)
collector.visit(node)
self.classes.append(collector)
def visit_Import(self, node):
for name in node.names:
if name.asname is None:
self.imports.append(name.name)
else:
self.imports.append("{} as {}".format(name.name, name.asname))
def visit_ImportFrom(self, node):
for name in node.names:
self.import_froms.append(unparse(node))
def visit_Assign(self, node):
self.variables.append(unparse(node))
class ClassCollector(ast.NodeVisitor):
def __init__(self, node):
self.title = "{}({})".format(node.name, ",".join(
unparse(base) for base in node.bases))
self.methods = []
def __str__(self):
return "{}\n{}".format(self.title, "".join(self.methods))
def visit_FunctionDef(self, node):
self.methods.append(indent(2) + "{}({})".format(
node.name,
unparse(node.args)) + "\n")
if __name__ == "__main__":
main()