-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_view.py
266 lines (232 loc) · 9.15 KB
/
source_view.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#/bin/python
import importlib
import contextlib
import os
import networkx as nx
from torch_snippets import *
from inspect import getmembers, isfunction, isclass, getsource
from torch_snippets.markup import AttrDict, pretty_json
from bokeh.io import show, output_file
from bokeh.models import Plot, Range1d, MultiLine, Circle
from bokeh.models import HoverTool, TapTool, BoxSelectTool, PanTool, WheelZoomTool, ResetTool
from bokeh.models.graphs import from_networkx
from bokeh.models.graphs import NodesAndLinkedEdges, EdgesAndLinkedNodes
from bokeh.palettes import Spectral4
from bokeh.models import ColumnDataSource, LabelSet
def merge(l):
l = [_l for _l in l if _l != {}]
d = AttrDict(dict((k,v) for d in l for k,v in d.items()))
return AttrDict({k:v for k,v in d.items() if v != {}})
def is_plural(item):
return len(item) != 1
class PyObj:
def __init__(self, name, function):
self.name = name
self.function = function
def __repr__(self): return self.name
@property
def source(self):
try:
return getsource(self.function)
except:
return getsource(self.function.fget)
class PyFunc(PyObj):
@property
def children(self):
calls = [f[:-1] for f in re.findall(r'[a-zA-Z_\.]+\(', self.source)]
return [c for c in calls if c!=self.name]
class PyClass(PyObj):
@property
def methods(self):
return [PyFunc(f'{self.name}.{f}', getattr(self.function, f)) for f in dir(self.function) if not f.startswith('__')]
class PyFile:
def __init__(self, file):
self.file = file
self._module = file.replace('/', '.').replace('.py','')
try:
self.mod = importlib.import_module(self.file.replace('/','.').replace('.py',''))
except Exception as e:
logger.warning(f'Error parsing functions @ {self.file}: {e}')
self.mod = None
@property
def classes(self):
if self.mod is None:
return []
else:
members = getmembers(self.mod, isclass)
return [PyClass(name, cls)
for (name, cls) in members
if cls.__module__ == self._module]
@property
def functions(self):
if self.mod is not None:
members = getmembers(self.mod, isfunction)
return [PyFunc(name, function)
for (name, function) in members
if function.__module__ == self._module]
else:
return []
def __len__(self):
return len(self.classes) + len(self.functions)
def __getitem__(self, ix):
return self.functions[ix]
def __repr__(self):
return f'{self.file} ({len(self.functions)} {"functions" if is_plural(self.functions) else "function"} and {len(self.classes)} {"classes" if is_plural(self.classes) else "class"})'
class Source:
def __init__(self, root, extension='py'):
self.root = root
self.catalogue = AttrDict({})
self.tree = self.filter(extension)
@property
def children(self):
files = Glob(self.root, silent=True)
self.folders = [Source(f) for f in files if os.path.isdir(f)]
self.files = [f for f in files if not os.path.isdir(f)]
return self.folders + self.files
def __repr__(self):
return str(self.root)
def get(self, path):
x = self.tree
for i in path.split('/'):
if i!='':
x = getattr(x, i)
return dir(x)
def resolve(self, x=None, extension=None):
x = self if x is None else x
if os.path.isfile(str(x)):
if extension is None or extn(x) == extension:
_file = str(x)
file = PyFile(_file)
for function in file.functions:
_name = function.name
function.name = f'{function.name} @ {_file}'
self.catalogue[_name] = function
for cls in file.classes:
_name = cls.name
cls.name = f'{cls.name} @ {_file}'
self.catalogue[_name] = cls
return AttrDict({stem(x): file})
else:
return AttrDict({})
if isinstance(x, Source):
return merge([self.resolve(f, extension) for f in x.children])
def filter(self, extension):
return self.resolve(extension=extension)
def add_node_if_not_existing(network, node_name, **kwargs):
if network.has_node(node_name):
pass
else:
network.add_node(node_name, **kwargs)
def pretty(node, NETWORK, children=False):
if isinstance(node, AttrDict):
return [pretty(_node) for k,_node in node.items()]
if len(node) == 0: return
add_node_if_not_existing(NETWORK, node.file, size=15, color='red', name=node.file, typ='file')
for f in node.functions:
add_node_if_not_existing(NETWORK, f.name, size=8, color='green', name=f.name, typ='function')
NETWORK.add_edge(f.name, node.file, weight=1/5)
if children:
[(add_node_if_not_existing(NETWORK, child, size=5, color='yellow', name=child, typ='child'), NETWORK.add_edge(f.name, child, weight=1/2.5))
for child in f.children]
for cls in node.classes:
add_node_if_not_existing(NETWORK, cls.name, size=12, color='purple', name=cls.name, typ='class')
NETWORK.add_edge(cls.name, node.file, weight=1/4)
for method in cls.methods:
try:
add_node_if_not_existing(NETWORK, method.name, size=5, color='blue', name=method.name, typ='method')
NETWORK.add_edge(cls.name, method.name, weight=1/2.5)
if children:
[(add_node_if_not_existing(NETWORK, child, size=5, color='yellow', name=child, typ='child'), NETWORK.add_edge(method.name, child, weight=1/1))
for child in method.children]
except:
...
def summarize(folder, NETWORK):
x = Source(folder)
[pretty(getattr(x.tree, f), NETWORK) for f in dir(x.tree)]
[pretty(getattr(x.tree, f), NETWORK, children=True) for f in dir(x.tree)]
@contextlib.contextmanager
def working_directory(path):
prev_cwd = os.getcwd()
os.chdir(parent(path))
sys.path.append(str(P(path).resolve()))
yield
os.chdir(prev_cwd)
def get_ranges(pos):
xs, ys = zip(*list(pos.values()))
return np.min(xs), np.max(xs), np.min(ys), np.max(ys)
def main(folder):
if folder.endswith('/'): folder = folder[:-1]
with working_directory(folder):
NETWORK = nx.Graph()
summarize(stem(folder), NETWORK)
logger.info(f'Built the graph for {folder}. {len(NETWORK.nodes)} nodes and {len(NETWORK.edges)} edges found')
# find('layout', dir(nx))
pos_ = nx.nx_agraph.graphviz_layout(NETWORK, 'neato')
# pos_ = nx.kamada_kawai_layout(NETWORK)
# pos_ = nx.rescale_layout(NETWORK)
# pos_ = nx.spring_layout(NETWORK)
logger.info(f'Identified the layout')
x, X, y, Y = get_ranges(pos_)
plot = Plot(
plot_width=1024,
plot_height=1024,
x_range=Range1d(x,X),
y_range=Range1d(y,Y)
)
plot.add_tools(
HoverTool(tooltips=None),
TapTool(),
BoxSelectTool(),
PanTool(),
WheelZoomTool(),
ResetTool()
)
plot.toolbar.active_scroll = plot.select_one(WheelZoomTool)
graph_renderer = from_networkx(
NETWORK,
pos_,
scale=1,
center=(0,0),
)
graph_renderer.node_renderer.data_source.data['size'] = [i*2 for i in list(nx.get_node_attributes(NETWORK, 'size').values())]
graph_renderer.node_renderer.data_source.data['color'] = [i for i in list(nx.get_node_attributes(NETWORK, 'color').values())]
graph_renderer.node_renderer.glyph = Circle(
size='size',
fill_color='color'
)
graph_renderer.node_renderer.selection_glyph = Circle(
size='size',
fill_color='color'
)
graph_renderer.node_renderer.hover_glyph = Circle(
size='size',
fill_color='color'
)
graph_renderer.edge_renderer.glyph = MultiLine(
line_color="#CCCCCC",
line_alpha=0.8,
line_width=1
)
graph_renderer.edge_renderer.selection_glyph = MultiLine(
line_color=(0,0,0),
line_width=3
)
graph_renderer.edge_renderer.hover_glyph = MultiLine(
line_color=(0,0,0),
line_width=3
)
graph_renderer.selection_policy = NodesAndLinkedEdges()
graph_renderer.inspection_policy = EdgesAndLinkedNodes()
plot.renderers.append(graph_renderer)
x, y = zip(*list(pos_.values()))
node_labels = nx.get_node_attributes(NETWORK, 'name')
types = nx.get_node_attributes(NETWORK, 'typ')
source = pd.DataFrame({'x': x, 'y': y, 'type': [types[i] for i in types.keys()],
'name': [node_labels[i] for i in node_labels.keys()]})
# source = source[source['type'].map(lambda x: x in ['file','class'])].reset_index(drop=True)
source.to_dict(orient='list')
source = ColumnDataSource(source)
labels = LabelSet(x='x', y='y', text='name', source=source, background_fill_alpha=0, text_font_size='10px')
plot.renderers.append(labels)
output_file(f"{folder}.html")
show(plot)