-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassBrowser.py
More file actions
222 lines (191 loc) · 7.9 KB
/
classBrowser.py
File metadata and controls
222 lines (191 loc) · 7.9 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'''
Altered version of python standard library pyclbr (python class browser).
Needed to deal with a) nested stuff better and b) know when functions ended.
Known issues: Comments, decorators and strings directly above a def will be
treated as part of the previous def.
'''
import collections
from io import StringIO
import tokenize
from token import NAME, DEDENT, OP
class Class:
'''Class to represent a Python class.'''
def __init__(self, module, name, super, file, lineno):
self.module = module
self.name = name
if super is None:
super = []
self.super = super
self.methods = {}
self.methodends = {}
self.file = file
self.lineno = lineno
self.linenoend = -1
def _addmethod(self, name, lineno):
self.methods[name] = lineno
self.methodends[name] = -1
def __repr__(self):
return '<Class s=%i e=%i>' % (self.lineno, self.linenoend)
class Function:
'''Class to represent a top-level Python function'''
def __init__(self, module, name, file, lineno):
self.module = module
self.name = name
self.file = file
self.lineno = lineno
self.linenoend = -1
def __repr__(self):
return '<Func s=%i e=%i>' % (self.lineno, self.linenoend)
def _readmodule(text):
_modules = {}
dict = collections.OrderedDict()
fullmodule = ''
fname = ''
f = StringIO(text)
stack = [] # stack of (class, indent) pairs
global prev_def
global prev_class
global prev_method
cur_class = None
prev_def = None
prev_class = None
prev_method = None
numblanks = 0
def markend(end):
end -= 1
global prev_def
global prev_class
global prev_method
if prev_def:
prev_def.linenoend = end - numblanks
prev_def = None
if prev_class:
prev_class.linenoend = end - numblanks
prev_class = None
if prev_method:
#prev_method.linenoend = end
prev_method[0].methodends[prev_method[1]] = end - numblanks
prev_method = None
g = tokenize.generate_tokens(f.readline)
try:
for tokentype, token, start, _end, _line in g:
#print(tokentype, token, start[0], _end[0] ) # , '>>' + _line + '<<')
if tokentype == DEDENT:
lineno, thisindent = start
# close nested classes and defs
while stack and stack[-1][1] >= thisindent:
if stack[-1][0]:
stack[-1][0].linenoend = lineno
del stack[-1]
elif token == 'def':
lineno, thisindent = start
# close previous nested classes and defs
while stack and stack[-1][1] >= thisindent:
if stack[-1][0]:
stack[-1][0].linenoend = lineno
del stack[-1]
tokentype, meth_name, start = next(g)[0:3]
if tokentype != NAME:
continue # Syntax error
if stack:
cur_class = stack[-1][0]
if isinstance(cur_class, Class):
# it's a method
cur_class._addmethod(meth_name, lineno)
markend(lineno)
prev_method = (cur_class, meth_name)
# else it's a nested def
else:
# it's a function
dict[meth_name] = Function(fullmodule, meth_name, fname, lineno)
markend(lineno)
if cur_class:
cur_class.linenoend = lineno - 1 - numblanks
cur_class = None
prev_def = dict[meth_name]
stack.append((None, thisindent)) # Marker for nested fns
elif token == 'class':
lineno, thisindent = start
# close previous nested classes and defs
while stack and stack[-1][1] >= thisindent:
if stack[-1][0]:
stack[-1][0].linenoend = lineno
del stack[-1]
tokentype, class_name, start = next(g)[0:3]
if tokentype != NAME:
continue # Syntax error
# parse what follows the class name
tokentype, token, start = next(g)[0:3]
inherit = None
if token == '(':
names = [] # List of superclasses
# there's a list of superclasses
level = 1
super = [] # Tokens making up current superclass
while True:
tokentype, token, start = next(g)[0:3]
if token in (')', ',') and level == 1:
n = "".join(super)
if n in dict:
# we know this super class
n = dict[n]
else:
c = n.split('.')
if len(c) > 1:
# super class is of the form
# module.class: look in module for
# class
m = c[-2]
c = c[-1]
if m in _modules:
d = _modules[m]
if c in d:
n = d[c]
names.append(n)
super = []
if token == '(':
level += 1
elif token == ')':
level -= 1
if level == 0:
break
elif token == ',' and level == 1:
pass
# only use NAME and OP (== dot) tokens for type name
elif tokentype in (NAME, OP) and level == 1:
super.append(token)
# expressions in the base list are not supported
inherit = names
markend(lineno)
if cur_class:
cur_class.linenoend = lineno - 1 - numblanks
cur_class = Class(fullmodule, class_name, inherit,
fname, lineno)
prev_class = cur_class
if not stack:
dict[class_name] = cur_class
stack.append((cur_class, thisindent))
if tokentype == tokenize.NL:
numblanks += 1
elif tokentype in (tokenize.INDENT, tokenize.DEDENT, tokenize.ENDMARKER):
# Don't let scope trigger clearing numblanks
pass
else:
numblanks = 0
except StopIteration:
pass
markend(_end[0] + 1) # Cleanup the final entry if there is one, not sure why I need to add one, but I do
f.close()
return dict
def prettyprint(results):
for name, data in results.items():
if isinstance(data, Class):
print( name )
for m in data.methods:
print( ' ', m, data.methods[m], data.methodends[m])
else:
print(name, data.lineno, data.linenoend)
if __name__ == '__main__':
with open(r'C:\Users\unknowable\AppData\Roaming\Sublime Text 3\Packages\User\classbrowser.py', 'r') as fid:
res = _readmodule(fid.read())
prettyprint(res)