def OnItemDeselected(self, event): """ Item has been deselected """ line_number = self.getColumnText(self.currentItem, 2) python_path = self.getColumnText(self.currentItem, 4) if line_number != "": ### recuperation du model DEVS devs = Utilities.getInstance(Components.GetClass(python_path)) ### check error and change image if not isinstance(devs, tuple): self.SetItemImage(self.currentItem, self.idx2)
def OnDoubleClick(self, event): """ Double click on cell has been invocked """ line_number = self.getColumnText(self.currentItem, 2) python_path = self.getColumnText(self.currentItem, 4) if line_number != "": devscomp = Components.DEVSComponent() devscomp.setDEVSPythonPath(python_path) editor_frame = Components.DEVSComponent.OnEditor(devscomp, event) if editor_frame: editor_frame.text.GotoLine(int(line_number)) event.Skip()
def python_path_call_back(evt): fn = evt.GetEventObject().GetValue() cls = Components.GetClass(fn) if inspect.isclass(cls): if not (issubclass(cls, DomainBehavior.DomainBehavior) or issubclass(cls, DomainStructure.DomainStructure)): dlg = wx.MessageDialog( parent, _('The python file must contain a class that inherit of DomainBehavior or DomainStructure master class.\n Please choose a correct python file.' ), _('Error'), wx.ID_OK | wx.ICON_ERROR) dlg.ShowModal() else: dlg = wx.MessageDialog( parent, _('The python file not includes a class definition.\n Please choose a correct python file.' ), _('Error'), wx.ID_OK | wx.ICON_ERROR) dlg.ShowModal()
def GetBlock(self, filename, label, x, y): """ """ import Core.Components.Components as Components ### Block factory bf = Components.BlockFactory() ### Get block m = bf.GetBlock(filename, label) ### Move and append block if m: ### convert coordinate depending on the canvas x, y = self.canvas.GetXY(m, x, y) # move model from mouse position m.move(x, y) return m
def OnUpdate(self, evt): """ Update list has been invocked """ ### deep copy of data list D = copy.deepcopy(self.list.itemDataMap) ### update in error line self.list.itemDataMap for k, v in D.items(): line_number = v[2] if line_number != "": python_path = v[-1] devs = Utilities.getInstance(Components.GetClass(python_path)) ### check error and change image if not isinstance(devs, tuple): self.list.itemDataMap[k] = (v[0], "", "", v[3], v[4]) ### refresh items self.list.RefreshItems(-1, -1)
def getPythonModelFileName(fn): """ Get filename of zipped python file """ #global Cmtp assert (zipfile.is_zipfile(fn)) zf = zipfile.ZipFile(fn, 'r') ### TODO: finally impose : py_file_list = filter(lambda f: f.endswith('.py')) ### find if python file has same name of model file py_file_list = filter(lambda f: f.endswith('.py') and os.path.dirname(f) == '' and f not in ( 'plugins.py', 'steps.py', 'environment.py'), zf.namelist()) zf.close() #Cmtp+=1 #print Cmtp, fn ### if there is more than one python file in the zip file ### we find the correct behavioral file if len(py_file_list) > 1: model_name = os.path.splitext(os.path.basename(fn))[0] for python_file in py_file_list: ### if the name of python fiel in zip and the name of the model are similar. if os.path.splitext(python_file)[0] == model_name: return python_file ### esle test if the python file containing the class inherit of the DomainBehavior or DomainStructure else: import Core.Components.Components as Components cls = Components.GetClass(os.path.join(fn, python_file)) if inspect.isclass(cls): if issubclass(cls, DomainBehavior.DomainBehavior) or issubclass(cls, DomainStructure.DomainStructure): return python_file sys.stdout.write(_('Behavioral python file not found in %s file' % fn)) raise Exception else: ### zip file must contain python file return py_file_list[0]
def OnItemEdit(self, evt): item = self.GetSelection() path = self.GetItemPyData(item) ### virtual DEVS component just for edition devscomp = Components.DEVSComponent() ### path depends on the nature of droped component ### if pure python path if path.endswith('.py'): devscomp.setDEVSPythonPath(path) ### if devsimpy model elif zipfile.is_zipfile(path): #zf = ZipManager.Zip(path) devscomp.setDEVSPythonPath( os.path.join(path, ZipManager.getPythonModelFileName(path))) devscomp.model_path = path else: sys.stdout.write( _('The code of this type of model is not editable')) return ### call frame editor Components.DEVSComponent.OnEditor(devscomp, evt)
def GetModelList(self, dName): """ Get the list of files from dName directory. """ ### list of py file from __init__.py if LibraryTree.EXT_LIB_PYTHON_FLAG: ### lsit of py file from url if dName.startswith('http'): o = urlparse(dName) c = httplib.HTTPConnection(o.netloc) c.request('GET', o.path + '/__init__.py') r = c.getresponse() code = r.read() if r.status == 200: exec code tmp = filter( lambda s: s.replace('\n', '').replace('\t', ''). replace(',', '').replace('"', "").replace( '\'', "").strip(), __all__) ### test if the content of __init__.py file is python file (isfile equivalent) py_file_list = [ s for s in tmp if 'python' in urllib.urlopen(dName + '/' + s + '.py').info().type ] else: py_file_list = [] return py_file_list else: try: name_list = Utilities.getFileListFromInit( os.path.join(dName, '__init__.py')) py_file_list = [] for s in name_list: python_file = os.path.join(dName, s + '.py') ### test if tmp is only composed by python file (case of the user write into the __init__.py file directory name is possible ! then we delete the directory names) if os.path.isfile(python_file): cls = Components.GetClass(python_file) if cls is not None and not isinstance(cls, tuple): ### only model that herite from DomainBehavior is shown in lib if issubclass(cls, DomainBehavior.DomainBehavior): py_file_list.append(s) ### If cls is tuple, there is an error but we load the model to correct it. ### If its not DEVS model, the Dnd don't allows the instantiation and when the error is corrected, it don't appear before a update. else: py_file_list.append(s) except Exception: py_file_list = [] # if dName contains a python file, __init__.py is forced for f in os.listdir(dName): if f.endswith('.py'): #sys.stderr.write(_("%s not imported : %s \n"%(dName,info))) break else: py_file_list = [] # list of amd and cmd files devsimpy_file_list = [ f for f in os.listdir(dName) if os.path.isfile(os.path.join(dName, f)) and (f[:2] != '__') and ( f.endswith(LibraryTree.EXT_LIB_FILE)) ] return py_file_list + devsimpy_file_list