Graphics/PackageDiagramBuilder.py

changeset 2037
7a3470ae170e
parent 2034
8de0fc1f7fef
child 2039
fa41ccd7f7bc
equal deleted inserted replaced
2036:3b443ed1b7d1 2037:7a3470ae170e
13 13
14 from PyQt4.QtGui import QProgressDialog, QApplication, QGraphicsTextItem 14 from PyQt4.QtGui import QProgressDialog, QApplication, QGraphicsTextItem
15 15
16 from .UMLDiagramBuilder import UMLDiagramBuilder 16 from .UMLDiagramBuilder import UMLDiagramBuilder
17 from .ClassItem import ClassItem, ClassModel 17 from .ClassItem import ClassItem, ClassModel
18 from .PackageItem import PackageItem, PackageModel
18 from .AssociationItem import AssociationItem, Generalisation 19 from .AssociationItem import AssociationItem, Generalisation
19 from . import GraphicsUtilities 20 from . import GraphicsUtilities
20 21
21 import Utilities.ModuleParser 22 import Utilities.ModuleParser
22 import Utilities 23 import Utilities
102 name = name[len(self.package) + 1:] 103 name = name[len(self.package) + 1:]
103 moduleDict[name] = mod 104 moduleDict[name] = mod
104 finally: 105 finally:
105 progress.setValue(tot) 106 progress.setValue(tot)
106 return moduleDict 107 return moduleDict
108
109 def __buildSubpackagesDict(self):
110 """
111 Private method to build a dictionary of sub-packages contained in this package.
112
113 @return dictionary of sub-packages contained in this package
114 """
115 supportedExt = \
116 ['*{0}'.format(ext) for ext in Preferences.getPython("PythonExtensions")] + \
117 ['*{0}'.format(ext) for ext in Preferences.getPython("Python3Extensions")] + \
118 ['*.rb']
119 extensions = Preferences.getPython("PythonExtensions") + \
120 Preferences.getPython("Python3Extensions") + ['.rb']
121
122 subpackagesDict = {}
123 subpackagesList = []
124
125 for subpackage in os.listdir(self.package):
126 subpackagePath = os.path.join(self.package, subpackage)
127 if os.path.isdir(subpackagePath) and \
128 len(glob.glob(os.path.join(subpackagePath, "__init__.*"))) != 0:
129 subpackagesList.append(subpackagePath)
130
131 tot = 0
132 for ext in supportedExt:
133 for subpackage in subpackagesList:
134 tot += len(glob.glob(Utilities.normjoinpath(subpackage, ext)))
135 try:
136 prog = 0
137 progress = QProgressDialog(self.trUtf8("Parsing modules..."),
138 None, 0, tot, self.parent())
139 progress.show()
140 QApplication.processEvents()
141 for subpackage in subpackagesList:
142 packageName = os.path.basename(subpackage)
143 subpackagesDict[packageName] = []
144 modules = []
145 for ext in supportedExt:
146 modules.extend(glob.glob(Utilities.normjoinpath(subpackage, ext)))
147 for module in modules:
148 progress.setValue(prog)
149 QApplication.processEvents()
150 prog += 1
151 try:
152 mod = Utilities.ModuleParser.readModule(
153 module, extensions=extensions)
154 except ImportError:
155 continue
156 else:
157 name = mod.name
158 if "." in name:
159 name = name.rsplit(".", 1)[1]
160 subpackagesDict[packageName].append(name)
161 subpackagesDict[packageName].sort()
162 # move __init__ to the front
163 if "__init__" in subpackagesDict[packageName]:
164 subpackagesDict[packageName].remove("__init__")
165 subpackagesDict[packageName].insert(0, "__init__")
166 finally:
167 progress.setValue(tot)
168 return subpackagesDict
107 169
108 def buildDiagram(self): 170 def buildDiagram(self):
109 """ 171 """
110 Public method to build the class shapes of the package diagram. 172 Public method to build the class shapes of the package diagram.
111 173
186 for child in children: 248 for child in children:
187 if (className, child) not in routes: 249 if (className, child) not in routes:
188 routes.append((className, child)) 250 routes.append((className, child))
189 251
190 del todo[0] 252 del todo[0]
191 253
254 # step 3: build the subpackages
255 subpackages = self.__buildSubpackagesDict()
256 for subpackage in sorted(subpackages.keys()):
257 self.__addPackage(subpackage, subpackages[subpackage], 0, 0)
258 nodes.append(subpackage)
259
192 self.__arrangeClasses(nodes, routes[:]) 260 self.__arrangeClasses(nodes, routes[:])
193 self.__createAssociations(routes) 261 self.__createAssociations(routes)
194 self.umlView.autoAdjustSceneSize(limit=True) 262 self.umlView.autoAdjustSceneSize(limit=True)
195 263
196 def __arrangeClasses(self, nodes, routes, whiteSpaceFactor=1.2): 264 def __arrangeClasses(self, nodes, routes, whiteSpaceFactor=1.2):
297 cl = ClassModel(_class) 365 cl = ClassModel(_class)
298 cw = ClassItem(cl, True, x, y, noAttrs=self.noAttrs, scene=self.scene) 366 cw = ClassItem(cl, True, x, y, noAttrs=self.noAttrs, scene=self.scene)
299 cw.setId(self.umlView.getItemId()) 367 cw.setId(self.umlView.getItemId())
300 self.allClasses[_class] = cw 368 self.allClasses[_class] = cw
301 369
370 def __addPackage(self, name, modules, x, y):
371 """
372 Private method to add a package to the diagram.
373
374 @param name package name to be shown (string)
375 @param modules list of module names contained in the package
376 (list of strings)
377 @param x x-coordinate (float)
378 @param y y-coordinate (float)
379 """
380 pm = PackageModel(name, modules)
381 pw = PackageItem(pm, x, y, scene=self.scene)
382 pw.setId(self.umlView.getItemId())
383 self.allClasses[name] = pw
384
302 def __createAssociations(self, routes): 385 def __createAssociations(self, routes):
303 """ 386 """
304 Private method to generate the associations between the class shapes. 387 Private method to generate the associations between the class shapes.
305 388
306 @param routes list of relationsships 389 @param routes list of relationsships

eric ide

mercurial