Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Side by Side Diff: third_party/pylint/pylint/pyreverse/diadefslib.py

Issue 1920403002: [content/test/gpu] Run pylint check of gpu tests in unittest instead of PRESUBMIT (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update path to LICENSE.txt of logilab/README.chromium Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright (c) 2000-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 """handle diagram generation options for class diagram or default diagrams
17 """
18
19 from logilab.common.compat import builtins
20
21 import astroid
22 from astroid.utils import LocalsVisitor
23
24 from pylint.pyreverse.diagrams import PackageDiagram, ClassDiagram
25
26 BUILTINS_NAME = builtins.__name__
27
28 # diagram generators ##########################################################
29
30 class DiaDefGenerator(object):
31 """handle diagram generation options"""
32
33 def __init__(self, linker, handler):
34 """common Diagram Handler initialization"""
35 self.config = handler.config
36 self._set_default_options()
37 self.linker = linker
38 self.classdiagram = None # defined by subclasses
39
40 def get_title(self, node):
41 """get title for objects"""
42 title = node.name
43 if self.module_names:
44 title = '%s.%s' % (node.root().name, title)
45 return title
46
47 def _set_option(self, option):
48 """activate some options if not explicitly deactivated"""
49 # if we have a class diagram, we want more information by default;
50 # so if the option is None, we return True
51 if option is None:
52 if self.config.classes:
53 return True
54 else:
55 return False
56 return option
57
58 def _set_default_options(self):
59 """set different default options with _default dictionary"""
60 self.module_names = self._set_option(self.config.module_names)
61 all_ancestors = self._set_option(self.config.all_ancestors)
62 all_associated = self._set_option(self.config.all_associated)
63 anc_level, ass_level = (0, 0)
64 if all_ancestors:
65 anc_level = -1
66 if all_associated:
67 ass_level = -1
68 if self.config.show_ancestors is not None:
69 anc_level = self.config.show_ancestors
70 if self.config.show_associated is not None:
71 ass_level = self.config.show_associated
72 self.anc_level, self.ass_level = anc_level, ass_level
73
74 def _get_levels(self):
75 """help function for search levels"""
76 return self.anc_level, self.ass_level
77
78 def show_node(self, node):
79 """true if builtins and not show_builtins"""
80 if self.config.show_builtin:
81 return True
82 return node.root().name != BUILTINS_NAME
83
84 def add_class(self, node):
85 """visit one class and add it to diagram"""
86 self.linker.visit(node)
87 self.classdiagram.add_object(self.get_title(node), node)
88
89 def get_ancestors(self, node, level):
90 """return ancestor nodes of a class node"""
91 if level == 0:
92 return
93 for ancestor in node.ancestors(recurs=False):
94 if not self.show_node(ancestor):
95 continue
96 yield ancestor
97
98 def get_associated(self, klass_node, level):
99 """return associated nodes of a class node"""
100 if level == 0:
101 return
102 for ass_nodes in list(klass_node.instance_attrs_type.values()) + \
103 list(klass_node.locals_type.values()):
104 for ass_node in ass_nodes:
105 if isinstance(ass_node, astroid.Instance):
106 ass_node = ass_node._proxied
107 if not (isinstance(ass_node, astroid.Class)
108 and self.show_node(ass_node)):
109 continue
110 yield ass_node
111
112 def extract_classes(self, klass_node, anc_level, ass_level):
113 """extract recursively classes related to klass_node"""
114 if self.classdiagram.has_node(klass_node) or not self.show_node(klass_no de):
115 return
116 self.add_class(klass_node)
117
118 for ancestor in self.get_ancestors(klass_node, anc_level):
119 self.extract_classes(ancestor, anc_level-1, ass_level)
120
121 for ass_node in self.get_associated(klass_node, ass_level):
122 self.extract_classes(ass_node, anc_level, ass_level-1)
123
124
125 class DefaultDiadefGenerator(LocalsVisitor, DiaDefGenerator):
126 """generate minimum diagram definition for the project :
127
128 * a package diagram including project's modules
129 * a class diagram including project's classes
130 """
131
132 def __init__(self, linker, handler):
133 DiaDefGenerator.__init__(self, linker, handler)
134 LocalsVisitor.__init__(self)
135
136 def visit_project(self, node):
137 """visit an astroid.Project node
138
139 create a diagram definition for packages
140 """
141 mode = self.config.mode
142 if len(node.modules) > 1:
143 self.pkgdiagram = PackageDiagram('packages %s' % node.name, mode)
144 else:
145 self.pkgdiagram = None
146 self.classdiagram = ClassDiagram('classes %s' % node.name, mode)
147
148 def leave_project(self, node): # pylint: disable=unused-argument
149 """leave the astroid.Project node
150
151 return the generated diagram definition
152 """
153 if self.pkgdiagram:
154 return self.pkgdiagram, self.classdiagram
155 return self.classdiagram,
156
157 def visit_module(self, node):
158 """visit an astroid.Module node
159
160 add this class to the package diagram definition
161 """
162 if self.pkgdiagram:
163 self.linker.visit(node)
164 self.pkgdiagram.add_object(node.name, node)
165
166 def visit_class(self, node):
167 """visit an astroid.Class node
168
169 add this class to the class diagram definition
170 """
171 anc_level, ass_level = self._get_levels()
172 self.extract_classes(node, anc_level, ass_level)
173
174 def visit_from(self, node):
175 """visit astroid.From and catch modules for package diagram
176 """
177 if self.pkgdiagram:
178 self.pkgdiagram.add_from_depend(node, node.modname)
179
180
181 class ClassDiadefGenerator(DiaDefGenerator):
182 """generate a class diagram definition including all classes related to a
183 given class
184 """
185
186 def __init__(self, linker, handler):
187 DiaDefGenerator.__init__(self, linker, handler)
188
189 def class_diagram(self, project, klass):
190 """return a class diagram definition for the given klass and its
191 related klasses
192 """
193
194 self.classdiagram = ClassDiagram(klass, self.config.mode)
195 if len(project.modules) > 1:
196 module, klass = klass.rsplit('.', 1)
197 module = project.get_module(module)
198 else:
199 module = project.modules[0]
200 klass = klass.split('.')[-1]
201 klass = next(module.ilookup(klass))
202
203 anc_level, ass_level = self._get_levels()
204 self.extract_classes(klass, anc_level, ass_level)
205 return self.classdiagram
206
207 # diagram handler #############################################################
208
209 class DiadefsHandler(object):
210 """handle diagram definitions :
211
212 get it from user (i.e. xml files) or generate them
213 """
214
215 def __init__(self, config):
216 self.config = config
217
218 def get_diadefs(self, project, linker):
219 """get the diagrams configuration data
220 :param linker: astroid.inspector.Linker(IdGeneratorMixIn, LocalsVisitor)
221 :param project: astroid.manager.Project
222 """
223
224 # read and interpret diagram definitions (Diadefs)
225 diagrams = []
226 generator = ClassDiadefGenerator(linker, self)
227 for klass in self.config.classes:
228 diagrams.append(generator.class_diagram(project, klass))
229 if not diagrams:
230 diagrams = DefaultDiadefGenerator(linker, self).visit(project)
231 for diagram in diagrams:
232 diagram.extract_relationships()
233 return diagrams
OLDNEW
« no previous file with comments | « third_party/pylint/pylint/pyreverse/__init__.py ('k') | third_party/pylint/pylint/pyreverse/diagrams.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698