gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / pylint / pyreverse / diagrams.py @ 745
History | View | Annotate | Download (8.97 KB)
1 |
# Copyright (c) 2004-2013 LOGILAB S.A. (Paris, FRANCE).
|
---|---|
2 |
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
|
3 |
#
|
4 |
# This program is free software; you can redistribute it and/or modify it under
|
5 |
# the terms of the GNU General Public License as published by the Free Software
|
6 |
# Foundation; either version 2 of the License, or (at your option) any later
|
7 |
# version.
|
8 |
#
|
9 |
# This program is distributed in the hope that it will be useful, but WITHOUT
|
10 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
11 |
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
12 |
#
|
13 |
# You should have received a copy of the GNU General Public License along with
|
14 |
# this program; if not, write to the Free Software Foundation, Inc.,
|
15 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
16 |
"""diagram objects
|
17 |
"""
|
18 |
|
19 |
import astroid |
20 |
from pylint.pyreverse.utils import is_interface, FilterMixIn |
21 |
from pylint.checkers.utils import decorated_with_property |
22 |
|
23 |
|
24 |
class Figure(object): |
25 |
"""base class for counter handling"""
|
26 |
|
27 |
|
28 |
class Relationship(Figure): |
29 |
"""a relation ship from an object in the diagram to another
|
30 |
"""
|
31 |
def __init__(self, from_object, to_object, relation_type, name=None): |
32 |
Figure.__init__(self)
|
33 |
self.from_object = from_object
|
34 |
self.to_object = to_object
|
35 |
self.type = relation_type
|
36 |
self.name = name
|
37 |
|
38 |
|
39 |
class DiagramEntity(Figure): |
40 |
"""a diagram object, i.e. a label associated to an astroid node
|
41 |
"""
|
42 |
def __init__(self, title='No name', node=None): |
43 |
Figure.__init__(self)
|
44 |
self.title = title
|
45 |
self.node = node
|
46 |
|
47 |
|
48 |
class ClassDiagram(Figure, FilterMixIn): |
49 |
"""main class diagram handling
|
50 |
"""
|
51 |
TYPE = 'class'
|
52 |
def __init__(self, title, mode): |
53 |
FilterMixIn.__init__(self, mode)
|
54 |
Figure.__init__(self)
|
55 |
self.title = title
|
56 |
self.objects = []
|
57 |
self.relationships = {}
|
58 |
self._nodes = {}
|
59 |
self.depends = []
|
60 |
|
61 |
def get_relationships(self, role): |
62 |
# sorted to get predictable (hence testable) results
|
63 |
return sorted(self.relationships.get(role, ()), |
64 |
key=lambda x: (x.from_object.fig_id, x.to_object.fig_id))
|
65 |
|
66 |
def add_relationship(self, from_object, to_object, |
67 |
relation_type, name=None):
|
68 |
"""create a relation ship
|
69 |
"""
|
70 |
rel = Relationship(from_object, to_object, relation_type, name) |
71 |
self.relationships.setdefault(relation_type, []).append(rel)
|
72 |
|
73 |
def get_relationship(self, from_object, relation_type): |
74 |
"""return a relation ship or None
|
75 |
"""
|
76 |
for rel in self.relationships.get(relation_type, ()): |
77 |
if rel.from_object is from_object: |
78 |
return rel
|
79 |
raise KeyError(relation_type) |
80 |
|
81 |
def get_attrs(self, node): |
82 |
"""return visible attributes, possibly with class name"""
|
83 |
attrs = [] |
84 |
properties = [ |
85 |
(n, m) for n, m in node.items() |
86 |
if isinstance(m, astroid.FunctionDef) |
87 |
and decorated_with_property(m)
|
88 |
] |
89 |
for node_name, ass_nodes in list(node.instance_attrs_type.items()) + \ |
90 |
list(node.locals_type.items()) + properties:
|
91 |
if not self.show_attr(node_name): |
92 |
continue
|
93 |
names = self.class_names(ass_nodes)
|
94 |
if names:
|
95 |
node_name = "%s : %s" % (node_name, ", ".join(names)) |
96 |
attrs.append(node_name) |
97 |
return sorted(attrs) |
98 |
|
99 |
def get_methods(self, node): |
100 |
"""return visible methods"""
|
101 |
methods = [ |
102 |
m for m in node.values() |
103 |
if isinstance(m, astroid.FunctionDef) |
104 |
and not decorated_with_property(m) |
105 |
and self.show_attr(m.name) |
106 |
] |
107 |
return sorted(methods, key=lambda n: n.name) |
108 |
|
109 |
def add_object(self, title, node): |
110 |
"""create a diagram object
|
111 |
"""
|
112 |
assert node not in self._nodes |
113 |
ent = DiagramEntity(title, node) |
114 |
self._nodes[node] = ent
|
115 |
self.objects.append(ent)
|
116 |
|
117 |
def class_names(self, nodes): |
118 |
"""return class names if needed in diagram"""
|
119 |
names = [] |
120 |
for ass_node in nodes: |
121 |
if isinstance(ass_node, astroid.Instance): |
122 |
ass_node = ass_node._proxied |
123 |
if isinstance(ass_node, astroid.ClassDef) \ |
124 |
and hasattr(ass_node, "name") and not self.has_node(ass_node): |
125 |
if ass_node.name not in names: |
126 |
ass_name = ass_node.name |
127 |
names.append(ass_name) |
128 |
return names
|
129 |
|
130 |
def nodes(self): |
131 |
"""return the list of underlying nodes
|
132 |
"""
|
133 |
return self._nodes.keys() |
134 |
|
135 |
def has_node(self, node): |
136 |
"""return true if the given node is included in the diagram
|
137 |
"""
|
138 |
return node in self._nodes |
139 |
|
140 |
def object_from_node(self, node): |
141 |
"""return the diagram object mapped to node
|
142 |
"""
|
143 |
return self._nodes[node] |
144 |
|
145 |
def classes(self): |
146 |
"""return all class nodes in the diagram"""
|
147 |
return [o for o in self.objects if isinstance(o.node, astroid.ClassDef)] |
148 |
|
149 |
def classe(self, name): |
150 |
"""return a class by its name, raise KeyError if not found
|
151 |
"""
|
152 |
for klass in self.classes(): |
153 |
if klass.node.name == name:
|
154 |
return klass
|
155 |
raise KeyError(name) |
156 |
|
157 |
def extract_relationships(self): |
158 |
"""extract relation ships between nodes in the diagram
|
159 |
"""
|
160 |
for obj in self.classes(): |
161 |
node = obj.node |
162 |
obj.attrs = self.get_attrs(node)
|
163 |
obj.methods = self.get_methods(node)
|
164 |
# shape
|
165 |
if is_interface(node):
|
166 |
obj.shape = 'interface'
|
167 |
else:
|
168 |
obj.shape = 'class'
|
169 |
# inheritance link
|
170 |
for par_node in node.ancestors(recurs=False): |
171 |
try:
|
172 |
par_obj = self.object_from_node(par_node)
|
173 |
self.add_relationship(obj, par_obj, 'specialization') |
174 |
except KeyError: |
175 |
continue
|
176 |
# implements link
|
177 |
for impl_node in node.implements: |
178 |
try:
|
179 |
impl_obj = self.object_from_node(impl_node)
|
180 |
self.add_relationship(obj, impl_obj, 'implements') |
181 |
except KeyError: |
182 |
continue
|
183 |
# associations link
|
184 |
for name, values in list(node.instance_attrs_type.items()) + \ |
185 |
list(node.locals_type.items()):
|
186 |
for value in values: |
187 |
if value is astroid.YES: |
188 |
continue
|
189 |
if isinstance(value, astroid.Instance): |
190 |
value = value._proxied |
191 |
try:
|
192 |
ass_obj = self.object_from_node(value)
|
193 |
self.add_relationship(ass_obj, obj, 'association', name) |
194 |
except KeyError: |
195 |
continue
|
196 |
|
197 |
|
198 |
class PackageDiagram(ClassDiagram): |
199 |
"""package diagram handling
|
200 |
"""
|
201 |
TYPE = 'package'
|
202 |
|
203 |
def modules(self): |
204 |
"""return all module nodes in the diagram"""
|
205 |
return [o for o in self.objects if isinstance(o.node, astroid.Module)] |
206 |
|
207 |
def module(self, name): |
208 |
"""return a module by its name, raise KeyError if not found
|
209 |
"""
|
210 |
for mod in self.modules(): |
211 |
if mod.node.name == name:
|
212 |
return mod
|
213 |
raise KeyError(name) |
214 |
|
215 |
def get_module(self, name, node): |
216 |
"""return a module by its name, looking also for relative imports;
|
217 |
raise KeyError if not found
|
218 |
"""
|
219 |
for mod in self.modules(): |
220 |
mod_name = mod.node.name |
221 |
if mod_name == name:
|
222 |
return mod
|
223 |
#search for fullname of relative import modules
|
224 |
package = node.root().name |
225 |
if mod_name == "%s.%s" % (package, name): |
226 |
return mod
|
227 |
if mod_name == "%s.%s" % (package.rsplit('.', 1)[0], name): |
228 |
return mod
|
229 |
raise KeyError(name) |
230 |
|
231 |
def add_from_depend(self, node, from_module): |
232 |
"""add dependencies created by from-imports
|
233 |
"""
|
234 |
mod_name = node.root().name |
235 |
obj = self.module(mod_name)
|
236 |
if from_module not in obj.node.depends: |
237 |
obj.node.depends.append(from_module) |
238 |
|
239 |
def extract_relationships(self): |
240 |
"""extract relation ships between nodes in the diagram
|
241 |
"""
|
242 |
ClassDiagram.extract_relationships(self)
|
243 |
for obj in self.classes(): |
244 |
# ownership
|
245 |
try:
|
246 |
mod = self.object_from_node(obj.node.root())
|
247 |
self.add_relationship(obj, mod, 'ownership') |
248 |
except KeyError: |
249 |
continue
|
250 |
for obj in self.modules(): |
251 |
obj.shape = 'package'
|
252 |
# dependencies
|
253 |
for dep_name in obj.node.depends: |
254 |
try:
|
255 |
dep = self.get_module(dep_name, obj.node)
|
256 |
except KeyError: |
257 |
continue
|
258 |
self.add_relationship(obj, dep, 'depends') |