def InsertNewDomain(self, dName, parent, L=[]): """ Recurrent function that insert new Domain on library panel. """ ### first only for the root if dName not in list(self.ItemDico.keys()): label = os.path.basename(dName) if not dName.startswith( 'http') else [a for a in dName.split('/') if a != ''][-1] id = self.InsertItemBefore(parent, 0, label) self.SetItemImage(id, self.fldridx, wx.TreeItemIcon_Normal) self.SetItemImage(id, self.fldropenidx, wx.TreeItemIcon_Expanded) self.SetItemBold(id) self.ItemDico.update({dName: id}) self.SetPyData(id, dName) ### end if L == []: return else: item = L.pop(0) isunicode = isinstance(item, str) isstr = isinstance(item, str) isdict = isinstance(item, dict) ### element to insert in the list D = [] ### if child is build from DEVSimPy if isstr: ### parent is retrieved from dict parent = self.ItemDico[dName] assert parent != None ### parent path parentPath = self.GetPyData(parent) come_from_net = parentPath.startswith('http') ### comma replace item = item.strip() ### suppression de l'extention su .cmd (model atomic lu à partir de __init__ donc pas d'extention) if item.endswith('.cmd'): ### gestion de l'importation de module (.py) associé au .cmd si le fichier .py n'a jamais été decompresssé (pour edition par exemple) if not come_from_net: path = os.path.join(parentPath, item) zf = Zip(path) module = zf.GetModule() image_file = zf.GetImage() else: path = "".join([parentPath, '/', item, '.py']) module = load_module_from_net(path) ### check error error = isinstance(module, Exception) ### change icon depending on the error and the presence of image in amd if error: img = self.not_importedidx elif image_file is not None: img = self.il.Add(image_file.ConvertToBitmap()) else: img = self.coupledidx ### insert into the tree id = self.InsertItemBefore(parent, 0, os.path.splitext(item)[0], img, img) self.SetPyData(id, path) self.MetricDico.update( {id: { 'mcc': 0.0, 'parent': parent }}) s = sum([ d['mcc'] for id, d in self.MetricDico.items() if d['parent'] == parent ]) self.MetricDico.update( {parent: { 'mcc': s, 'parent': None }}) elif item.endswith('.amd'): ### gestion de l'importation de module (.py) associé au .amd si le fichier .py n'a jamais été decompresssé (pour edition par exemple) if not come_from_net: path = os.path.join(parentPath, item) zf = Zip(path) module = zf.GetModule() image_file = zf.GetImage() else: path = "".join([parentPath, '/', item, '.py']) module = load_module_from_net(path) ### check error error = isinstance(module, Exception) ### change icon depending on the error and the presence of image in amd if error: img = self.not_importedidx elif image_file is not None: img = self.il.Add(image_file.ConvertToBitmap()) else: img = self.atomicidx mcc = GetMacCabeMetric(path) ### insert in the tree id = self.InsertItemBefore(parent, 0, os.path.splitext(item)[0], img, img) self.SetPyData(id, path) self.MetricDico.update( {id: { 'mcc': mcc, 'parent': parent }}) s = sum([ d['mcc'] for id, d in self.MetricDico.items() if d['parent'] == parent ]) self.MetricDico.update( {parent: { 'mcc': s, 'parent': None }}) else: path = os.path.join(parentPath, "".join( [item, '.py'])) if not come_from_net else "".join( [parentPath, '/', item, '.py']) ### try for .pyc ispyc = False if not os.path.exists(path): path = os.path.join(parentPath, "".join( [item, '.pyc'])) if not come_from_net else "".join( [parentPath, '/', item, '.pyc']) ispyc = True devs = Container.CheckClass(path) #mcc = float(subprocess.check_output('python {} {}'.format('Complexity.py', path), shell = True)) mcc = GetMacCabeMetric(path) error = isinstance(devs, tuple) img = self.not_importedidx if error else self.pythoncfileidx if ispyc else self.pythonfileidx ### insert in the tree id = self.InsertItemBefore(parent, 0, item, img, img) self.SetPyData(id, path) self.MetricDico.update( {id: { 'mcc': mcc, 'parent': parent }}) s = sum([ d['mcc'] for id, d in self.MetricDico.items() if d['parent'] == parent ]) self.MetricDico.update( {parent: { 'mcc': s, 'parent': None }}) ### error info back propagation if error: while (parent): self.SetItemImage(parent, self.not_importedidx, wx.TreeItemIcon_Normal) ### next parent item parent = self.GetItemParent(parent) ### insertion des donnees dans l'item et gestion du ItemDico self.ItemDico.update({os.path.join( parentPath, item, ): id}) ### si le fils est un sous repertoire contenant au moins un fichier (all dans __init__.py different de []) elif isdict and list(item.values()) != [[]]: ### name to insert in the tree dName = os.path.basename(list(item.keys())[0]) ### new parent parent = self.ItemDico[os.path.dirname( list(item.keys())[0] )] if not dName.startswith('http') else self.ItemDico[list( item.keys())[0].replace('/' + dName, '')] assert (parent != None) ### insert of the fName above the parent id = self.InsertItemBefore(parent, 0, dName) self.SetItemBold(id) self.SetItemImage(id, self.fldridx, wx.TreeItemIcon_Normal) self.SetItemImage(id, self.fldropenidx, wx.TreeItemIcon_Expanded) ### stockage du parent avec pour cle le chemin complet avec extention (pour l'import du moule dans le Dnd) self.ItemDico.update({list(item.keys())[0]: id}) self.SetPyData(id, list(item.keys())[0]) self.MetricDico.update({id: {'mcc': 0.0, 'parent': parent}}) self.MetricDico.update({parent: {'mcc': 0.0, 'parent': None}}) ### for the childrens of the sub-domain for elem in list(item.values())[0]: # si elem simple (modèle couple ou atomic) if isinstance(elem, str): ### replace the spaces elem = elem.strip() #replace(' ','') ### parent provisoir p = self.ItemDico[list(item.keys())[0]] assert (p != None) come_from_net = list(item.keys())[0].startswith('http') ### si model atomic if elem.endswith('.cmd'): ### gestion de l'importation de module (.py) associé au .amd si le fichier .py n'a jamais été decompresssé (pour edition par exemple) if not come_from_net: path = os.path.join(list(item.keys())[0], elem) zf = Zip(path) module = zf.GetModule() image_file = zf.GetImage() else: path = "".join( [list(item.keys())[0], '/', elem, '.py']) module = load_module_from_net(path) ### check error error = isinstance(module, Exception) ### change icon depending on the error and the presence of image in amd if error: img = self.not_importedidx elif image_file is not None: img = self.il.Add(image_file.ConvertToBitmap()) else: img = self.coupledidx ### insertion dans le tree id = self.InsertItemBefore( p, 0, os.path.splitext(elem)[0], img, img) self.SetPyData(id, path) self.MetricDico.update( {id: { 'mcc': 0.0, 'parent': parent }}) s = sum([ d['mcc'] for id, d in self.MetricDico.items() if d['parent'] == parent ]) self.MetricDico.update( {parent: { 'mcc': s, 'parent': None }}) elif elem.endswith('.amd'): ### gestion de l'importation de module (.py) associé au .amd si le fichier .py n'a jamais été decompresssé (pour edition par exemple) if not come_from_net: path = os.path.join(list(item.keys())[0], elem) zf = Zip(path) module = zf.GetModule() image_file = zf.GetImage() else: path = "".join( [list(item.keys())[0], '/', elem, '.py']) module = load_module_from_net(path) ### check error error = isinstance(module, Exception) ### change icon depending on the error and the presence of image in amd if error: img = self.not_importedidx elif image_file is not None: img = self.il.Add(image_file.ConvertToBitmap()) else: img = self.atomicidx mcc = GetMacCabeMetric(path) ### insert in the tree id = self.InsertItemBefore( p, 0, os.path.splitext(elem)[0], img, img) self.SetPyData(id, path) self.MetricDico.update( {id: { 'mcc': mcc, 'parent': parent }}) s = sum([ d['mcc'] for id, d in self.MetricDico.items() if d['parent'] == parent ]) self.MetricDico.update( {parent: { 'mcc': s, 'parent': None }}) else: path = os.path.join( list(item.keys())[0], "".join([elem, '.py'])) if not list(item.keys( ))[0].startswith('http') else list( item.keys())[0] + '/' + elem + '.py' ### try for .pyc file ispyc = False if not os.path.exists(path): path = os.path.join( list(item.keys())[0], "".join([ elem, '.pyc' ])) if not list(item.keys( ))[0].startswith('http') else list( item.keys())[0] + '/' + elem + '.pyc' ispyc = True devs = Container.CheckClass(path) mcc = GetMacCabeMetric(path) error = isinstance(devs, tuple) img = self.not_importedidx if error else self.pythoncfileidx if ispyc else self.pythonfileidx ### insert in the tree id = self.InsertItemBefore(p, 0, elem, img, img) self.SetPyData(id, path) self.MetricDico.update( {id: { 'mcc': mcc, 'parent': parent }}) s = sum([ d['mcc'] for id, d in self.MetricDico.items() if d['parent'] == parent ]) self.MetricDico.update( {parent: { 'mcc': s, 'parent': None }}) ### error info back propagation if error: ### insert error to the doc field while (p): self.SetItemImage(p, self.not_importedidx, wx.TreeItemIcon_Normal) ### next parent item p = self.GetItemParent(p) self.ItemDico.update( {os.path.join(list(item.keys())[0], elem): id}) else: ### in order to go up the information in the list D.append(elem) ### update with whole name dName = list(item.keys())[0] ### for spash screen try: ### format the string depending the nature of the item if isdict: item = " ".join([ os.path.basename(list(item.keys())[0]), 'from', os.path.basename(os.path.dirname(list(item.keys())[0])) ]) else: item = " ".join([item, 'from', os.path.basename(dName)]) pub.sendMessage('object.added', message='Loading %s domain...' % item) except: pass ### managment of the recursion if D != []: self.InsertNewDomain(dName, parent, D) try: self.SortChildren(parent) except: pass return self.InsertNewDomain(dName, parent, L)
def InsertNewDomain(self, dName, parent, L=[]): """ Recurrent function that insert new Domain on library panel. """ ### first only for the root if dName not in self.ItemDico.keys(): label = os.path.basename( dName) if not dName.startswith('http') else filter( lambda a: a != '', dName.split('/'))[-1] id = self.InsertItemBefore(parent, 0, label) self.SetItemImage(id, self.fldridx, wx.TreeItemIcon_Normal) self.SetItemImage(id, self.fldropenidx, wx.TreeItemIcon_Expanded) self.SetItemBold(id) self.ItemDico.update({dName: id}) self.SetPyData(id, dName) ### end if L == []: return else: item = L.pop(0) isunicode = isinstance(item, unicode) isstr = isinstance(item, str) isdict = isinstance(item, dict) assert not isunicode, _("Warning unicode item !") ### element to insert in the list D = [] ### if child is build from DEVSimPy if isstr: ### parent is retrieved from dict parent = self.ItemDico[dName] assert parent != None ### parent path parentPath = self.GetPyData(parent) come_from_net = parentPath.startswith('http') ### comma replace item = item.strip() ### suppression de l'extention su .cmd (model atomic lu à partir de __init__ donc pas d'extention) if item.endswith('.cmd'): ### gestion de l'importation de module (.py) associé au .cmd si le fichier .py n'a jamais été decompresssé (pour edition par exemple) if not come_from_net: path = os.path.join(parentPath, item) zf = Zip(path) module = zf.GetModule() image_file = zf.GetImage() else: path = "".join([parentPath, '/', item, '.py']) module = load_module_from_net(path) ### check error error = isinstance(module, Exception) ### change icon depending on the error and the presence of image in amd if error: img = self.not_importedidx elif image_file is not None: img = self.il.Add(image_file.ConvertToBitmap()) else: img = self.coupledidx ### insert into the tree id = self.InsertItemBefore(parent, 0, os.path.splitext(item)[0], img, img) self.SetPyData(id, path) elif item.endswith('.amd'): ### gestion de l'importation de module (.py) associé au .amd si le fichier .py n'a jamais été decompresssé (pour edition par exemple) if not come_from_net: path = os.path.join(parentPath, item) zf = Zip(path) module = zf.GetModule() image_file = zf.GetImage() else: path = "".join([parentPath, '/', item, '.py']) module = load_module_from_net(path) ### check error error = isinstance(module, Exception) ### change icon depending on the error and the presence of image in amd if error: img = self.not_importedidx elif image_file is not None: img = self.il.Add(image_file.ConvertToBitmap()) else: img = self.atomicidx ### insertion dans le tree id = self.InsertItemBefore(parent, 0, os.path.splitext(item)[0], img, img) self.SetPyData(id, path) else: path = os.path.join(parentPath, "".join( [item, '.py'])) if not come_from_net else "".join( [parentPath, '/', item, '.py']) info = Container.CheckClass(path) error = isinstance(info, tuple) img = self.not_importedidx if error else self.pythonfileidx ### insertion dans le tree id = self.InsertItemBefore(parent, 0, item, img, img) self.SetPyData(id, path) ### error info back propagation if error: while (parent): self.SetItemImage(parent, self.not_importedidx, wx.TreeItemIcon_Normal) ### next parent item parent = self.GetItemParent(parent) ### insertion des donnees dans l'item et gestion du ItemDico self.ItemDico.update({os.path.join( parentPath, item, ): id}) ### si le fils est un sous repertoire contenant au moins un fichier (all dans __init__.py different de []) elif isdict and item.values() != [[]]: ### nom a inserer dans l'arbe dName = os.path.basename(item.keys()[0]) ### nouveau parent parent = self.ItemDico[os.path.dirname( item.keys()[0])] if not dName.startswith( 'http') else self.ItemDico[item.keys()[0].replace( '/' + dName, '')] assert (parent != None) ### insertion de fName sous parent id = self.InsertItemBefore(parent, 0, dName) self.SetItemImage(id, self.fldridx, wx.TreeItemIcon_Normal) self.SetItemImage(id, self.fldropenidx, wx.TreeItemIcon_Expanded) ### stockage du parent avec pour cle le chemin complet avec extention (pour l'import du moule dans le Dnd) self.ItemDico.update({item.keys()[0]: id}) self.SetPyData(id, item.keys()[0]) ### pour les fils du sous domain for elem in item.values()[0]: # si elem simple (modèle couple ou atomic) if isstr: ### remplacement des espaces elem = elem.strip() #replace(' ','') ### parent provisoir p = self.ItemDico[item.keys()[0]] assert (p != None) come_from_net = item.keys()[0].startswith('http') ### si model atomic if elem.endswith('.cmd'): ### gestion de l'importation de module (.py) associé au .amd si le fichier .py n'a jamais été decompresssé (pour edition par exemple) if not come_from_net: path = os.path.join(item.keys()[0], elem) zf = Zip(path) module = zf.GetModule() image_file = zf.GetImage() else: path = "".join( [item.keys()[0], '/', elem, '.py']) module = load_module_from_net(path) ### check error error = isinstance(module, Exception) ### change icon depending on the error and the presence of image in amd if error: img = self.not_importedidx elif image_file is not None: img = self.il.Add(image_file.ConvertToBitmap()) else: img = self.coupledidx ### insertion dans le tree id = self.InsertItemBefore( p, 0, os.path.splitext(elem)[0], img, img) self.SetPyData(id, path) elif elem.endswith('.amd'): ### gestion de l'importation de module (.py) associé au .amd si le fichier .py n'a jamais été decompresssé (pour edition par exemple) if not come_from_net: path = os.path.join(item.keys()[0], elem) zf = Zip(path) module = zf.GetModule() image_file = zf.GetImage() else: path = "".join( [item.keys()[0], '/', elem, '.py']) module = load_module_from_net(path) ### check error error = isinstance(module, Exception) ### change icon depending on the error and the presence of image in amd if error: img = self.not_importedidx elif image_file is not None: img = self.il.Add(image_file.ConvertToBitmap()) else: img = self.atomicidx ### insertion dans le tree id = self.InsertItemBefore( p, 0, os.path.splitext(elem)[0], img, img) self.SetPyData(id, path) else: path = os.path.join(item.keys()[0], "".join( [elem, '.py'])) if not item.keys( )[0].startswith('http') else "".join( [item.keys()[0], '/', elem, '.py']) info = Container.CheckClass(path) error = isinstance(info, tuple) img = self.not_importedidx if error else self.pythonfileidx ### insertion dans le tree id = self.InsertItemBefore(p, 0, elem, img, img) self.SetPyData(id, path) ### error info back propagation if error: ### insert error to the doc field while (p): self.SetItemImage(p, self.not_importedidx, wx.TreeItemIcon_Normal) ### next parent item p = self.GetItemParent(p) self.ItemDico.update({ os.path.join(item.keys()[0], os.path.splitext(elem)[0]): id }) else: ### pour faire remonter l'info dans la liste D.append(elem) ### mise a jour avec le nom complet dName = item.keys()[0] ### for spash screen try: ### format the string depending the nature of the item if isdict: item = " ".join([ os.path.basename(item.keys()[0]), 'from', os.path.basename(os.path.dirname(item.keys()[0])) ]) else: item = " ".join([item, 'from', os.path.basename(dName)]) pub.sendMessage('object.added', message='Loading %s domain...' % item) except: pass ### gestion de la recursion if D != []: return self.InsertNewDomain(dName, parent, L + D) else: return self.InsertNewDomain(dName, parent, L)