|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2007 - 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog showing an imports diagram of the application. |
|
8 """ |
|
9 |
|
10 import os |
|
11 import glob |
|
12 |
|
13 from PyQt4.QtCore import * |
|
14 from PyQt4.QtGui import * |
|
15 |
|
16 from UMLDialog import UMLDialog |
|
17 from PackageItem import PackageItem, PackageModel |
|
18 from AssociationItem import AssociationItem, Imports |
|
19 import GraphicsUtilities |
|
20 |
|
21 import Utilities.ModuleParser |
|
22 import Utilities |
|
23 |
|
24 class ApplicationDiagram(UMLDialog): |
|
25 """ |
|
26 Class implementing a dialog showing an imports diagram of the application. |
|
27 """ |
|
28 def __init__(self, project, parent = None, name = None, noModules = False): |
|
29 """ |
|
30 Constructor |
|
31 |
|
32 @param project reference to the project object |
|
33 @param parent parent widget of the view (QWidget) |
|
34 @param name name of the view widget (string) |
|
35 @keyparam noModules flag indicating, that no module names should be |
|
36 shown (boolean) |
|
37 """ |
|
38 self.project = project |
|
39 self.noModules = noModules |
|
40 |
|
41 UMLDialog.__init__(self, self.project.ppath, parent) |
|
42 |
|
43 if not name: |
|
44 self.setObjectName("ApplicationDiagram") |
|
45 else: |
|
46 self.setObjectName(name) |
|
47 |
|
48 self.connect(self.umlView, SIGNAL("relayout()"), self.relayout) |
|
49 |
|
50 def __buildModulesDict(self): |
|
51 """ |
|
52 Private method to build a dictionary of modules contained in the application. |
|
53 |
|
54 @return dictionary of modules contained in the application. |
|
55 """ |
|
56 moduleDict = {} |
|
57 mods = self.project.pdata["SOURCES"] |
|
58 modules = [] |
|
59 for module in mods: |
|
60 modules.append(Utilities.normabsjoinpath(self.project.ppath, module)) |
|
61 tot = len(modules) |
|
62 try: |
|
63 prog = 0 |
|
64 progress = QProgressDialog(self.trUtf8("Parsing modules..."), |
|
65 "", 0, tot, self) |
|
66 progress.show() |
|
67 QApplication.processEvents() |
|
68 for module in modules: |
|
69 progress.setValue(prog) |
|
70 QApplication.processEvents() |
|
71 prog += 1 |
|
72 if module.endswith("__init__.py"): |
|
73 continue |
|
74 try: |
|
75 mod = Utilities.ModuleParser.readModule(module) |
|
76 except ImportError: |
|
77 continue |
|
78 else: |
|
79 name = mod.name |
|
80 moduleDict[name] = mod |
|
81 finally: |
|
82 progress.setValue(tot) |
|
83 return moduleDict |
|
84 |
|
85 def __buildPackages(self): |
|
86 """ |
|
87 Private method to build the packages shapes of the diagram. |
|
88 """ |
|
89 project = os.path.splitdrive(self.project.ppath)[1].replace(os.sep, '.')[1:] |
|
90 packages = {} |
|
91 shapes = {} |
|
92 p = 10 |
|
93 y = 10 |
|
94 maxHeight = 0 |
|
95 sceneRect = self.umlView.sceneRect() |
|
96 |
|
97 modules = self.__buildModulesDict() |
|
98 sortedkeys = sorted(modules.keys()) |
|
99 |
|
100 # step 1: build a dictionary of packages |
|
101 for module in sortedkeys: |
|
102 l = module.split('.') |
|
103 package = '.'.join(l[:-1]) |
|
104 if package in packages: |
|
105 packages[package][0].append(l[-1]) |
|
106 else: |
|
107 packages[package] = ([l[-1]], []) |
|
108 |
|
109 # step 2: assign modules to dictionaries and update import relationship |
|
110 for module in sortedkeys: |
|
111 l = module.split('.') |
|
112 package = '.'.join(l[:-1]) |
|
113 impLst = [] |
|
114 for i in modules[module].imports: |
|
115 if i in modules: |
|
116 impLst.append(i) |
|
117 else: |
|
118 if i.find('.') == -1: |
|
119 n = "%s.%s" % (modules[module].package, i) |
|
120 if n in modules: |
|
121 impLst.append(n) |
|
122 else: |
|
123 n = "%s.%s" % (project, i) |
|
124 if n in modules: |
|
125 impLst.append(n) |
|
126 elif n in packages: |
|
127 n = "%s.<<Dummy>>" % n |
|
128 impLst.append(n) |
|
129 else: |
|
130 n = "%s.%s" % (project, i) |
|
131 if n in modules: |
|
132 impLst.append(n) |
|
133 for i in modules[module].from_imports.keys(): |
|
134 if i.startswith('.'): |
|
135 dots = len(i) - len(i.lstrip('.')) |
|
136 if dots == 1: |
|
137 i = i[1:] |
|
138 elif dots > 1: |
|
139 packagePath = os.path.dirname(modules[module].file) |
|
140 hasInit = True |
|
141 ppath = packagePath |
|
142 while hasInit: |
|
143 ppath = os.path.dirname(ppath) |
|
144 hasInit = \ |
|
145 len(glob.glob(os.path.join(ppath, '__init__.*'))) > 0 |
|
146 shortPackage = \ |
|
147 packagePath.replace(ppath, '').replace(os.sep, '.')[1:] |
|
148 packageList = shortPackage.split('.')[1:] |
|
149 packageListLen = len(packageList) |
|
150 i = '.'.join(packageList[:packageListLen - dots + 1] + [i[dots:]]) |
|
151 |
|
152 if i in modules: |
|
153 impLst.append(i) |
|
154 else: |
|
155 if i.find('.') == -1: |
|
156 n = "%s.%s" % (modules[module].package, i) |
|
157 if n in modules: |
|
158 impLst.append(n) |
|
159 else: |
|
160 n = "%s.%s" % (project, i) |
|
161 if n in modules: |
|
162 impLst.append(n) |
|
163 elif n in packages: |
|
164 n = "%s.<<Dummy>>" % n |
|
165 impLst.append(n) |
|
166 else: |
|
167 n = "%s.%s" % (project, i) |
|
168 if n in modules: |
|
169 impLst.append(n) |
|
170 for imp in impLst: |
|
171 impPackage = '.'.join(imp.split('.')[:-1]) |
|
172 if not impPackage in packages[package][1] and \ |
|
173 not impPackage == package: |
|
174 packages[package][1].append(impPackage) |
|
175 |
|
176 sortedkeys = sorted(packages.keys()) |
|
177 for package in sortedkeys: |
|
178 if package: |
|
179 relPackage = package.replace(project, '') |
|
180 if relPackage and relPackage[0] == '.': |
|
181 relPackage = relPackage[1:] |
|
182 else: |
|
183 relPackage = self.trUtf8("<<Application>>") |
|
184 else: |
|
185 relPackage = self.trUtf8("<<Others>>") |
|
186 shape = self.__addPackage(relPackage, packages[package][0], 0.0, 0.0) |
|
187 shapeRect = shape.sceneBoundingRect() |
|
188 shapes[package] = (shape, packages[package][1]) |
|
189 pn = p + shapeRect.width() + 10 |
|
190 maxHeight = max(maxHeight, shapeRect.height()) |
|
191 if pn > sceneRect.width(): |
|
192 p = 10 |
|
193 y += maxHeight + 10 |
|
194 maxHeight = shapeRect.height() |
|
195 shape.setPos(p, y) |
|
196 p += shapeRect.width() + 10 |
|
197 else: |
|
198 shape.setPos(p, y) |
|
199 p = pn |
|
200 |
|
201 rect = self.umlView._getDiagramRect(10) |
|
202 sceneRect = self.umlView.sceneRect() |
|
203 if rect.width() > sceneRect.width(): |
|
204 sceneRect.setWidth(rect.width()) |
|
205 if rect.height() > sceneRect.height(): |
|
206 sceneRect.setHeight(rect.height()) |
|
207 self.umlView.setSceneSize(sceneRect.width(), sceneRect.height()) |
|
208 |
|
209 self.__createAssociations(shapes) |
|
210 |
|
211 def __addPackage(self, name, modules, x, y): |
|
212 """ |
|
213 Private method to add a package to the diagram. |
|
214 |
|
215 @param name package name to be shown (string) |
|
216 @param modules list of module names contained in the package |
|
217 (list of strings) |
|
218 @param x x-coordinate (float) |
|
219 @param y y-coordinate (float) |
|
220 """ |
|
221 modules.sort() |
|
222 pm = PackageModel(name, modules) |
|
223 pw = PackageItem(pm, x, y, noModules = self.noModules, scene = self.scene) |
|
224 return pw |
|
225 |
|
226 def __createAssociations(self, shapes): |
|
227 """ |
|
228 Private method to generate the associations between the package shapes. |
|
229 |
|
230 @param shapes list of shapes |
|
231 """ |
|
232 for package in shapes: |
|
233 for rel in shapes[package][1]: |
|
234 assoc = AssociationItem(\ |
|
235 shapes[package][0], shapes[rel][0], |
|
236 Imports) |
|
237 self.scene.addItem(assoc) |
|
238 |
|
239 def show(self): |
|
240 """ |
|
241 Overriden method to show the dialog. |
|
242 """ |
|
243 self.__buildPackages() |
|
244 UMLDialog.show(self) |
|
245 |
|
246 def relayout(self): |
|
247 """ |
|
248 Method to relayout the diagram. |
|
249 """ |
|
250 self.__buildPackages() |