-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
156d049
commit f81fe14
Showing
13 changed files
with
207 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,6 +37,3 @@ nosetests.xml | |
|
||
# IDEs | ||
/.idea/* | ||
|
||
# Project Specific | ||
/samples/code_visualization/reports/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,60 @@ | ||
# -*- coding: utf-8 -*- | ||
import re | ||
from networkx import Graph | ||
|
||
|
||
class FrameworkFeatureAnalyzer(object): | ||
def __init__(self, framework, project): | ||
""" | ||
:param inspector.models.base.Project project: the project to be analyzed | ||
""" | ||
self.framework_namespace = framework | ||
self.project = project | ||
|
||
def analyze_file_imports(self, source_file): | ||
self.framework_namespace = str(framework) | ||
self.framework_tree = Graph() | ||
self.framework_tree.add_node(self.framework_namespace) | ||
self.import_usages = [] | ||
|
||
def analyze_framework_imports(self, source_file): | ||
""" | ||
:param inspector.models.base.SourceFile source_file: the file | ||
""" | ||
for im in source_file.imports: | ||
if im.import_str.startswith(self.framework_namespace): | ||
print im | ||
self.import_usages.append((im, im.find_usages())) | ||
|
||
components = im.import_str.split('.') | ||
|
||
data = {'group': 1} | ||
if re.match(r'^[A-Z]+(_[A-Z]+)*$', components[-1]): | ||
data['group'] = 3 | ||
|
||
last = None | ||
for i in range(len(components)): | ||
cn = '.'.join(components[:i + 1]) | ||
self.framework_tree.add_node(cn, **data) | ||
if last: | ||
self.framework_tree.add_edge(last, cn, weight=1, group=1) | ||
last = cn | ||
if last: | ||
data['group'] = 3 | ||
self.framework_tree.add_node(last, **data) | ||
|
||
def analyze_source(self, source_file): | ||
""" | ||
:param inspector.models.base.SourceFile source_file: the file | ||
""" | ||
for cl in source_file.classes: | ||
self.framework_tree.add_node(cl.name, group=4) | ||
for fu in cl.methods: | ||
# print '[{0}-{1}]'.format(fu.starting_line, fu.ending_line), re.sub('\s*\n\s*', ' ', unicode(fu)) | ||
fn = fu.qualified_name | ||
self.framework_tree.add_node(fn, group=5) | ||
self.framework_tree.add_edge(cl.name, fn, weight=1, group=2) | ||
for im, usages in self.import_usages: | ||
w = 0 | ||
for ln in usages: | ||
if fu.starting_line <= ln <= fu.ending_line: | ||
w += 1 | ||
if w: | ||
self.framework_tree.add_edge(im.import_str, fn, weight=w, group=3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# -*- coding: utf-8 -*- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# -*- coding: utf-8 -*- | ||
import os | ||
|
||
from inspector.utils.strings import render_template | ||
|
||
|
||
def generate_graph_html(graph, filename): | ||
""" | ||
:type graph: networkx.Graph | ||
:param str filename: path to save the generated html file | ||
""" | ||
|
||
params = { | ||
'nodes': [], | ||
'links': [], | ||
} | ||
ids = {} | ||
for i, (node, data) in enumerate(graph.nodes(data=True)): | ||
val = unicode(node) | ||
ids[val] = i | ||
params['nodes'].append('{name:"%s",group:%d}' % (node, data.get('group', 1))) | ||
for u, v, data in graph.edges(data=True): | ||
params['links'].append('{source:%d,target:%d,value:%d,group:%d}' % (ids[unicode(u)], | ||
ids[unicode(v)], | ||
data.get('weight', 1), | ||
data.get('group', 1))) | ||
params['nodes'] = ','.join(params['nodes']) | ||
params['links'] = ','.join(params['links']) | ||
|
||
# generating output | ||
current_dir = os.path.abspath(os.path.dirname(__file__)) | ||
with open(os.path.join(current_dir, 'templates', 'force_directed_graph.html'), 'r') as f: | ||
html = f.read() | ||
html = render_template(html, params) | ||
with open(filename, 'w') as f: | ||
f.write(html) |
73 changes: 73 additions & 0 deletions
73
inspector/utils/visualization/templates/force_directed_graph.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<style> | ||
.node { | ||
stroke: #fff; | ||
stroke-width: 1.5px; | ||
} | ||
|
||
.link { | ||
stroke: #999; | ||
stroke-opacity: .6; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<script src="http://d3js.org/d3.v3.min.js"></script> | ||
<script> | ||
var graph = { | ||
nodes: [{{ nodes }}], | ||
links: [{{ links }}] | ||
}; | ||
</script> | ||
<script> | ||
var width = 960, | ||
height = 500; | ||
|
||
var color = d3.scale.category20(); | ||
|
||
var force = d3.layout.force() | ||
.charge(-120) | ||
.linkDistance(30) | ||
.size([width, height]); | ||
|
||
var svg = d3.select("body").append("svg") | ||
.attr("width", width) | ||
.attr("height", height); | ||
|
||
force | ||
.nodes(graph.nodes) | ||
.links(graph.links) | ||
.start(); | ||
|
||
var link = svg.selectAll(".link") | ||
.data(graph.links) | ||
.enter().append("line") | ||
.attr("class", "link") | ||
.style("stroke-width", function(d) { return Math.sqrt(d.value); }); | ||
|
||
var node = svg.selectAll(".node") | ||
.data(graph.nodes) | ||
.enter().append("circle") | ||
.attr("class", "node") | ||
.attr("r", 5) | ||
.style("fill", function(d) { return color(d.group); }) | ||
.call(force.drag); | ||
|
||
node.append("title") | ||
.text(function(d) { return d.name; }); | ||
|
||
force.on("tick", function() { | ||
link.attr("x1", function(d) { return d.source.x; }) | ||
.attr("y1", function(d) { return d.source.y; }) | ||
.attr("x2", function(d) { return d.target.x; }) | ||
.attr("y2", function(d) { return d.target.y; }); | ||
|
||
node.attr("cx", function(d) { return d.x; }) | ||
.attr("cy", function(d) { return d.y; }); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
networkx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters