etisserant@14: """ etisserant@14: Base definitions for beremiz plugins etisserant@14: """ etisserant@14: etisserant@178: import os,sys,traceback laurent@411: import time lbessard@17: import plugins etisserant@14: import types etisserant@14: import shutil etisserant@14: from xml.dom import minidom etisserant@22: import wx etisserant@20: etisserant@20: #Quick hack to be able to find Beremiz IEC tools. Should be config params. etisserant@20: base_folder = os.path.split(sys.path[0])[0] greg@126: etisserant@14: from xmlclass import GenerateClassesFromXSDstring etisserant@110: from wxPopen import ProcessLogger etisserant@14: laurent@401: from PLCControler import PLCControler, LOCATION_PLUGIN, LOCATION_MODULE, LOCATION_GROUP, LOCATION_VAR_INPUT, LOCATION_VAR_OUTPUT, LOCATION_VAR_MEMORY greg@274: etisserant@14: _BaseParamsClass = GenerateClassesFromXSDstring(""" etisserant@14: etisserant@14: etisserant@14: lbessard@86: lbessard@17: lbessard@86: etisserant@14: etisserant@14: lbessard@86: """)["BaseParams"] etisserant@14: etisserant@14: NameTypeSeparator = '@' etisserant@14: lbessard@65: class MiniTextControler: lbessard@65: lbessard@65: def __init__(self, filepath): lbessard@65: self.FilePath = filepath lbessard@65: lbessard@74: def SetEditedElementText(self, tagname, text): lbessard@65: file = open(self.FilePath, "w") lbessard@65: file.write(text) lbessard@65: file.close() lbessard@65: greg@273: def GetEditedElementText(self, tagname, debug = False): lbessard@65: if os.path.isfile(self.FilePath): lbessard@65: file = open(self.FilePath, "r") lbessard@65: text = file.read() lbessard@65: file.close() lbessard@65: return text lbessard@65: return "" lbessard@65: greg@273: def GetEditedElementInterfaceVars(self, tagname, debug = False): lbessard@74: return [] lbessard@74: greg@273: def GetEditedElementType(self, tagname, debug = False): lbessard@74: return "program" lbessard@74: greg@273: def GetBlockTypes(self, tagname = "", debug = False): lbessard@74: return [] lbessard@74: laurent@630: def GetDataTypes(self, tagname = "", basetypes = True, only_locatables = False, debug = False): laurent@630: return [] laurent@630: greg@273: def GetEnumeratedDataValues(self, debug = False): lbessard@74: return [] lbessard@74: lbessard@65: def StartBuffering(self): lbessard@65: pass lbessard@65: lbessard@65: def EndBuffering(self): lbessard@65: pass lbessard@65: lbessard@65: def BufferProject(self): lbessard@65: pass lbessard@65: etisserant@203: # helper func to get path to images etisserant@203: def opjimg(imgname): laurent@655: return os.path.join(base_folder, "beremiz", "images",imgname) greg@427: greg@427: # helper func to check path write permission greg@427: def CheckPathPerm(path): greg@427: if path is None or not os.path.isdir(path): greg@427: return False greg@427: for root, dirs, files in os.walk(path): greg@427: for name in files: greg@427: if os.access(root, os.W_OK) is not True or os.access(os.path.join(root, name), os.W_OK) is not True: greg@427: return False greg@427: return True greg@427: etisserant@14: class PlugTemplate: etisserant@14: """ etisserant@14: This class is the one that define plugins. etisserant@14: """ etisserant@14: etisserant@14: XSD = None etisserant@14: PlugChildsTypes = [] etisserant@14: PlugMaxCount = None etisserant@14: PluginMethods = [] greg@274: LibraryControler = None laurent@656: EditorType = None etisserant@14: etisserant@14: def _AddParamsMembers(self): lbessard@19: self.PlugParams = None etisserant@29: if self.XSD: laurent@411: self.Classes = GenerateClassesFromXSDstring(self.XSD) laurent@411: Classes = [(name, XSDclass) for name, XSDclass in self.Classes.items() if XSDclass.IsBaseClass] etisserant@29: if len(Classes) == 1: etisserant@29: name, XSDclass = Classes[0] etisserant@29: obj = XSDclass() etisserant@29: self.PlugParams = (name, obj) etisserant@29: setattr(self, name, obj) lbessard@17: lbessard@17: def __init__(self): etisserant@14: # Create BaseParam etisserant@14: self.BaseParams = _BaseParamsClass() lbessard@17: self.MandatoryParams = ("BaseParams", self.BaseParams) etisserant@14: self._AddParamsMembers() etisserant@14: self.PluggedChilds = {} laurent@656: self._View = None etisserant@106: # copy PluginMethods so that it can be later customized etisserant@106: self.PluginMethods = [dic.copy() for dic in self.PluginMethods] lbessard@325: self.LoadSTLibrary() lbessard@325: lbessard@17: def PluginBaseXmlFilePath(self, PlugName=None): lbessard@17: return os.path.join(self.PlugPath(PlugName), "baseplugin.xml") etisserant@14: etisserant@14: def PluginXmlFilePath(self, PlugName=None): etisserant@14: return os.path.join(self.PlugPath(PlugName), "plugin.xml") etisserant@14: lbessard@325: def PluginLibraryFilePath(self): laurent@363: return os.path.join(self.PluginPath(), "pous.xml") laurent@363: laurent@363: def PluginPath(self): laurent@363: return os.path.join(self.PlugParent.PluginPath(), self.PlugType) greg@274: etisserant@14: def PlugPath(self,PlugName=None): etisserant@14: if not PlugName: etisserant@14: PlugName = self.BaseParams.getName() etisserant@203: return os.path.join(self.PlugParent.PlugPath(), etisserant@203: PlugName + NameTypeSeparator + self.PlugType) etisserant@14: laurent@656: def PlugFullName(self): laurent@656: parent = self.PlugParent.PlugFullName() laurent@656: if parent != "": laurent@656: return parent + "." + self.BaseParams.getName() laurent@656: return self.BaseParams.getName() laurent@656: laurent@652: def GetIconPath(self, name): laurent@652: return opjimg(name) laurent@652: etisserant@14: def PlugTestModified(self): etisserant@118: return self.ChangesToSave etisserant@118: etisserant@118: def ProjectTestModified(self): etisserant@118: """ etisserant@118: recursively check modified status etisserant@118: """ etisserant@118: if self.PlugTestModified(): etisserant@118: return True etisserant@118: etisserant@118: for PlugChild in self.IterChilds(): etisserant@118: if PlugChild.ProjectTestModified(): etisserant@118: return True etisserant@118: etisserant@14: return False etisserant@14: etisserant@14: def OnPlugSave(self): etisserant@20: #Default, do nothing and return success etisserant@14: return True etisserant@14: lbessard@19: def GetParamsAttributes(self, path = None): lbessard@19: if path: lbessard@19: parts = path.split(".", 1) lbessard@19: if self.MandatoryParams and parts[0] == self.MandatoryParams[0]: lbessard@19: return self.MandatoryParams[1].getElementInfos(parts[0], parts[1]) lbessard@19: elif self.PlugParams and parts[0] == self.PlugParams[0]: lbessard@19: return self.PlugParams[1].getElementInfos(parts[0], parts[1]) lbessard@17: else: lbessard@19: params = [] lbessard@82: if wx.VERSION < (2, 8, 0) and self.MandatoryParams: lbessard@19: params.append(self.MandatoryParams[1].getElementInfos(self.MandatoryParams[0])) lbessard@19: if self.PlugParams: lbessard@19: params.append(self.PlugParams[1].getElementInfos(self.PlugParams[0])) lbessard@19: return params lbessard@19: etisserant@203: def SetParamsAttribute(self, path, value): etisserant@118: self.ChangesToSave = True etisserant@29: # Filter IEC_Channel and Name, that have specific behavior etisserant@29: if path == "BaseParams.IEC_Channel": laurent@443: old_leading = ".".join(map(str, self.GetCurrentLocation())) laurent@443: new_value = self.FindNewIEC_Channel(value) laurent@443: new_leading = ".".join(map(str, self.PlugParent.GetCurrentLocation() + (new_value,))) laurent@443: self.GetPlugRoot().UpdateProjectVariableLocation(old_leading, new_leading) laurent@443: return new_value, True etisserant@29: elif path == "BaseParams.Name": etisserant@203: res = self.FindNewName(value) etisserant@29: self.PlugRequestSave() etisserant@118: return res, True etisserant@29: lbessard@19: parts = path.split(".", 1) lbessard@19: if self.MandatoryParams and parts[0] == self.MandatoryParams[0]: lbessard@19: self.MandatoryParams[1].setElementValue(parts[1], value) lbessard@19: elif self.PlugParams and parts[0] == self.PlugParams[0]: lbessard@19: self.PlugParams[1].setElementValue(parts[1], value) etisserant@29: return value, False lbessard@17: laurent@395: def PlugMakeDir(self): laurent@395: os.mkdir(self.PlugPath()) laurent@395: etisserant@14: def PlugRequestSave(self): greg@427: if self.GetPlugRoot().CheckProjectPathPerm(False): greg@427: # If plugin do not have corresponding directory greg@427: plugpath = self.PlugPath() greg@427: if not os.path.isdir(plugpath): greg@427: # Create it greg@427: os.mkdir(plugpath) greg@427: greg@427: # generate XML for base XML parameters controller of the plugin greg@427: if self.MandatoryParams: greg@427: BaseXMLFile = open(self.PluginBaseXmlFilePath(),'w') greg@427: BaseXMLFile.write("\n") laurent@430: BaseXMLFile.write(self.MandatoryParams[1].generateXMLText(self.MandatoryParams[0], 0).encode("utf-8")) greg@427: BaseXMLFile.close() greg@427: greg@427: # generate XML for XML parameters controller of the plugin greg@427: if self.PlugParams: greg@427: XMLFile = open(self.PluginXmlFilePath(),'w') greg@427: XMLFile.write("\n") laurent@430: XMLFile.write(self.PlugParams[1].generateXMLText(self.PlugParams[0], 0).encode("utf-8")) greg@427: XMLFile.close() greg@427: greg@427: # Call the plugin specific OnPlugSave method greg@427: result = self.OnPlugSave() greg@427: if not result: greg@427: return _("Error while saving \"%s\"\n")%self.PlugPath() greg@427: greg@427: # mark plugin as saved greg@427: self.ChangesToSave = False greg@427: # go through all childs and do the same greg@427: for PlugChild in self.IterChilds(): greg@427: result = PlugChild.PlugRequestSave() greg@427: if result: greg@427: return result lbessard@17: return None etisserant@14: etisserant@14: def PlugImport(self, src_PlugPath): etisserant@14: shutil.copytree(src_PlugPath, self.PlugPath) etisserant@14: return True etisserant@14: etisserant@203: def PlugGenerate_C(self, buildpath, locations): etisserant@14: """ etisserant@14: Generate C code etisserant@14: @param locations: List of complete variables locations \ etisserant@22: [{"IEC_TYPE" : the IEC type (i.e. "INT", "STRING", ...) etisserant@22: "NAME" : name of the variable (generally "__IW0_1_2" style) etisserant@22: "DIR" : direction "Q","I" or "M" etisserant@22: "SIZE" : size "X", "B", "W", "D", "L" etisserant@22: "LOC" : tuple of interger for IEC location (0,1,2,...) etisserant@22: }, ...] etisserant@18: @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND etisserant@18: """ laurent@421: self.GetPlugRoot().logger.write_warning(".".join(map(lambda x:str(x), self.GetCurrentLocation())) + " -> Nothing to do\n") etisserant@51: return [],"",False etisserant@14: etisserant@203: def _Generate_C(self, buildpath, locations): etisserant@203: # Generate plugins [(Cfiles, CFLAGS)], LDFLAGS, DoCalls, extra_files etisserant@203: # extra_files = [(fname,fobject), ...] etisserant@203: gen_result = self.PlugGenerate_C(buildpath, locations) etisserant@203: PlugCFilesAndCFLAGS, PlugLDFLAGS, DoCalls = gen_result[:3] etisserant@203: extra_files = gen_result[3:] laurent@361: # if some files have been generated put them in the list with their location etisserant@47: if PlugCFilesAndCFLAGS: etisserant@51: LocationCFilesAndCFLAGS = [(self.GetCurrentLocation(), PlugCFilesAndCFLAGS, DoCalls)] etisserant@47: else: etisserant@47: LocationCFilesAndCFLAGS = [] etisserant@47: etisserant@115: # plugin asks for some LDFLAGS etisserant@47: if PlugLDFLAGS: etisserant@47: # LDFLAGS can be either string etisserant@47: if type(PlugLDFLAGS)==type(str()): etisserant@47: LDFLAGS=[PlugLDFLAGS] etisserant@47: #or list of strings etisserant@47: elif type(PlugLDFLAGS)==type(list()): etisserant@47: LDFLAGS=PlugLDFLAGS[:] etisserant@47: else: etisserant@47: LDFLAGS=[] etisserant@47: etisserant@14: # recurse through all childs, and stack their results etisserant@47: for PlugChild in self.IECSortedChilds(): etisserant@24: new_location = PlugChild.GetCurrentLocation() etisserant@24: # How deep are we in the tree ? etisserant@24: depth=len(new_location) etisserant@203: _LocationCFilesAndCFLAGS, _LDFLAGS, _extra_files = \ etisserant@14: PlugChild._Generate_C( etisserant@14: #keep the same path etisserant@14: buildpath, etisserant@14: # filter locations that start with current IEC location etisserant@203: [loc for loc in locations if loc["LOC"][0:depth] == new_location ]) etisserant@14: # stack the result etisserant@47: LocationCFilesAndCFLAGS += _LocationCFilesAndCFLAGS etisserant@47: LDFLAGS += _LDFLAGS etisserant@203: extra_files += _extra_files etisserant@203: etisserant@203: return LocationCFilesAndCFLAGS, LDFLAGS, extra_files etisserant@14: laurent@669: def PluginTypesFactory(self): greg@274: if self.LibraryControler is not None: laurent@669: return [{"name" : self.PlugType, "types": self.LibraryControler.Project}] etisserant@14: return [] etisserant@14: laurent@669: def ParentsTypesFactory(self): laurent@669: return self.PlugParent.ParentsTypesFactory() + self.PluginTypesFactory() laurent@669: laurent@669: def PluginsTypesFactory(self): laurent@669: list = self.PluginTypesFactory() laurent@363: for PlugChild in self.IterChilds(): laurent@669: list += PlugChild.PluginsTypesFactory() laurent@363: return list laurent@363: etisserant@14: def STLibraryFactory(self): greg@274: if self.LibraryControler is not None: lbessard@309: program, errors, warnings = self.LibraryControler.GenerateProgram() laurent@363: return program + "\n" etisserant@14: return "" etisserant@14: laurent@363: def PluginsSTLibraryFactory(self): laurent@363: program = self.STLibraryFactory() laurent@363: for PlugChild in self.IECSortedChilds(): laurent@363: program += PlugChild.PluginsSTLibraryFactory() laurent@363: return program laurent@363: etisserant@14: def IterChilds(self): etisserant@14: for PlugType, PluggedChilds in self.PluggedChilds.items(): etisserant@14: for PlugInstance in PluggedChilds: lbessard@250: yield PlugInstance etisserant@14: etisserant@47: def IECSortedChilds(self): etisserant@47: # reorder childs by IEC_channels etisserant@47: ordered = [(chld.BaseParams.getIEC_Channel(),chld) for chld in self.IterChilds()] etisserant@47: if ordered: etisserant@47: ordered.sort() etisserant@47: return zip(*ordered)[1] etisserant@47: else: etisserant@47: return [] etisserant@47: etisserant@47: def _GetChildBySomething(self, something, toks): lbessard@17: for PlugInstance in self.IterChilds(): etisserant@14: # if match component of the name etisserant@14: if getattr(PlugInstance.BaseParams, something) == toks[0]: etisserant@14: # if Name have other components etisserant@47: if len(toks) >= 2: etisserant@14: # Recurse in order to find the latest object etisserant@47: return PlugInstance._GetChildBySomething( something, toks[1:]) etisserant@14: # No sub name -> found etisserant@14: return PlugInstance etisserant@14: # Not found etisserant@14: return None etisserant@14: etisserant@14: def GetChildByName(self, Name): etisserant@47: if Name: etisserant@47: toks = Name.split('.') etisserant@47: return self._GetChildBySomething("Name", toks) etisserant@47: else: etisserant@47: return self etisserant@14: etisserant@14: def GetChildByIECLocation(self, Location): etisserant@47: if Location: etisserant@47: return self._GetChildBySomething("IEC_Channel", Location) etisserant@47: else: etisserant@47: return self etisserant@14: etisserant@23: def GetCurrentLocation(self): etisserant@24: """ etisserant@24: @return: Tupple containing plugin IEC location of current plugin : %I0.0.4.5 => (0,0,4,5) etisserant@24: """ etisserant@23: return self.PlugParent.GetCurrentLocation() + (self.BaseParams.getIEC_Channel(),) etisserant@23: etisserant@47: def GetCurrentName(self): etisserant@47: """ etisserant@47: @return: String "ParentParentName.ParentName.Name" etisserant@47: """ etisserant@47: return self.PlugParent._GetCurrentName() + self.BaseParams.getName() etisserant@47: etisserant@47: def _GetCurrentName(self): etisserant@47: """ etisserant@47: @return: String "ParentParentName.ParentName.Name." etisserant@47: """ etisserant@47: return self.PlugParent._GetCurrentName() + self.BaseParams.getName() + "." etisserant@47: etisserant@23: def GetPlugRoot(self): etisserant@23: return self.PlugParent.GetPlugRoot() etisserant@23: lbessard@97: def GetFullIEC_Channel(self): lbessard@97: return ".".join([str(i) for i in self.GetCurrentLocation()]) + ".x" lbessard@97: lbessard@97: def GetLocations(self): lbessard@97: location = self.GetCurrentLocation() lbessard@97: return [loc for loc in self.PlugParent.GetLocations() if loc["LOC"][0:len(location)] == location] lbessard@97: laurent@401: def GetVariableLocationTree(self): laurent@401: ''' laurent@401: This function is meant to be overridden by plugins. laurent@401: laurent@401: It should returns an list of dictionaries laurent@401: laurent@401: - IEC_type is an IEC type like BOOL/BYTE/SINT/... laurent@401: - location is a string of this variable's location, like "%IX0.0.0" laurent@401: ''' laurent@401: children = [] laurent@401: for child in self.IECSortedChilds(): laurent@402: children.append(child.GetVariableLocationTree()) laurent@402: return {"name": self.BaseParams.getName(), laurent@402: "type": LOCATION_PLUGIN, laurent@402: "location": self.GetFullIEC_Channel(), laurent@402: "children": children} laurent@401: lbessard@17: def GetPlugInfos(self): lbessard@17: childs = [] etisserant@33: # reorder childs by IEC_channels etisserant@47: for child in self.IECSortedChilds(): etisserant@47: childs.append(child.GetPlugInfos()) lbessard@82: if wx.VERSION < (2, 8, 0): lbessard@82: return {"name" : "%d-%s"%(self.BaseParams.getIEC_Channel(),self.BaseParams.getName()), "type" : self.BaseParams.getName(), "values" : childs} lbessard@82: else: lbessard@82: return {"name" : self.BaseParams.getName(), "channel" : self.BaseParams.getIEC_Channel(), "enabled" : self.BaseParams.getEnabled(), "parent" : len(self.PlugChildsTypes) > 0, "type" : self.BaseParams.getName(), "values" : childs} lbessard@17: etisserant@203: def FindNewName(self, DesiredName): etisserant@29: """ etisserant@29: Changes Name to DesiredName if available, Name-N if not. etisserant@29: @param DesiredName: The desired Name (string) etisserant@29: """ etisserant@29: # Get Current Name etisserant@29: CurrentName = self.BaseParams.getName() etisserant@29: # Do nothing if no change etisserant@29: #if CurrentName == DesiredName: return CurrentName etisserant@29: # Build a list of used Name out of parent's PluggedChilds etisserant@29: AllNames=[] etisserant@29: for PlugInstance in self.PlugParent.IterChilds(): etisserant@29: if PlugInstance != self: etisserant@29: AllNames.append(PlugInstance.BaseParams.getName()) etisserant@29: etisserant@29: # Find a free name, eventually appending digit etisserant@29: res = DesiredName etisserant@29: suffix = 1 etisserant@29: while res in AllNames: etisserant@29: res = "%s-%d"%(DesiredName, suffix) etisserant@29: suffix += 1 etisserant@29: etisserant@29: # Get old path etisserant@29: oldname = self.PlugPath() etisserant@29: # Check previous plugin existance etisserant@29: dontexist = self.BaseParams.getName() == "__unnamed__" etisserant@29: # Set the new name etisserant@29: self.BaseParams.setName(res) etisserant@29: # Rename plugin dir if exist etisserant@29: if not dontexist: etisserant@29: shutil.move(oldname, self.PlugPath()) etisserant@29: # warn user he has two left hands etisserant@29: if DesiredName != res: laurent@421: self.GetPlugRoot().logger.write_warning(_("A child names \"%s\" already exist -> \"%s\"\n")%(DesiredName,res)) etisserant@29: return res etisserant@29: etisserant@203: def FindNewIEC_Channel(self, DesiredChannel): etisserant@14: """ etisserant@14: Changes IEC Channel number to DesiredChannel if available, nearest available if not. etisserant@14: @param DesiredChannel: The desired IEC channel (int) etisserant@14: """ etisserant@14: # Get Current IEC channel etisserant@14: CurrentChannel = self.BaseParams.getIEC_Channel() etisserant@14: # Do nothing if no change etisserant@29: #if CurrentChannel == DesiredChannel: return CurrentChannel etisserant@14: # Build a list of used Channels out of parent's PluggedChilds etisserant@14: AllChannels=[] etisserant@14: for PlugInstance in self.PlugParent.IterChilds(): etisserant@14: if PlugInstance != self: etisserant@14: AllChannels.append(PlugInstance.BaseParams.getIEC_Channel()) etisserant@14: AllChannels.sort() etisserant@14: etisserant@14: # Now, try to guess the nearest available channel etisserant@14: res = DesiredChannel etisserant@14: while res in AllChannels: # While channel not free etisserant@14: if res < CurrentChannel: # Want to go down ? etisserant@14: res -= 1 # Test for n-1 etisserant@33: if res < 0 : laurent@421: self.GetPlugRoot().logger.write_warning(_("Cannot find lower free IEC channel than %d\n")%CurrentChannel) etisserant@33: return CurrentChannel # Can't go bellow 0, do nothing etisserant@14: else : # Want to go up ? etisserant@14: res += 1 # Test for n-1 etisserant@14: # Finally set IEC Channel etisserant@14: self.BaseParams.setIEC_Channel(res) etisserant@203: if DesiredChannel != res: laurent@421: self.GetPlugRoot().logger.write_warning(_("A child with IEC channel %d already exist -> %d\n")%(DesiredChannel,res)) etisserant@14: return res etisserant@14: laurent@656: def _OpenView(self): laurent@656: if self.EditorType is not None and self._View is None: laurent@656: app_frame = self.GetPlugRoot().AppFrame laurent@656: laurent@656: self._View = self.EditorType(app_frame.TabsOpened, self, app_frame) laurent@656: laurent@656: app_frame.EditProjectElement(self._View, self.GetFilename()) laurent@656: laurent@656: def OnCloseEditor(self): laurent@656: self._View = None laurent@656: etisserant@14: def OnPlugClose(self): laurent@656: if self._View is not None: laurent@656: app_frame = self.GetPlugRoot().AppFrame laurent@656: if app_frame is not None: laurent@656: app_frame.DeletePage(self._View) etisserant@14: return True etisserant@14: etisserant@14: def _doRemoveChild(self, PlugInstance): etisserant@14: # Remove all childs of child etisserant@14: for SubPlugInstance in PlugInstance.IterChilds(): etisserant@14: PlugInstance._doRemoveChild(SubPlugInstance) etisserant@14: # Call the OnCloseMethod etisserant@14: PlugInstance.OnPlugClose() etisserant@14: # Delete plugin dir etisserant@14: shutil.rmtree(PlugInstance.PlugPath()) etisserant@14: # Remove child of PluggedChilds etisserant@14: self.PluggedChilds[PlugInstance.PlugType].remove(PlugInstance) etisserant@14: # Forget it... (View have to refresh) etisserant@14: etisserant@51: def PlugRemove(self): etisserant@14: # Fetch the plugin etisserant@51: #PlugInstance = self.GetChildByName(PlugName) etisserant@14: # Ask to his parent to remove it etisserant@51: self.PlugParent._doRemoveChild(self) etisserant@14: laurent@417: def PlugAddChild(self, PlugName, PlugType, IEC_Channel=0): etisserant@14: """ etisserant@14: Create the plugins that may be added as child to this node self etisserant@14: @param PlugType: string desining the plugin class name (get name from PlugChildsTypes) etisserant@14: @param PlugName: string for the name of the plugin instance etisserant@14: """ etisserant@106: # reorgabize self.PlugChildsTypes tuples from (name, PlugClass, Help) etisserant@106: # to ( name, (PlugClass, Help)), an make a dict etisserant@106: transpose = zip(*self.PlugChildsTypes) etisserant@106: PlugChildsTypes = dict(zip(transpose[0],zip(transpose[1],transpose[2]))) etisserant@14: # Check that adding this plugin is allowed etisserant@14: try: etisserant@106: PlugClass, PlugHelp = PlugChildsTypes[PlugType] etisserant@14: except KeyError: laurent@361: raise Exception, _("Cannot create child %s of type %s ")%(PlugName, PlugType) etisserant@14: etisserant@14: # if PlugClass is a class factory, call it. (prevent unneeded imports) etisserant@14: if type(PlugClass) == types.FunctionType: etisserant@14: PlugClass = PlugClass() etisserant@14: etisserant@14: # Eventualy Initialize child instance list for this class of plugin lbessard@17: PluggedChildsWithSameClass = self.PluggedChilds.setdefault(PlugType, list()) etisserant@14: # Check count lbessard@17: if getattr(PlugClass, "PlugMaxCount", None) and len(PluggedChildsWithSameClass) >= PlugClass.PlugMaxCount: laurent@361: raise Exception, _("Max count (%d) reached for this plugin of type %s ")%(PlugClass.PlugMaxCount, PlugType) etisserant@14: etisserant@14: # create the final class, derived of provided plugin and template etisserant@14: class FinalPlugClass(PlugClass, PlugTemplate): etisserant@14: """ etisserant@14: Plugin class is derivated into FinalPlugClass before being instanciated etisserant@14: This way __init__ is overloaded to ensure PlugTemplate.__init__ is called etisserant@14: before PlugClass.__init__, and to do the file related stuff. etisserant@14: """ etisserant@14: def __init__(_self): etisserant@14: # self is the parent etisserant@14: _self.PlugParent = self etisserant@14: # Keep track of the plugin type name etisserant@14: _self.PlugType = PlugType etisserant@106: # remind the help string, for more fancy display etisserant@106: _self.PlugHelp = PlugHelp etisserant@14: # Call the base plugin template init - change XSD into class members etisserant@14: PlugTemplate.__init__(_self) etisserant@29: # check name is unique etisserant@203: NewPlugName = _self.FindNewName(PlugName) etisserant@14: # If dir have already be made, and file exist etisserant@29: if os.path.isdir(_self.PlugPath(NewPlugName)): #and os.path.isfile(_self.PluginXmlFilePath(PlugName)): etisserant@14: #Load the plugin.xml file into parameters members etisserant@203: _self.LoadXMLParams(NewPlugName) etisserant@20: # Basic check. Better to fail immediately. etisserant@29: if (_self.BaseParams.getName() != NewPlugName): laurent@361: raise Exception, _("Project tree layout do not match plugin.xml %s!=%s ")%(NewPlugName, _self.BaseParams.getName()) etisserant@20: etisserant@20: # Now, self.PlugPath() should be OK etisserant@20: etisserant@15: # Check that IEC_Channel is not already in use. etisserant@203: _self.FindNewIEC_Channel(_self.BaseParams.getIEC_Channel()) etisserant@14: # Call the plugin real __init__ lbessard@17: if getattr(PlugClass, "__init__", None): lbessard@17: PlugClass.__init__(_self) etisserant@14: #Load and init all the childs etisserant@203: _self.LoadChilds() etisserant@118: #just loaded, nothing to saved etisserant@118: _self.ChangesToSave = False etisserant@14: else: etisserant@14: # If plugin do not have corresponding file/dirs - they will be created on Save laurent@395: _self.PlugMakeDir() etisserant@14: # Find an IEC number laurent@417: _self.FindNewIEC_Channel(IEC_Channel) etisserant@14: # Call the plugin real __init__ lbessard@17: if getattr(PlugClass, "__init__", None): lbessard@17: PlugClass.__init__(_self) lbessard@17: _self.PlugRequestSave() etisserant@118: #just created, must be saved etisserant@118: _self.ChangesToSave = True laurent@403: lbessard@77: def _getBuildPath(_self): lbessard@77: return self._getBuildPath() lbessard@77: etisserant@14: # Create the object out of the resulting class etisserant@14: newPluginOpj = FinalPlugClass() etisserant@14: # Store it in PluggedChils etisserant@14: PluggedChildsWithSameClass.append(newPluginOpj) etisserant@14: etisserant@14: return newPluginOpj laurent@403: laurent@403: def ClearPluggedChilds(self): laurent@403: for child in self.IterChilds(): laurent@403: child.ClearPluggedChilds() laurent@403: self.PluggedChilds = {} laurent@403: lbessard@325: def LoadSTLibrary(self): lbessard@325: # Get library blocks if plcopen library exist lbessard@325: library_path = self.PluginLibraryFilePath() lbessard@325: if os.path.isfile(library_path): lbessard@325: self.LibraryControler = PLCControler() lbessard@325: self.LibraryControler.OpenXMLFile(library_path) laurent@363: self.LibraryControler.ClearPluginTypes() laurent@669: self.LibraryControler.AddPluginTypesList(self.ParentsTypesFactory()) etisserant@14: etisserant@203: def LoadXMLParams(self, PlugName = None): etisserant@105: methode_name = os.path.join(self.PlugPath(PlugName), "methods.py") etisserant@105: if os.path.isfile(methode_name): etisserant@105: execfile(methode_name) greg@274: lbessard@17: # Get the base xml tree etisserant@20: if self.MandatoryParams: etisserant@203: try: etisserant@106: basexmlfile = open(self.PluginBaseXmlFilePath(PlugName), 'r') etisserant@106: basetree = minidom.parse(basexmlfile) etisserant@106: self.MandatoryParams[1].loadXMLTree(basetree.childNodes[0]) etisserant@106: basexmlfile.close() etisserant@203: except Exception, exc: laurent@421: self.GetPlugRoot().logger.write_error(_("Couldn't load plugin base parameters %s :\n %s") % (PlugName, str(exc))) laurent@421: self.GetPlugRoot().logger.write_error(traceback.format_exc()) lbessard@17: etisserant@14: # Get the xml tree etisserant@20: if self.PlugParams: etisserant@203: try: etisserant@106: xmlfile = open(self.PluginXmlFilePath(PlugName), 'r') etisserant@106: tree = minidom.parse(xmlfile) etisserant@106: self.PlugParams[1].loadXMLTree(tree.childNodes[0]) etisserant@106: xmlfile.close() etisserant@203: except Exception, exc: laurent@421: self.GetPlugRoot().logger.write_error(_("Couldn't load plugin parameters %s :\n %s") % (PlugName, str(exc))) laurent@421: self.GetPlugRoot().logger.write_error(traceback.format_exc()) etisserant@203: etisserant@203: def LoadChilds(self): etisserant@14: # Iterate over all PlugName@PlugType in plugin directory, and try to open them etisserant@14: for PlugDir in os.listdir(self.PlugPath()): lbessard@17: if os.path.isdir(os.path.join(self.PlugPath(), PlugDir)) and \ etisserant@14: PlugDir.count(NameTypeSeparator) == 1: etisserant@24: pname, ptype = PlugDir.split(NameTypeSeparator) etisserant@203: try: etisserant@203: self.PlugAddChild(pname, ptype) etisserant@203: except Exception, exc: laurent@421: self.GetPlugRoot().logger.write_error(_("Could not add child \"%s\", type %s :\n%s\n")%(pname, ptype, str(exc))) laurent@421: self.GetPlugRoot().logger.write_error(traceback.format_exc()) etisserant@13: etisserant@109: def EnableMethod(self, method, value): etisserant@109: for d in self.PluginMethods: etisserant@109: if d["method"]==method: etisserant@109: d["enabled"]=value etisserant@109: return True etisserant@109: return False etisserant@109: etisserant@203: def ShowMethod(self, method, value): etisserant@203: for d in self.PluginMethods: etisserant@203: if d["method"]==method: etisserant@203: d["shown"]=value etisserant@203: return True etisserant@203: return False etisserant@203: Edouard@623: def CallMethod(self, method): Edouard@623: for d in self.PluginMethods: Edouard@623: if d["method"]==method and d.get("enabled", True) and d.get("shown", True): Edouard@623: getattr(self, method)() Edouard@623: lbessard@17: def _GetClassFunction(name): lbessard@17: def GetRootClass(): lbessard@17: return getattr(__import__("plugins." + name), name).RootClass lbessard@17: return GetRootClass lbessard@17: etisserant@20: etisserant@20: #################################################################################### etisserant@20: #################################################################################### etisserant@20: #################################################################################### etisserant@20: ################################### ROOT ###################################### etisserant@20: #################################################################################### etisserant@20: #################################################################################### etisserant@20: #################################################################################### etisserant@20: etisserant@81: if wx.Platform == '__WXMSW__': etisserant@75: exe_ext=".exe" etisserant@75: else: etisserant@75: exe_ext="" etisserant@75: etisserant@20: # import for project creation timestamping etisserant@239: from threading import Timer, Lock, Thread, Semaphore etisserant@20: from time import localtime etisserant@20: from datetime import datetime etisserant@20: # import necessary stuff from PLCOpenEditor etisserant@20: from PLCOpenEditor import PLCOpenEditor, ProjectDialog etisserant@20: from TextViewer import TextViewer etisserant@203: from plcopen.structures import IEC_KEYWORDS, TypeHierarchy_list etisserant@203: etisserant@203: greg@427: import re, tempfile etisserant@203: import targets Edouard@592: from targets.typemapping import DebugTypesSize Edouard@592: etisserant@203: import connectors etisserant@203: from discovery import DiscoveryDialog etisserant@235: from weakref import WeakKeyDictionary etisserant@20: lbessard@356: MATIEC_ERROR_MODEL = re.compile(".*\.st:(\d+)-(\d+)\.\.(\d+)-(\d+): error : (.*)$") lbessard@356: edouard@578: DEBUG_RETRIES_WARN = 3 edouard@578: DEBUG_RETRIES_REREGISTER = 4 edouard@578: lbessard@41: class PluginsRoot(PlugTemplate, PLCControler): etisserant@20: """ etisserant@20: This class define Root object of the plugin tree. etisserant@20: It is responsible of : etisserant@20: - Managing project directory etisserant@20: - Building project etisserant@20: - Handling PLCOpenEditor controler and view etisserant@20: - Loading user plugins and instanciante them as childs etisserant@20: - ... etisserant@20: etisserant@20: """ etisserant@13: etisserant@14: # For root object, available Childs Types are modules of the plugin packages. etisserant@106: PlugChildsTypes = [(name, _GetClassFunction(name), help) for name, help in zip(plugins.__all__,plugins.helps)] etisserant@13: etisserant@13: XSD = """ etisserant@13: etisserant@13: etisserant@13: lbessard@86: lbessard@86: lbessard@86: laurent@411: etisserant@203: """+targets.targetchoices+""" etisserant@106: etisserant@106: etisserant@106: lbessard@86: greg@204: greg@338: etisserant@13: etisserant@13: etisserant@13: etisserant@13: """ etisserant@13: etisserant@290: def __init__(self, frame, logger): lbessard@41: PLCControler.__init__(self) etisserant@227: etisserant@20: self.MandatoryParams = None laurent@417: self.SetAppFrame(frame, logger) etisserant@203: self._builder = None etisserant@203: self._connector = None etisserant@203: greg@418: self.iec2c_path = os.path.join(base_folder, "matiec", "iec2c"+exe_ext) greg@418: self.ieclib_path = os.path.join(base_folder, "matiec", "lib") greg@418: etisserant@203: # Setup debug information etisserant@227: self.IECdebug_datas = {} etisserant@227: self.IECdebug_lock = Lock() etisserant@222: etisserant@235: self.DebugTimer=None etisserant@203: self.ResetIECProgramsAndVariables() etisserant@203: etisserant@203: #This method are not called here... but in NewProject and OpenProject etisserant@203: #self._AddParamsMembers() etisserant@203: #self.PluggedChilds = {} etisserant@203: etisserant@118: # In both new or load scenario, no need to save greg@350: self.ChangesToSave = False etisserant@23: # root have no parent etisserant@13: self.PlugParent = None etisserant@13: # Keep track of the plugin type name etisserant@13: self.PlugType = "Beremiz" laurent@496: self.PluggedChilds = {} etisserant@20: # After __init__ root plugin is not valid etisserant@20: self.ProjectPath = None greg@427: self._setBuildPath(None) etisserant@286: self.DebugThread = None etisserant@286: self.debug_break = False greg@350: self.previous_plcstate = None etisserant@106: # copy PluginMethods so that it can be later customized etisserant@106: self.PluginMethods = [dic.copy() for dic in self.PluginMethods] lbessard@325: self.LoadSTLibrary() lbessard@325: laurent@395: def __del__(self): edouard@466: if self.DebugTimer: edouard@466: self.DebugTimer.cancel() edouard@466: self.KillDebugThread() laurent@395: laurent@417: def SetAppFrame(self, frame, logger): laurent@417: self.AppFrame = frame laurent@417: self.logger = logger laurent@417: self.StatusTimer = None laurent@417: laurent@417: if frame is not None: laurent@417: # Timer to pull PLC status laurent@417: ID_STATUSTIMER = wx.NewId() laurent@417: self.StatusTimer = wx.Timer(self.AppFrame, ID_STATUSTIMER) laurent@417: self.AppFrame.Bind(wx.EVT_TIMER, self.PullPLCStatusProc, self.StatusTimer) laurent@652: laurent@652: self.RefreshPluginsBlockLists() laurent@417: laurent@417: def ResetAppFrame(self, logger): laurent@417: if self.AppFrame is not None: laurent@417: self.AppFrame.Unbind(wx.EVT_TIMER, self.StatusTimer) laurent@417: self.StatusTimer = None laurent@417: self.AppFrame = None laurent@417: laurent@417: self.logger = logger laurent@417: lbessard@325: def PluginLibraryFilePath(self): lbessard@325: return os.path.join(os.path.split(__file__)[0], "pous.xml") greg@274: etisserant@118: def PlugTestModified(self): etisserant@118: return self.ChangesToSave or not self.ProjectIsSaved() etisserant@118: laurent@656: def PlugFullName(self): laurent@656: return "" laurent@656: etisserant@23: def GetPlugRoot(self): etisserant@23: return self etisserant@23: greg@418: def GetIECLibPath(self): greg@418: return self.ieclib_path greg@418: greg@418: def GetIEC2cPath(self): greg@418: return self.iec2c_path greg@418: etisserant@23: def GetCurrentLocation(self): etisserant@23: return () etisserant@47: etisserant@47: def GetCurrentName(self): etisserant@47: return "" etisserant@47: etisserant@47: def _GetCurrentName(self): etisserant@47: return "" etisserant@47: lbessard@17: def GetProjectPath(self): lbessard@17: return self.ProjectPath etisserant@51: etisserant@51: def GetProjectName(self): etisserant@51: return os.path.split(self.ProjectPath)[1] lbessard@17: etisserant@20: def GetPlugInfos(self): etisserant@20: childs = [] etisserant@20: for child in self.IterChilds(): etisserant@20: childs.append(child.GetPlugInfos()) etisserant@51: return {"name" : "PLC (%s)"%self.GetProjectName(), "type" : None, "values" : childs} laurent@411: laurent@510: def GetDefaultTargetName(self): laurent@411: if wx.Platform == '__WXMSW__': laurent@510: return "Win32" laurent@411: else: laurent@510: return "Linux" laurent@510: laurent@510: def GetTarget(self): laurent@510: target = self.BeremizRoot.getTargetType() laurent@510: if target.getcontent() is None: laurent@510: target = self.Classes["BeremizRoot_TargetType"]() laurent@510: target_name = self.GetDefaultTargetName() laurent@510: target.setcontent({"name": target_name, "value": self.Classes["TargetType_%s"%target_name]()}) laurent@411: return target laurent@411: laurent@411: def GetParamsAttributes(self, path = None): laurent@411: params = PlugTemplate.GetParamsAttributes(self, path) laurent@411: if params[0]["name"] == "BeremizRoot": laurent@411: for child in params[0]["children"]: laurent@411: if child["name"] == "TargetType" and child["value"] == '': laurent@510: child.update(self.GetTarget().getElementInfos("TargetType")) laurent@411: return params laurent@411: laurent@411: def SetParamsAttribute(self, path, value): laurent@411: if path.startswith("BeremizRoot.TargetType.") and self.BeremizRoot.getTargetType().getcontent() is None: laurent@607: self.BeremizRoot.setTargetType(self.GetTarget()) laurent@411: return PlugTemplate.SetParamsAttribute(self, path, value) greg@427: greg@427: # helper func to check project path write permission greg@427: def CheckProjectPathPerm(self, dosave=True): greg@427: if CheckPathPerm(self.ProjectPath): greg@427: return True greg@427: dialog = wx.MessageDialog(self.AppFrame, laurent@428: _('You must have permission to work on the project\nWork on a project copy ?'), greg@427: _('Error'), greg@427: wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION) greg@427: answer = dialog.ShowModal() greg@427: dialog.Destroy() greg@427: if answer == wx.ID_YES: greg@427: if self.SaveProjectAs(): greg@427: self.AppFrame.RefreshAll() greg@427: self.AppFrame.RefreshTitle() laurent@534: self.AppFrame.RefreshFileMenu() greg@427: return True greg@427: return False etisserant@20: greg@256: def NewProject(self, ProjectPath, BuildPath=None): lbessard@17: """ lbessard@17: Create a new project in an empty folder lbessard@17: @param ProjectPath: path of the folder where project have to be created lbessard@17: @param PLCParams: properties of the PLCOpen program created lbessard@17: """ laurent@415: # Verify that chosen folder is empty lbessard@17: if not os.path.isdir(ProjectPath) or len(os.listdir(ProjectPath)) > 0: laurent@415: return _("Chosen folder isn't empty. You can't use it for a new project!") etisserant@20: etisserant@20: dialog = ProjectDialog(self.AppFrame) etisserant@20: if dialog.ShowModal() == wx.ID_OK: etisserant@20: values = dialog.GetValues() etisserant@20: values["creationDateTime"] = datetime(*localtime()[:6]) etisserant@20: dialog.Destroy() etisserant@20: else: etisserant@20: dialog.Destroy() laurent@361: return _("Project not created") etisserant@20: lbessard@41: # Create PLCOpen program etisserant@113: self.CreateNewProject(values) etisserant@13: # Change XSD into class members etisserant@13: self._AddParamsMembers() etisserant@13: self.PluggedChilds = {} lbessard@17: # Keep track of the root plugin (i.e. project path) lbessard@17: self.ProjectPath = ProjectPath greg@427: self._setBuildPath(BuildPath) etisserant@114: # get plugins bloclist (is that usefull at project creation?) lbessard@41: self.RefreshPluginsBlockLists() etisserant@114: # this will create files base XML files etisserant@114: self.SaveProject() lbessard@17: return None lbessard@17: greg@256: def LoadProject(self, ProjectPath, BuildPath=None): lbessard@17: """ lbessard@17: Load a project contained in a folder lbessard@17: @param ProjectPath: path of the project folder lbessard@17: """ lbessard@190: if os.path.basename(ProjectPath) == "": lbessard@190: ProjectPath = os.path.dirname(ProjectPath) etisserant@203: # Verify that project contains a PLCOpen program lbessard@17: plc_file = os.path.join(ProjectPath, "plc.xml") lbessard@17: if not os.path.isfile(plc_file): laurent@415: return _("Chosen folder doesn't contain a program. It's not a valid project!") lbessard@17: # Load PLCOpen file lbessard@41: result = self.OpenXMLFile(plc_file) lbessard@17: if result: lbessard@17: return result lbessard@17: # Change XSD into class members lbessard@17: self._AddParamsMembers() lbessard@17: self.PluggedChilds = {} lbessard@17: # Keep track of the root plugin (i.e. project path) lbessard@17: self.ProjectPath = ProjectPath greg@427: self._setBuildPath(BuildPath) etisserant@13: # If dir have already be made, and file exist lbessard@17: if os.path.isdir(self.PlugPath()) and os.path.isfile(self.PluginXmlFilePath()): etisserant@13: #Load the plugin.xml file into parameters members etisserant@203: result = self.LoadXMLParams() lbessard@17: if result: lbessard@17: return result etisserant@13: #Load and init all the childs etisserant@203: self.LoadChilds() lbessard@41: self.RefreshPluginsBlockLists() etisserant@203: etisserant@203: if os.path.exists(self._getBuildPath()): etisserant@203: self.EnableMethod("_Clean", True) etisserant@203: etisserant@203: if os.path.isfile(self._getIECrawcodepath()): etisserant@203: self.ShowMethod("_showIECcode", True) etisserant@203: lbessard@17: return None lbessard@17: laurent@403: def CloseProject(self): laurent@403: self.ClearPluggedChilds() laurent@417: self.ResetAppFrame(None) laurent@417: lbessard@17: def SaveProject(self): greg@427: if self.CheckProjectPathPerm(False): lbessard@41: self.SaveXMLFile(os.path.join(self.ProjectPath, 'plc.xml')) greg@427: result = self.PlugRequestSave() greg@427: if result: greg@427: self.logger.write_error(result) greg@427: greg@427: def SaveProjectAs(self, dosave=True): greg@427: # Ask user to choose a path with write permissions laurent@529: if wx.Platform == '__WXMSW__': laurent@529: path = os.getenv("USERPROFILE") laurent@529: else: laurent@529: path = os.getenv("HOME") laurent@529: dirdialog = wx.DirDialog(self.AppFrame , _("Choose a directory to save project"), path, wx.DD_NEW_DIR_BUTTON) greg@427: answer = dirdialog.ShowModal() greg@427: dirdialog.Destroy() greg@427: if answer == wx.ID_OK: greg@427: newprojectpath = dirdialog.GetPath() greg@427: if os.path.isdir(newprojectpath): greg@427: self.ProjectPath = newprojectpath greg@427: if dosave: greg@427: self.SaveProject() greg@427: self._setBuildPath(self.BuildPath) greg@427: return True greg@427: return False lbessard@17: lbessard@41: # Update PLCOpenEditor Plugin Block types from loaded plugins lbessard@41: def RefreshPluginsBlockLists(self): lbessard@62: if getattr(self, "PluggedChilds", None) is not None: lbessard@202: self.ClearPluginTypes() laurent@669: self.AddPluginTypesList(self.PluginsTypesFactory()) laurent@395: if self.AppFrame is not None: laurent@395: self.AppFrame.RefreshLibraryTree() laurent@395: self.AppFrame.RefreshEditor() lbessard@41: laurent@443: # Update a PLCOpenEditor Pou variable location laurent@443: def UpdateProjectVariableLocation(self, old_leading, new_leading): laurent@443: self.Project.updateElementAddress(old_leading, new_leading) laurent@443: self.BufferProject() laurent@443: if self.AppFrame is not None: laurent@468: self.AppFrame.RefreshTitle() laurent@468: self.AppFrame.RefreshInstancesTree() laurent@468: self.AppFrame.RefreshFileMenu() laurent@468: self.AppFrame.RefreshEditMenu() laurent@443: self.AppFrame.RefreshEditor() laurent@443: laurent@401: def GetVariableLocationTree(self): laurent@411: ''' laurent@411: This function is meant to be overridden by plugins. laurent@411: laurent@411: It should returns an list of dictionaries laurent@411: laurent@411: - IEC_type is an IEC type like BOOL/BYTE/SINT/... laurent@411: - location is a string of this variable's location, like "%IX0.0.0" laurent@411: ''' laurent@411: children = [] laurent@411: for child in self.IECSortedChilds(): laurent@411: children.append(child.GetVariableLocationTree()) laurent@411: return children laurent@401: laurent@363: def PluginPath(self): laurent@363: return os.path.join(os.path.split(__file__)[0], "plugins") laurent@363: lbessard@17: def PlugPath(self, PlugName=None): etisserant@13: return self.ProjectPath lbessard@17: etisserant@13: def PluginXmlFilePath(self, PlugName=None): etisserant@13: return os.path.join(self.PlugPath(PlugName), "beremiz.xml") etisserant@18: laurent@669: def ParentsTypesFactory(self): laurent@669: return self.PluginTypesFactory() laurent@363: greg@427: def _setBuildPath(self, buildpath): greg@427: if CheckPathPerm(buildpath): greg@427: self.BuildPath = buildpath greg@427: else: greg@427: self.BuildPath = None greg@427: self.BuildPath = buildpath greg@427: self.DefaultBuildPath = None greg@427: if self._builder is not None: greg@427: self._builder.SetBuildPath(self._getBuildPath()) greg@427: etisserant@20: def _getBuildPath(self): greg@427: # BuildPath is defined by user greg@427: if self.BuildPath is not None: greg@427: return self.BuildPath greg@427: # BuildPath isn't defined by user but already created by default greg@427: if self.DefaultBuildPath is not None: greg@427: return self.DefaultBuildPath greg@427: # Create a build path in project folder if user has permissions greg@427: if CheckPathPerm(self.ProjectPath): greg@427: self.DefaultBuildPath = os.path.join(self.ProjectPath, "build") greg@427: # Create a build path in temp folder greg@427: else: greg@427: self.DefaultBuildPath = os.path.join(tempfile.mkdtemp(), os.path.basename(self.ProjectPath), "build") greg@427: greg@427: if not os.path.exists(self.DefaultBuildPath): greg@427: os.makedirs(self.DefaultBuildPath) greg@427: return self.DefaultBuildPath etisserant@20: etisserant@203: def _getExtraFilesPath(self): etisserant@203: return os.path.join(self._getBuildPath(), "extra_files") etisserant@203: etisserant@20: def _getIECcodepath(self): etisserant@20: # define name for IEC code file etisserant@20: return os.path.join(self._getBuildPath(), "plc.st") etisserant@20: lbessard@65: def _getIECgeneratedcodepath(self): lbessard@65: # define name for IEC generated code file lbessard@65: return os.path.join(self._getBuildPath(), "generated_plc.st") lbessard@65: lbessard@65: def _getIECrawcodepath(self): lbessard@65: # define name for IEC raw code file etisserant@203: return os.path.join(self.PlugPath(), "raw_plc.st") lbessard@65: lbessard@97: def GetLocations(self): lbessard@97: locations = [] lbessard@97: filepath = os.path.join(self._getBuildPath(),"LOCATED_VARIABLES.h") lbessard@97: if os.path.isfile(filepath): lbessard@97: # IEC2C compiler generate a list of located variables : LOCATED_VARIABLES.h lbessard@97: location_file = open(os.path.join(self._getBuildPath(),"LOCATED_VARIABLES.h")) lbessard@97: # each line of LOCATED_VARIABLES.h declares a located variable lbessard@97: lines = [line.strip() for line in location_file.readlines()] lbessard@97: # This regular expression parses the lines genereated by IEC2C lbessard@348: LOCATED_MODEL = re.compile("__LOCATED_VAR\((?P[A-Z]*),(?P[_A-Za-z0-9]*),(?P[QMI])(?:,(?P[XBWDL]))?,(?P[,0-9]*)\)") lbessard@97: for line in lines: lbessard@97: # If line match RE, lbessard@97: result = LOCATED_MODEL.match(line) lbessard@97: if result: lbessard@97: # Get the resulting dict lbessard@97: resdict = result.groupdict() lbessard@97: # rewrite string for variadic location as a tuple of integers lbessard@97: resdict['LOC'] = tuple(map(int,resdict['LOC'].split(','))) lbessard@97: # set located size to 'X' if not given lbessard@97: if not resdict['SIZE']: lbessard@97: resdict['SIZE'] = 'X' lbessard@97: # finally store into located variable list lbessard@97: locations.append(resdict) lbessard@97: return locations lbessard@97: etisserant@203: def _Generate_SoftPLC(self): etisserant@20: """ lbessard@64: Generate SoftPLC ST/IL/SFC code out of PLCOpenEditor controller, and compile it with IEC2C etisserant@20: @param buildpath: path where files should be created etisserant@20: """ etisserant@20: lbessard@41: # Update PLCOpenEditor Plugin Block types before generate ST code lbessard@41: self.RefreshPluginsBlockLists() lbessard@41: laurent@361: self.logger.write(_("Generating SoftPLC IEC-61131 ST/IL/SFC code...\n")) etisserant@20: buildpath = self._getBuildPath() etisserant@20: # ask PLCOpenEditor controller to write ST/IL/SFC code file lbessard@309: program, errors, warnings = self.GenerateProgram(self._getIECgeneratedcodepath()) lbessard@309: if len(warnings) > 0: laurent@361: self.logger.write_warning(_("Warnings in ST/IL/SFC code generator :\n")) lbessard@309: for warning in warnings: lbessard@309: self.logger.write_warning("%s\n"%warning) lbessard@309: if len(errors) > 0: etisserant@20: # Failed ! laurent@361: self.logger.write_error(_("Error in ST/IL/SFC code generator :\n%s\n")%errors[0]) etisserant@20: return False lbessard@65: plc_file = open(self._getIECcodepath(), "w") laurent@363: # Add ST Library from plugins laurent@363: plc_file.write(self.PluginsSTLibraryFactory()) lbessard@65: if os.path.isfile(self._getIECrawcodepath()): lbessard@65: plc_file.write(open(self._getIECrawcodepath(), "r").read()) lbessard@65: plc_file.write("\n") lbessard@356: plc_file.close() lbessard@356: plc_file = open(self._getIECcodepath(), "r") lbessard@356: self.ProgramOffset = 0 lbessard@356: for line in plc_file.xreadlines(): lbessard@356: self.ProgramOffset += 1 lbessard@356: plc_file.close() lbessard@356: plc_file = open(self._getIECcodepath(), "a") lbessard@65: plc_file.write(open(self._getIECgeneratedcodepath(), "r").read()) lbessard@65: plc_file.close() laurent@415: laurent@415: self.logger.write(_("Compiling IEC Program into C code...\n")) laurent@415: etisserant@20: # Now compile IEC code into many C files etisserant@20: # files are listed to stdout, and errors to stderr. etisserant@110: status, result, err_result = ProcessLogger( etisserant@203: self.logger, lbessard@351: "\"%s\" -f -I \"%s\" -T \"%s\" \"%s\""%( greg@418: self.iec2c_path, greg@418: self.ieclib_path, lbessard@351: buildpath, lbessard@351: self._getIECcodepath()), lbessard@356: no_stdout=True, no_stderr=True).spin() etisserant@20: if status: etisserant@20: # Failed ! lbessard@356: lbessard@356: # parse iec2c's error message. if it contains a line number, lbessard@356: # then print those lines from the generated IEC file. lbessard@356: for err_line in err_result.split('\n'): lbessard@356: self.logger.write_warning(err_line + "\n") lbessard@356: lbessard@356: m_result = MATIEC_ERROR_MODEL.match(err_line) lbessard@356: if m_result is not None: lbessard@356: first_line, first_column, last_line, last_column, error = m_result.groups() lbessard@356: first_line, last_line = int(first_line), int(last_line) lbessard@356: lbessard@356: last_section = None lbessard@356: f = open(self._getIECcodepath()) lbessard@356: lbessard@356: for i, line in enumerate(f.readlines()): laurent@661: i = i + 1 lbessard@356: if line[0] not in '\t \r\n': lbessard@356: last_section = line lbessard@356: lbessard@356: if first_line <= i <= last_line: lbessard@356: if last_section is not None: lbessard@356: self.logger.write_warning("In section: " + last_section) lbessard@356: last_section = None # only write section once lbessard@356: self.logger.write_warning("%04d: %s" % (i, line)) lbessard@356: lbessard@356: f.close() lbessard@356: laurent@361: self.logger.write_error(_("Error : IEC to C compiler returned %d\n")%status) etisserant@20: return False lbessard@356: etisserant@20: # Now extract C files of stdout etisserant@113: C_files = [ fname for fname in result.splitlines() if fname[-2:]==".c" or fname[-2:]==".C" ] etisserant@20: # remove those that are not to be compiled because included by others etisserant@20: C_files.remove("POUS.c") etisserant@115: if not C_files: laurent@415: self.logger.write_error(_("Error : At least one configuration and one resource must be declared in PLC !\n")) etisserant@115: return False etisserant@20: # transform those base names to full names with path etisserant@23: C_files = map(lambda filename:os.path.join(buildpath, filename), C_files) laurent@361: self.logger.write(_("Extracting Located Variables...\n")) lbessard@97: # Keep track of generated located variables for later use by self._Generate_C lbessard@97: self.PLCGeneratedLocatedVars = self.GetLocations() etisserant@20: # Keep track of generated C files for later use by self.PlugGenerate_C etisserant@18: self.PLCGeneratedCFiles = C_files etisserant@49: # compute CFLAGS for plc greg@418: self.plcCFLAGS = "\"-I"+self.ieclib_path+"\"" etisserant@18: return True etisserant@18: etisserant@203: def GetBuilder(self): etisserant@203: """ etisserant@203: Return a Builder (compile C code into machine code) etisserant@203: """ etisserant@203: # Get target, module and class name laurent@510: targetname = self.GetTarget().getcontent()["name"] etisserant@203: modulename = "targets." + targetname etisserant@203: classname = targetname + "_target" etisserant@203: etisserant@203: # Get module reference etisserant@203: try : etisserant@203: targetmodule = getattr(__import__(modulename), targetname) etisserant@203: etisserant@203: except Exception, msg: laurent@361: self.logger.write_error(_("Can't find module for target %s!\n")%targetname) etisserant@203: self.logger.write_error(str(msg)) etisserant@203: return None etisserant@203: etisserant@203: # Get target class etisserant@203: targetclass = getattr(targetmodule, classname) etisserant@203: etisserant@203: # if target already etisserant@203: if self._builder is None or not isinstance(self._builder,targetclass): etisserant@203: # Get classname instance etisserant@203: self._builder = targetclass(self) etisserant@203: return self._builder etisserant@203: etisserant@203: def GetLastBuildMD5(self): etisserant@203: builder=self.GetBuilder() etisserant@203: if builder is not None: etisserant@203: return builder.GetBinaryCodeMD5() etisserant@203: else: etisserant@203: return None etisserant@203: etisserant@203: ####################################################################### etisserant@203: # etisserant@203: # C CODE GENERATION METHODS etisserant@203: # etisserant@203: ####################################################################### etisserant@203: etisserant@203: def PlugGenerate_C(self, buildpath, locations): etisserant@203: """ etisserant@203: Return C code generated by iec2c compiler etisserant@203: when _generate_softPLC have been called etisserant@203: @param locations: ignored etisserant@203: @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND etisserant@203: """ etisserant@283: laurent@366: return ([(C_file_name, self.plcCFLAGS) etisserant@283: for C_file_name in self.PLCGeneratedCFiles ], etisserant@283: "", # no ldflags etisserant@283: False) # do not expose retreive/publish calls etisserant@203: etisserant@203: def ResetIECProgramsAndVariables(self): etisserant@203: """ etisserant@203: Reset variable and program list that are parsed from etisserant@203: CSV file generated by IEC2C compiler. etisserant@203: """ etisserant@203: self._ProgramList = None etisserant@203: self._VariablesList = None edouard@532: self._IECPathToIdx = {} laurent@670: self._Ticktime = 0 etisserant@235: self.TracedIECPath = [] etisserant@235: etisserant@203: def GetIECProgramsAndVariables(self): etisserant@203: """ etisserant@203: Parse CSV-like file VARIABLES.csv resulting from IEC2C compiler. etisserant@203: Each section is marked with a line staring with '//' etisserant@203: list of all variables used in various POUs etisserant@203: """ etisserant@203: if self._ProgramList is None or self._VariablesList is None: etisserant@203: try: etisserant@203: csvfile = os.path.join(self._getBuildPath(),"VARIABLES.csv") etisserant@203: # describes CSV columns etisserant@203: ProgramsListAttributeName = ["num", "C_path", "type"] etisserant@203: VariablesListAttributeName = ["num", "vartype", "IEC_path", "C_path", "type"] etisserant@203: self._ProgramList = [] etisserant@203: self._VariablesList = [] etisserant@203: self._IECPathToIdx = {} etisserant@203: etisserant@203: # Separate sections etisserant@203: ListGroup = [] etisserant@203: for line in open(csvfile,'r').xreadlines(): etisserant@203: strippedline = line.strip() etisserant@203: if strippedline.startswith("//"): etisserant@203: # Start new section etisserant@203: ListGroup.append([]) etisserant@203: elif len(strippedline) > 0 and len(ListGroup) > 0: etisserant@203: # append to this section etisserant@203: ListGroup[-1].append(strippedline) etisserant@203: etisserant@203: # first section contains programs etisserant@203: for line in ListGroup[0]: etisserant@203: # Split and Maps each field to dictionnary entries etisserant@203: attrs = dict(zip(ProgramsListAttributeName,line.strip().split(';'))) etisserant@203: # Truncate "C_path" to remove conf an ressources names etisserant@203: attrs["C_path"] = '__'.join(attrs["C_path"].split(".",2)[1:]) etisserant@203: # Push this dictionnary into result. etisserant@203: self._ProgramList.append(attrs) etisserant@203: etisserant@203: # second section contains all variables etisserant@203: for line in ListGroup[1]: etisserant@203: # Split and Maps each field to dictionnary entries etisserant@203: attrs = dict(zip(VariablesListAttributeName,line.strip().split(';'))) etisserant@203: # Truncate "C_path" to remove conf an ressources names laurent@639: parts = attrs["C_path"].split(".",2) laurent@639: if len(parts) > 2: laurent@639: attrs["C_path"] = '__'.join(parts[1:]) laurent@639: else: laurent@639: attrs["C_path"] = '__'.join(parts) etisserant@203: # Push this dictionnary into result. etisserant@203: self._VariablesList.append(attrs) etisserant@203: # Fill in IEC<->C translation dicts etisserant@203: IEC_path=attrs["IEC_path"] etisserant@203: Idx=int(attrs["num"]) edouard@450: self._IECPathToIdx[IEC_path]=(Idx, attrs["type"]) laurent@670: laurent@670: # third section contains ticktime laurent@670: if len(ListGroup) > 2: laurent@670: self._Ticktime = int(ListGroup[2][0]) laurent@670: etisserant@203: except Exception,e: laurent@361: self.logger.write_error(_("Cannot open/parse VARIABLES.csv!\n")) etisserant@203: self.logger.write_error(traceback.format_exc()) etisserant@203: self.ResetIECProgramsAndVariables() etisserant@203: return False etisserant@203: etisserant@203: return True etisserant@203: etisserant@203: def Generate_plc_debugger(self): etisserant@203: """ etisserant@203: Generate trace/debug code out of PLC variable list etisserant@203: """ etisserant@203: self.GetIECProgramsAndVariables() etisserant@203: etisserant@203: # prepare debug code etisserant@209: debug_code = targets.code("plc_debug") % { greg@335: "buffer_size": reduce(lambda x, y: x + y, [DebugTypesSize.get(v["type"], 0) for v in self._VariablesList], 0), etisserant@203: "programs_declarations": etisserant@203: "\n".join(["extern %(type)s %(C_path)s;"%p for p in self._ProgramList]), etisserant@203: "extern_variables_declarations":"\n".join([ laurent@506: {"EXT":"extern __IEC_%(type)s_p %(C_path)s;", laurent@506: "IN":"extern __IEC_%(type)s_p %(C_path)s;", laurent@601: "MEM":"extern __IEC_%(type)s_p %(C_path)s;", laurent@506: "OUT":"extern __IEC_%(type)s_p %(C_path)s;", laurent@463: "VAR":"extern __IEC_%(type)s_t %(C_path)s;"}[v["vartype"]]%v lbessard@275: for v in self._VariablesList if v["vartype"] != "FB" and v["C_path"].find('.')<0]), edouard@450: "for_each_variable_do_code":"\n".join([ laurent@506: {"EXT":" (*fp)((void*)&%(C_path)s,%(type)s_P_ENUM);\n", laurent@506: "IN":" (*fp)((void*)&%(C_path)s,%(type)s_P_ENUM);\n", Edouard@604: "MEM":" (*fp)((void*)&%(C_path)s,%(type)s_O_ENUM);\n", laurent@511: "OUT":" (*fp)((void*)&%(C_path)s,%(type)s_O_ENUM);\n", edouard@450: "VAR":" (*fp)((void*)&%(C_path)s,%(type)s_ENUM);\n"}[v["vartype"]]%v Edouard@592: for v in self._VariablesList if v["vartype"] != "FB" and v["type"] in DebugTypesSize ]), edouard@450: "find_variable_case_code":"\n".join([ edouard@450: " case %(num)s:\n"%v+ edouard@458: " *varp = (void*)&%(C_path)s;\n"%v+ laurent@506: {"EXT":" return %(type)s_P_ENUM;\n", laurent@506: "IN":" return %(type)s_P_ENUM;\n", Edouard@604: "MEM":" return %(type)s_O_ENUM;\n", laurent@511: "OUT":" return %(type)s_O_ENUM;\n", edouard@450: "VAR":" return %(type)s_ENUM;\n"}[v["vartype"]]%v Edouard@592: for v in self._VariablesList if v["vartype"] != "FB" and v["type"] in DebugTypesSize ])} etisserant@203: etisserant@203: return debug_code etisserant@203: etisserant@203: def Generate_plc_common_main(self): etisserant@203: """ etisserant@203: Use plugins layout given in LocationCFilesAndCFLAGS to etisserant@203: generate glue code that dispatch calls to all plugins etisserant@203: """ etisserant@203: # filter location that are related to code that will be called etisserant@203: # in retreive, publish, init, cleanup etisserant@203: locstrs = map(lambda x:"_".join(map(str,x)), etisserant@203: [loc for loc,Cfiles,DoCalls in self.LocationCFilesAndCFLAGS if loc and DoCalls]) etisserant@203: etisserant@203: # Generate main, based on template greg@338: if self.BeremizRoot.getEnable_Plugins(): greg@338: plc_main_code = targets.code("plc_common_main") % { greg@338: "calls_prototypes":"\n".join([( greg@338: "int __init_%(s)s(int argc,char **argv);\n"+ greg@418: "void __cleanup_%(s)s(void);\n"+ greg@418: "void __retrieve_%(s)s(void);\n"+ greg@418: "void __publish_%(s)s(void);")%{'s':locstr} for locstr in locstrs]), greg@338: "retrieve_calls":"\n ".join([ greg@338: "__retrieve_%s();"%locstr for locstr in locstrs]), greg@338: "publish_calls":"\n ".join([ #Call publish in reverse order greg@338: "__publish_%s();"%locstrs[i-1] for i in xrange(len(locstrs), 0, -1)]), greg@338: "init_calls":"\n ".join([ greg@338: "init_level=%d; "%(i+1)+ greg@423: "if((res = __init_%s(argc,argv))){"%locstr + greg@338: #"printf(\"%s\"); "%locstr + #for debug greg@338: "return res;}" for i,locstr in enumerate(locstrs)]), greg@338: "cleanup_calls":"\n ".join([ greg@338: "if(init_level >= %d) "%i+ greg@338: "__cleanup_%s();"%locstrs[i-1] for i in xrange(len(locstrs), 0, -1)]) greg@338: } greg@338: else: greg@338: plc_main_code = targets.code("plc_common_main") % { greg@338: "calls_prototypes":"\n", greg@338: "retrieve_calls":"\n", greg@338: "publish_calls":"\n", greg@338: "init_calls":"\n", greg@338: "cleanup_calls":"\n" greg@338: } laurent@510: plc_main_code += targets.targetcode(self.GetTarget().getcontent()["name"]) etisserant@203: return plc_main_code etisserant@203: etisserant@203: Edouard@623: def _Build(self): etisserant@20: """ etisserant@20: Method called by user to (re)build SoftPLC and plugin tree etisserant@20: """ laurent@395: if self.AppFrame is not None: laurent@395: self.AppFrame.ClearErrors() lbessard@202: etisserant@20: buildpath = self._getBuildPath() etisserant@20: etisserant@20: # Eventually create build dir etisserant@18: if not os.path.exists(buildpath): etisserant@18: os.mkdir(buildpath) etisserant@203: # There is something to clean etisserant@110: self.EnableMethod("_Clean", True) etisserant@203: etisserant@203: self.logger.flush() laurent@361: self.logger.write(_("Start build in %s\n") % buildpath) etisserant@203: etisserant@203: # Generate SoftPLC IEC code etisserant@203: IECGenRes = self._Generate_SoftPLC() etisserant@203: self.ShowMethod("_showIECcode", True) etisserant@203: etisserant@203: # If IEC code gen fail, bail out. etisserant@203: if not IECGenRes: laurent@361: self.logger.write_error(_("IEC-61131-3 code generation failed !\n")) etisserant@20: return False etisserant@20: etisserant@203: # Reset variable and program list that are parsed from etisserant@203: # CSV file generated by IEC2C compiler. etisserant@203: self.ResetIECProgramsAndVariables() etisserant@18: etisserant@20: # Generate C code and compilation params from plugin hierarchy laurent@361: self.logger.write(_("Generating plugins C code\n")) etisserant@24: try: etisserant@203: self.LocationCFilesAndCFLAGS, self.LDFLAGS, ExtraFiles = self._Generate_C( etisserant@24: buildpath, etisserant@203: self.PLCGeneratedLocatedVars) etisserant@178: except Exception, exc: laurent@361: self.logger.write_error(_("Plugins code generation failed !\n")) etisserant@203: self.logger.write_error(traceback.format_exc()) etisserant@24: return False etisserant@18: laurent@361: # Get temporary directory path etisserant@203: extrafilespath = self._getExtraFilesPath() etisserant@203: # Remove old directory etisserant@203: if os.path.exists(extrafilespath): etisserant@203: shutil.rmtree(extrafilespath) etisserant@203: # Recreate directory etisserant@203: os.mkdir(extrafilespath) etisserant@203: # Then write the files etisserant@203: for fname,fobject in ExtraFiles: etisserant@203: fpath = os.path.join(extrafilespath,fname) etisserant@203: open(fpath, "wb").write(fobject.read()) etisserant@203: # Now we can forget ExtraFiles (will close files object) etisserant@203: del ExtraFiles laurent@510: etisserant@203: # Template based part of C code generation etisserant@203: # files are stacked at the beginning, as files of plugin tree root etisserant@203: for generator, filename, name in [ etisserant@203: # debugger code etisserant@203: (self.Generate_plc_debugger, "plc_debugger.c", "Debugger"), etisserant@203: # init/cleanup/retrieve/publish, run and align code etisserant@203: (self.Generate_plc_common_main,"plc_common_main.c","Common runtime")]: etisserant@203: try: etisserant@203: # Do generate etisserant@203: code = generator() greg@335: if code is None: greg@335: raise etisserant@203: code_path = os.path.join(buildpath,filename) etisserant@203: open(code_path, "w").write(code) etisserant@203: # Insert this file as first file to be compiled at root plugin etisserant@203: self.LocationCFilesAndCFLAGS[0][1].insert(0,(code_path, self.plcCFLAGS)) etisserant@203: except Exception, exc: laurent@361: self.logger.write_error(name+_(" generation failed !\n")) etisserant@203: self.logger.write_error(traceback.format_exc()) etisserant@203: return False etisserant@203: laurent@361: self.logger.write(_("C code generated successfully.\n")) etisserant@203: etisserant@203: # Get current or fresh builder etisserant@203: builder = self.GetBuilder() etisserant@203: if builder is None: laurent@361: self.logger.write_error(_("Fatal : cannot get builder.\n")) etisserant@51: return False etisserant@203: etisserant@203: # Build etisserant@203: try: etisserant@203: if not builder.build() : laurent@361: self.logger.write_error(_("C Build failed.\n")) etisserant@203: return False etisserant@203: except Exception, exc: laurent@361: self.logger.write_error(_("C Build crashed !\n")) etisserant@203: self.logger.write_error(traceback.format_exc()) etisserant@203: return False etisserant@203: Edouard@624: self.logger.write(_("Successfully built.\n")) etisserant@203: # Update GUI status about need for transfer etisserant@203: self.CompareLocalAndRemotePLC() etisserant@49: return True lbessard@202: lbessard@202: def ShowError(self, logger, from_location, to_location): lbessard@202: chunk_infos = self.GetChunkInfos(from_location, to_location) lbessard@202: for infos, (start_row, start_col) in chunk_infos: lbessard@202: start = (from_location[0] - start_row, from_location[1] - start_col) lbessard@202: end = (to_location[0] - start_row, to_location[1] - start_col) laurent@661: print from_location, to_location, start_row, start_col, start, end laurent@396: if self.AppFrame is not None: laurent@396: self.AppFrame.ShowError(infos, start, end) etisserant@203: etisserant@203: def _showIECcode(self): etisserant@20: plc_file = self._getIECcodepath() laurent@630: laurent@630: IEC_code_viewer = TextViewer(self.AppFrame.TabsOpened, "", None, None, instancepath="IEC code") laurent@630: #IEC_code_viewer.Enable(False) laurent@630: IEC_code_viewer.SetTextSyntax("ALL") laurent@630: IEC_code_viewer.SetKeywords(IEC_KEYWORDS) etisserant@20: try: etisserant@20: text = file(plc_file).read() etisserant@20: except: etisserant@20: text = '(* No IEC code have been generated at that time ! *)' laurent@630: IEC_code_viewer.SetText(text = text) laurent@630: IEC_code_viewer.SetIcon(self.AppFrame.GenerateBitmap("ST")) lbessard@65: laurent@630: self.AppFrame.EditProjectElement(IEC_code_viewer, "IEC code") laurent@630: etisserant@203: def _editIECrawcode(self): lbessard@173: new_dialog = wx.Frame(self.AppFrame) lbessard@66: lbessard@65: controler = MiniTextControler(self._getIECrawcodepath()) laurent@630: IEC_raw_code_viewer = TextViewer(self.AppFrame.TabsOpened, "", None, controler, instancepath="IEC raw code") laurent@630: #IEC_raw_code_viewer.Enable(False) laurent@630: IEC_raw_code_viewer.SetTextSyntax("ALL") laurent@630: IEC_raw_code_viewer.SetKeywords(IEC_KEYWORDS) laurent@630: IEC_raw_code_viewer.RefreshView() laurent@630: IEC_raw_code_viewer.SetIcon(self.AppFrame.GenerateBitmap("ST")) etisserant@20: laurent@630: self.AppFrame.EditProjectElement(IEC_raw_code_viewer, "IEC raw code") etisserant@20: etisserant@203: def _Clean(self): greg@108: if os.path.isdir(os.path.join(self._getBuildPath())): laurent@361: self.logger.write(_("Cleaning the build directory\n")) greg@108: shutil.rmtree(os.path.join(self._getBuildPath())) greg@108: else: laurent@361: self.logger.write_error(_("Build directory already clean\n")) etisserant@203: self.ShowMethod("_showIECcode", False) etisserant@110: self.EnableMethod("_Clean", False) etisserant@286: # kill the builder etisserant@286: self._builder = None etisserant@203: self.CompareLocalAndRemotePLC() etisserant@203: etisserant@203: ############# Real PLC object access ############# etisserant@203: def UpdateMethodsFromPLCStatus(self): etisserant@203: # Get PLC state : Running or Stopped etisserant@203: # TODO : use explicit status instead of boolean laurent@486: status = None etisserant@203: if self._connector is not None: etisserant@203: status = self._connector.GetPLCstatus() laurent@486: if status is None: laurent@516: self._connector = None etisserant@203: status = "Disconnected" ed@446: if(self.previous_plcstate != status): ed@446: for args in { ed@446: "Started" : [("_Run", False), ed@446: ("_Stop", True)], ed@446: "Stopped" : [("_Run", True), ed@446: ("_Stop", False)], ed@446: "Empty" : [("_Run", False), ed@446: ("_Stop", False)], ed@446: "Broken" : [], ed@446: "Disconnected" :[("_Run", False), ed@446: ("_Stop", False), ed@446: ("_Transfer", False), ed@446: ("_Connect", True), ed@446: ("_Disconnect", False)], ed@446: }.get(status,[]): ed@446: self.ShowMethod(*args) ed@446: self.previous_plcstate = status ed@446: return True ed@446: return False ed@446: ed@446: def PullPLCStatusProc(self, event): greg@355: if self._connector is None: greg@355: self.StatusTimer.Stop() ed@446: if self.UpdateMethodsFromPLCStatus(): laurent@486: ed@446: status = _(self.previous_plcstate) ed@446: {"Broken": self.logger.write_error, ed@446: None: lambda x: None}.get( ed@446: self.previous_plcstate, self.logger.write)(_("PLC is %s\n")%status) greg@350: self.AppFrame.RefreshAll() greg@355: etisserant@239: def RegisterDebugVarToConnector(self): etisserant@239: self.DebugTimer=None etisserant@239: Idxs = [] etisserant@239: self.TracedIECPath = [] etisserant@239: if self._connector is not None: etisserant@239: self.IECdebug_lock.acquire() etisserant@239: IECPathsToPop = [] etisserant@239: for IECPath,data_tuple in self.IECdebug_datas.iteritems(): laurent@474: WeakCallableDict, data_log, status, fvalue = data_tuple etisserant@239: if len(WeakCallableDict) == 0: etisserant@239: # Callable Dict is empty. etisserant@239: # This variable is not needed anymore! etisserant@239: #print "Unused : " + IECPath etisserant@239: IECPathsToPop.append(IECPath) greg@355: elif IECPath != "__tick__": etisserant@239: # Convert edouard@450: Idx, IEC_Type = self._IECPathToIdx.get(IECPath,(None,None)) etisserant@239: if Idx is not None: Edouard@592: if IEC_Type in DebugTypesSize: Edouard@592: Idxs.append((Idx, IEC_Type, fvalue, IECPath)) Edouard@592: else: Edouard@592: self.logger.write_warning(_("Debug : Unsuppoted type to debug %s\n")%IEC_Type) etisserant@239: else: laurent@361: self.logger.write_warning(_("Debug : Unknown variable %s\n")%IECPath) etisserant@239: for IECPathToPop in IECPathsToPop: etisserant@239: self.IECdebug_datas.pop(IECPathToPop) etisserant@239: ed@457: if Idxs: ed@457: Idxs.sort() laurent@475: self.TracedIECPath = zip(*Idxs)[3] laurent@474: self._connector.SetTraceVariablesList(zip(*zip(*Idxs)[0:3])) edouard@465: else: edouard@465: self.TracedIECPath = [] edouard@465: self._connector.SetTraceVariablesList([]) etisserant@239: self.IECdebug_lock.release() lbessard@243: lbessard@243: #for IEC_path, IECdebug_data in self.IECdebug_datas.iteritems(): lbessard@243: # print IEC_path, IECdebug_data[0].keys() lbessard@243: lbessard@243: def ReArmDebugRegisterTimer(self): lbessard@243: if self.DebugTimer is not None: lbessard@243: self.DebugTimer.cancel() lbessard@243: edouard@466: # Timer to prevent rapid-fire when registering many variables edouard@466: # use wx.CallAfter use keep using same thread. TODO : use wx.Timer instead edouard@466: self.DebugTimer=Timer(0.5,wx.CallAfter,args = [self.RegisterDebugVarToConnector]) edouard@466: # Rearm anti-rapid-fire timer edouard@466: self.DebugTimer.start() lbessard@243: laurent@463: def GetDebugIECVariableType(self, IECPath): laurent@463: Idx, IEC_Type = self._IECPathToIdx.get(IECPath,(None,None)) laurent@463: return IEC_Type etisserant@239: etisserant@239: def SubscribeDebugIECVariable(self, IECPath, callableobj, *args, **kwargs): etisserant@239: """ etisserant@239: Dispatching use a dictionnary linking IEC variable paths etisserant@239: to a WeakKeyDictionary linking etisserant@239: weakly referenced callables to optionnal args etisserant@239: """ edouard@450: if IECPath != "__tick__" and not self._IECPathToIdx.has_key(IECPath): lbessard@246: return None lbessard@246: etisserant@239: self.IECdebug_lock.acquire() etisserant@239: # If no entry exist, create a new one with a fresh WeakKeyDictionary etisserant@239: IECdebug_data = self.IECdebug_datas.get(IECPath, None) etisserant@239: if IECdebug_data is None: etisserant@239: IECdebug_data = [ etisserant@239: WeakKeyDictionary(), # Callables etisserant@239: [], # Data storage [(tick, data),...] laurent@474: "Registered", # Variable status laurent@474: None] # Forced value etisserant@239: self.IECdebug_datas[IECPath] = IECdebug_data etisserant@239: etisserant@239: IECdebug_data[0][callableobj]=(args, kwargs) etisserant@239: etisserant@239: self.IECdebug_lock.release() lbessard@243: lbessard@243: self.ReArmDebugRegisterTimer() lbessard@243: etisserant@239: return IECdebug_data[1] etisserant@239: etisserant@239: def UnsubscribeDebugIECVariable(self, IECPath, callableobj): lbessard@243: #print "Unsubscribe", IECPath, callableobj lbessard@243: self.IECdebug_lock.acquire() etisserant@239: IECdebug_data = self.IECdebug_datas.get(IECPath, None) lbessard@243: if IECdebug_data is not None: etisserant@239: IECdebug_data[0].pop(callableobj,None) lbessard@243: self.IECdebug_lock.release() lbessard@243: lbessard@243: self.ReArmDebugRegisterTimer() etisserant@239: lbessard@334: def UnsubscribeAllDebugIECVariable(self): lbessard@334: self.IECdebug_lock.acquire() lbessard@334: IECdebug_data = {} lbessard@334: self.IECdebug_lock.release() lbessard@334: greg@355: self.ReArmDebugRegisterTimer() greg@355: laurent@474: def ForceDebugIECVariable(self, IECPath, fvalue): laurent@474: if not self.IECdebug_datas.has_key(IECPath): laurent@474: return laurent@474: laurent@474: self.IECdebug_lock.acquire() laurent@474: laurent@474: # If no entry exist, create a new one with a fresh WeakKeyDictionary laurent@474: IECdebug_data = self.IECdebug_datas.get(IECPath, None) laurent@474: IECdebug_data[2] = "Forced" laurent@474: IECdebug_data[3] = fvalue laurent@474: laurent@474: self.IECdebug_lock.release() laurent@474: laurent@474: self.ReArmDebugRegisterTimer() laurent@463: laurent@463: def ReleaseDebugIECVariable(self, IECPath): laurent@474: if not self.IECdebug_datas.has_key(IECPath): laurent@474: return laurent@474: laurent@474: self.IECdebug_lock.acquire() laurent@474: laurent@474: # If no entry exist, create a new one with a fresh WeakKeyDictionary laurent@474: IECdebug_data = self.IECdebug_datas.get(IECPath, None) laurent@474: IECdebug_data[2] = "Registered" laurent@474: IECdebug_data[3] = None laurent@474: laurent@474: self.IECdebug_lock.release() laurent@474: laurent@474: self.ReArmDebugRegisterTimer() laurent@474: greg@355: def CallWeakcallables(self, IECPath, function_name, *cargs): greg@355: data_tuple = self.IECdebug_datas.get(IECPath, None) greg@355: if data_tuple is not None: laurent@474: WeakCallableDict, data_log, status, fvalue = data_tuple greg@355: #data_log.append((debug_tick, value)) greg@355: for weakcallable,(args,kwargs) in WeakCallableDict.iteritems(): greg@355: #print weakcallable, value, args, kwargs greg@355: function = getattr(weakcallable, function_name, None) greg@355: if function is not None: laurent@481: if status == "Forced" and cargs[1] == fvalue: laurent@476: function(*(cargs + (True,) + args), **kwargs) laurent@474: else: laurent@474: function(*(cargs + args), **kwargs) greg@355: # This will block thread if more than one call is waiting lbessard@334: laurent@670: def GetTicktime(self): laurent@670: return self._Ticktime laurent@670: etisserant@235: def DebugThreadProc(self): etisserant@239: """ etisserant@239: This thread waid PLC debug data, and dispatch them to subscribers etisserant@239: """ etisserant@286: self.debug_break = False edouard@461: debug_getvar_retry = 0 etisserant@286: while (not self.debug_break) and (self._connector is not None): Edouard@674: Trace = self._connector.GetTraceVariables() Edouard@674: if(Trace): Edouard@674: plc_status, debug_tick, debug_vars = Trace Edouard@674: else: Edouard@674: plc_status = None edouard@578: debug_getvar_retry += 1 etisserant@239: #print debug_tick, debug_vars edouard@578: if plc_status == "Started": edouard@578: self.IECdebug_lock.acquire() ed@446: if len(debug_vars) == len(self.TracedIECPath): edouard@578: if debug_getvar_retry > DEBUG_RETRIES_WARN: edouard@578: wx.CallAfter(self.logger.write, edouard@578: _("... debugger recovered\n")) edouard@578: debug_getvar_retry = 0 edouard@673: for IECPath,value in zip(self.TracedIECPath, debug_vars): edouard@673: if value is not None: edouard@673: self.CallWeakcallables(IECPath, "NewValue", debug_tick, value) edouard@673: self.CallWeakcallables("__tick__", "NewDataAvailable") edouard@578: self.IECdebug_lock.release() edouard@578: if debug_getvar_retry == DEBUG_RETRIES_WARN: edouard@578: wx.CallAfter(self.logger.write, edouard@578: _("Waiting debugger to recover...\n")) edouard@578: if debug_getvar_retry == DEBUG_RETRIES_REREGISTER: edouard@578: # re-register debug registry to PLC edouard@578: wx.CallAfter(self.RegisterDebugVarToConnector) edouard@578: if debug_getvar_retry != 0: ed@446: # Be patient, tollerate PLC to come up before debugging edouard@460: time.sleep(0.1) edouard@578: else: edouard@578: self.debug_break = True edouard@578: wx.CallAfter(self.logger.write, _("Debugger disabled\n")) etisserant@235: etisserant@286: def KillDebugThread(self): etisserant@286: self.debug_break = True edouard@466: if self.DebugThread is not None: Edouard@624: self.logger.writeyield(_("Stopping debugger...\n")) edouard@578: self.DebugThread.join(timeout=5) edouard@466: if self.DebugThread.isAlive() and self.logger: Edouard@624: self.logger.write_warning(_("Couldn't stop debugger.\n")) edouard@578: else: Edouard@624: self.logger.write(_("Debugger stopped.\n")) etisserant@286: self.DebugThread = None etisserant@286: edouard@465: def _connect_debug(self): edouard@465: if self.AppFrame: edouard@465: self.AppFrame.ResetGraphicViewers() edouard@465: self.RegisterDebugVarToConnector() edouard@578: if self.DebugThread is None: edouard@578: self.DebugThread = Thread(target=self.DebugThreadProc) edouard@578: self.DebugThread.start() edouard@465: edouard@462: def _Run(self): etisserant@203: """ edouard@464: Start PLC etisserant@203: """ greg@350: if self.GetIECProgramsAndVariables(): edouard@462: self._connector.StartPLC() edouard@464: self.logger.write(_("Starting PLC\n")) edouard@465: self._connect_debug() etisserant@203: else: edouard@464: self.logger.write_error(_("Couldn't start PLC !\n")) etisserant@203: self.UpdateMethodsFromPLCStatus() etisserant@235: etisserant@286: etisserant@235: # def _Do_Test_Debug(self): etisserant@235: # # debug code etisserant@235: # self.temporary_non_weak_callable_refs = [] etisserant@235: # for IEC_Path, idx in self._IECPathToIdx.iteritems(): etisserant@235: # class tmpcls: etisserant@239: # def __init__(_self): greg@244: # _self.buf = None etisserant@239: # def setbuf(_self,buf): greg@244: # _self.buf = buf etisserant@239: # def SetValue(_self, value, idx, name): etisserant@239: # self.logger.write("debug call: %s %d %s\n"%(repr(value), idx, name)) etisserant@239: # #self.logger.write("debug call: %s %d %s %s\n"%(repr(value), idx, name, repr(self.buf))) etisserant@235: # a = tmpcls() etisserant@235: # res = self.SubscribeDebugIECVariable(IEC_Path, a, idx, IEC_Path) etisserant@235: # a.setbuf(res) etisserant@235: # self.temporary_non_weak_callable_refs.append(a) etisserant@203: etisserant@203: def _Stop(self): etisserant@203: """ etisserant@203: Stop PLC etisserant@203: """ edouard@483: if self._connector is not None and not self._connector.StopPLC(): edouard@483: self.logger.write_error(_("Couldn't stop PLC !\n")) edouard@483: edouard@578: self.KillDebugThread() etisserant@286: etisserant@203: self.UpdateMethodsFromPLCStatus() etisserant@203: etisserant@203: def _Connect(self): etisserant@203: # don't accept re-connetion is already connected etisserant@203: if self._connector is not None: laurent@361: self.logger.write_error(_("Already connected. Please disconnect\n")) etisserant@203: return etisserant@203: etisserant@203: # Get connector uri etisserant@203: uri = self.\ etisserant@203: BeremizRoot.\ etisserant@203: getURI_location().\ etisserant@203: strip() etisserant@203: etisserant@203: # if uri is empty launch discovery dialog etisserant@203: if uri == "": etisserant@203: # Launch Service Discovery dialog laurent@392: dialog = DiscoveryDialog(self.AppFrame) laurent@392: answer = dialog.ShowModal() laurent@392: uri = dialog.GetURI() laurent@392: dialog.Destroy() laurent@392: etisserant@203: # Nothing choosed or cancel button laurent@392: if uri is None or answer == wx.ID_CANCEL: laurent@392: self.logger.write_error(_("Connection canceled!\n")) etisserant@203: return etisserant@203: else: etisserant@203: self.\ etisserant@203: BeremizRoot.\ etisserant@203: setURI_location(uri) etisserant@203: etisserant@203: # Get connector from uri etisserant@203: try: etisserant@203: self._connector = connectors.ConnectorFactory(uri, self) etisserant@203: except Exception, msg: laurent@361: self.logger.write_error(_("Exception while connecting %s!\n")%uri) etisserant@203: self.logger.write_error(traceback.format_exc()) etisserant@203: etisserant@203: # Did connection success ? etisserant@203: if self._connector is None: etisserant@203: # Oups. laurent@361: self.logger.write_error(_("Connection failed to %s!\n")%uri) etisserant@203: else: etisserant@203: self.ShowMethod("_Connect", False) etisserant@203: self.ShowMethod("_Disconnect", True) etisserant@203: self.ShowMethod("_Transfer", True) etisserant@203: etisserant@203: self.CompareLocalAndRemotePLC() greg@350: greg@350: # Init with actual PLC status and print it ed@446: self.UpdateMethodsFromPLCStatus() laurent@361: if self.previous_plcstate is not None: laurent@361: status = _(self.previous_plcstate) laurent@361: else: laurent@361: status = "" laurent@361: self.logger.write(_("PLC is %s\n")%status) greg@350: greg@350: # Start the status Timer greg@350: self.StatusTimer.Start(milliseconds=500, oneShot=False) edouard@465: edouard@465: if self.previous_plcstate=="Started": edouard@465: if self.DebugAvailable() and self.GetIECProgramsAndVariables(): edouard@465: self.logger.write(_("Debug connect matching running PLC\n")) edouard@465: self._connect_debug() edouard@465: else: edouard@465: self.logger.write_warning(_("Debug do not match PLC - stop/transfert/start to re-enable\n")) etisserant@203: etisserant@203: def CompareLocalAndRemotePLC(self): etisserant@203: if self._connector is None: etisserant@203: return etisserant@203: # We are now connected. Update button status etisserant@203: MD5 = self.GetLastBuildMD5() etisserant@203: # Check remote target PLC correspondance to that md5 etisserant@203: if MD5 is not None: etisserant@203: if not self._connector.MatchMD5(MD5): Lolitech@544: # self.logger.write_warning( Lolitech@544: # _("Latest build does not match with target, please transfer.\n")) etisserant@203: self.EnableMethod("_Transfer", True) etisserant@203: else: Lolitech@544: # self.logger.write( Lolitech@544: # _("Latest build matches target, no transfer needed.\n")) etisserant@203: self.EnableMethod("_Transfer", True) edouard@465: # warns controller that program match edouard@465: self.ProgramTransferred() etisserant@203: #self.EnableMethod("_Transfer", False) etisserant@203: else: Lolitech@544: # self.logger.write_warning( Lolitech@544: # _("Cannot compare latest build to target. Please build.\n")) etisserant@203: self.EnableMethod("_Transfer", False) etisserant@203: etisserant@203: etisserant@203: def _Disconnect(self): etisserant@203: self._connector = None greg@350: self.StatusTimer.Stop() etisserant@203: self.UpdateMethodsFromPLCStatus() etisserant@203: etisserant@203: def _Transfer(self): etisserant@203: # Get the last build PLC's etisserant@203: MD5 = self.GetLastBuildMD5() etisserant@203: etisserant@203: # Check if md5 file is empty : ask user to build PLC etisserant@203: if MD5 is None : laurent@361: self.logger.write_error(_("Failed : Must build before transfer.\n")) etisserant@203: return False etisserant@203: etisserant@203: # Compare PLC project with PLC on target etisserant@203: if self._connector.MatchMD5(MD5): etisserant@203: self.logger.write( laurent@415: _("Latest build already matches current target. Transfering anyway...\n")) etisserant@203: etisserant@203: # Get temprary directory path etisserant@203: extrafilespath = self._getExtraFilesPath() etisserant@203: extrafiles = [(name, open(os.path.join(extrafilespath, name), etisserant@203: 'rb').read()) \ etisserant@203: for name in os.listdir(extrafilespath) \ etisserant@203: if not name=="CVS"] etisserant@203: etisserant@203: # Send PLC on target etisserant@203: builder = self.GetBuilder() etisserant@203: if builder is not None: etisserant@203: data = builder.GetBinaryCode() etisserant@203: if data is not None : etisserant@203: if self._connector.NewPLC(MD5, data, extrafiles): laurent@395: if self.AppFrame is not None: laurent@395: self.AppFrame.CloseDebugTabs() laurent@402: self.AppFrame.RefreshInstancesTree() lbessard@334: self.UnsubscribeAllDebugIECVariable() lbessard@246: self.ProgramTransferred() laurent@361: self.logger.write(_("Transfer completed successfully.\n")) etisserant@203: else: laurent@361: self.logger.write_error(_("Transfer failed\n")) etisserant@203: else: laurent@415: self.logger.write_error(_("No PLC to transfer (did build succeed ?)\n")) laurent@415: etisserant@203: self.UpdateMethodsFromPLCStatus() etisserant@105: lbessard@65: PluginMethods = [ etisserant@203: {"bitmap" : opjimg("Build"), laurent@361: "name" : _("Build"), laurent@361: "tooltip" : _("Build project into build folder"), Edouard@623: "method" : "_Build"}, etisserant@203: {"bitmap" : opjimg("Clean"), laurent@361: "name" : _("Clean"), etisserant@203: "enabled" : False, laurent@361: "tooltip" : _("Clean project build folder"), etisserant@105: "method" : "_Clean"}, etisserant@203: {"bitmap" : opjimg("Run"), laurent@361: "name" : _("Run"), etisserant@203: "shown" : False, laurent@361: "tooltip" : _("Start PLC"), etisserant@105: "method" : "_Run"}, etisserant@203: {"bitmap" : opjimg("Stop"), laurent@361: "name" : _("Stop"), etisserant@203: "shown" : False, laurent@361: "tooltip" : _("Stop Running PLC"), etisserant@105: "method" : "_Stop"}, etisserant@203: {"bitmap" : opjimg("Connect"), laurent@361: "name" : _("Connect"), laurent@361: "tooltip" : _("Connect to the target PLC"), etisserant@203: "method" : "_Connect"}, etisserant@203: {"bitmap" : opjimg("Transfer"), laurent@361: "name" : _("Transfer"), etisserant@203: "shown" : False, laurent@361: "tooltip" : _("Transfer PLC"), etisserant@203: "method" : "_Transfer"}, etisserant@203: {"bitmap" : opjimg("Disconnect"), laurent@361: "name" : _("Disconnect"), etisserant@203: "shown" : False, laurent@361: "tooltip" : _("Disconnect from PLC"), etisserant@203: "method" : "_Disconnect"}, etisserant@203: {"bitmap" : opjimg("ShowIECcode"), laurent@361: "name" : _("Show code"), etisserant@203: "shown" : False, laurent@361: "tooltip" : _("Show IEC code generated by PLCGenerator"), etisserant@105: "method" : "_showIECcode"}, etisserant@203: {"bitmap" : opjimg("editIECrawcode"), laurent@361: "name" : _("Raw IEC code"), laurent@361: "tooltip" : _("Edit raw IEC code added to code generated by PLCGenerator"), etisserant@203: "method" : "_editIECrawcode"}, lbessard@65: ]