etisserant@14: """ etisserant@14: Base definitions for beremiz plugins etisserant@14: """ etisserant@14: etisserant@20: import os,sys 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] etisserant@20: sys.path.append(os.path.join(base_folder, "plcopeneditor")) etisserant@20: etisserant@14: from xmlclass import GenerateClassesFromXSDstring etisserant@110: from wxPopen import ProcessLogger etisserant@14: 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: lbessard@74: def GetEditedElementText(self, tagname): 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: lbessard@74: def GetEditedElementInterfaceVars(self, tagname): lbessard@74: return [] lbessard@74: lbessard@74: def GetEditedElementType(self, tagname): lbessard@74: return "program" lbessard@74: lbessard@74: def GetBlockTypes(self, tagname = ""): lbessard@74: return [] lbessard@74: lbessard@74: def GetEnumeratedDataValues(self): 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@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 = [] etisserant@14: etisserant@14: def _AddParamsMembers(self): lbessard@19: self.PlugParams = None etisserant@29: if self.XSD: lbessard@86: Classes = GenerateClassesFromXSDstring(self.XSD) etisserant@29: Classes = [(name, XSDclass) for name, XSDclass in 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 = {} etisserant@106: # copy PluginMethods so that it can be later customized etisserant@106: self.PluginMethods = [dic.copy() for dic in self.PluginMethods] lbessard@17: 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: etisserant@14: def PlugPath(self,PlugName=None): etisserant@14: if not PlugName: etisserant@14: PlugName = self.BaseParams.getName() etisserant@14: return os.path.join(self.PlugParent.PlugPath(), PlugName + NameTypeSeparator + self.PlugType) etisserant@14: etisserant@14: def PlugTestModified(self): 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@29: def SetParamsAttribute(self, path, value, logger): etisserant@29: # Filter IEC_Channel and Name, that have specific behavior etisserant@29: if path == "BaseParams.IEC_Channel": etisserant@33: return self.FindNewIEC_Channel(value,logger), True etisserant@29: elif path == "BaseParams.Name": etisserant@29: res = self.FindNewName(value,logger) etisserant@29: self.PlugRequestSave() lbessard@97: return res, False 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: etisserant@14: def PlugRequestSave(self): etisserant@14: # If plugin do not have corresponding directory lbessard@17: plugpath = self.PlugPath() lbessard@17: if not os.path.isdir(plugpath): etisserant@14: # Create it lbessard@17: os.mkdir(plugpath) lbessard@17: lbessard@17: # generate XML for base XML parameters controller of the plugin etisserant@20: if self.MandatoryParams: etisserant@20: BaseXMLFile = open(self.PluginBaseXmlFilePath(),'w') lbessard@17: BaseXMLFile.write("\n") lbessard@17: BaseXMLFile.write(self.MandatoryParams[1].generateXMLText(self.MandatoryParams[0], 0)) lbessard@17: BaseXMLFile.close() lbessard@17: lbessard@17: # generate XML for XML parameters controller of the plugin etisserant@20: if self.PlugParams: etisserant@20: XMLFile = open(self.PluginXmlFilePath(),'w') lbessard@17: XMLFile.write("\n") lbessard@17: XMLFile.write(self.PlugParams[1].generateXMLText(self.PlugParams[0], 0)) lbessard@17: XMLFile.close() etisserant@14: etisserant@14: # Call the plugin specific OnPlugSave method lbessard@17: result = self.OnPlugSave() lbessard@17: if not result: lbessard@17: return "Error while saving \"%s\""%self.PlugPath() etisserant@14: etisserant@14: # go through all childs and do the same etisserant@14: for PlugChild in self.IterChilds(): lbessard@17: result = PlugChild.PlugRequestSave() lbessard@17: if result: lbessard@17: 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@24: def PlugGenerate_C(self, buildpath, locations, logger): 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: """ etisserant@115: logger.write_warning(".".join(map(lambda x:str(x), self.GetCurrentLocation())) + " -> Nothing to do\n") etisserant@51: return [],"",False etisserant@14: etisserant@24: def _Generate_C(self, buildpath, locations, logger): etisserant@14: # Generate plugins [(Cfiles, CFLAGS)], LDFLAGS etisserant@51: PlugCFilesAndCFLAGS, PlugLDFLAGS, DoCalls = self.PlugGenerate_C(buildpath, locations, logger) etisserant@47: # if some files heve 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@47: _LocationCFilesAndCFLAGS, _LDFLAGS = \ 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@24: [loc for loc in locations if loc["LOC"][0:depth] == new_location ], etisserant@18: #propagete logger etisserant@18: logger) etisserant@14: # stack the result etisserant@47: LocationCFilesAndCFLAGS += _LocationCFilesAndCFLAGS etisserant@47: LDFLAGS += _LDFLAGS etisserant@47: etisserant@47: return LocationCFilesAndCFLAGS,LDFLAGS etisserant@14: etisserant@14: def BlockTypesFactory(self): etisserant@14: return [] etisserant@14: etisserant@14: def STLibraryFactory(self): etisserant@14: return "" etisserant@14: etisserant@14: def IterChilds(self): etisserant@14: for PlugType, PluggedChilds in self.PluggedChilds.items(): etisserant@14: for PlugInstance in PluggedChilds: etisserant@14: 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: 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@29: def FindNewName(self, DesiredName, logger): 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: etisserant@29: logger.write_warning("A child names \"%s\" already exist -> \"%s\"\n"%(DesiredName,res)) etisserant@29: return res etisserant@29: etisserant@29: def FindNewIEC_Channel(self, DesiredChannel, logger): 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 : etisserant@33: if logger : etisserant@33: 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@29: if logger and DesiredChannel != res: etisserant@29: logger.write_warning("A child with IEC channel %d already exist -> %d\n"%(DesiredChannel,res)) etisserant@14: return res etisserant@14: etisserant@14: def OnPlugClose(self): 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: etisserant@24: def PlugAddChild(self, PlugName, PlugType, logger): 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: etisserant@14: 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: lbessard@17: 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@29: NewPlugName = _self.FindNewName(PlugName, logger) 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@106: _self.LoadXMLParams(logger, NewPlugName) etisserant@20: # Basic check. Better to fail immediately. etisserant@29: if (_self.BaseParams.getName() != NewPlugName): etisserant@29: 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@29: _self.FindNewIEC_Channel(_self.BaseParams.getIEC_Channel(),logger) 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@24: _self.LoadChilds(logger) etisserant@14: else: etisserant@14: # If plugin do not have corresponding file/dirs - they will be created on Save lbessard@17: os.mkdir(_self.PlugPath()) etisserant@14: # Find an IEC number etisserant@29: _self.FindNewIEC_Channel(0, None) etisserant@14: # Call the plugin real __init__ lbessard@17: if getattr(PlugClass, "__init__", None): lbessard@17: PlugClass.__init__(_self) lbessard@17: _self.PlugRequestSave() lbessard@77: 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 etisserant@14: etisserant@14: etisserant@106: def LoadXMLParams(self, logger, PlugName = None): etisserant@105: methode_name = os.path.join(self.PlugPath(PlugName), "methods.py") etisserant@105: if os.path.isfile(methode_name): etisserant@106: logger.write("Info: %s plugin as some special methods in methods.py\n" % (PlugName or "Root")) etisserant@105: execfile(methode_name) etisserant@105: lbessard@17: # Get the base xml tree etisserant@20: if self.MandatoryParams: etisserant@106: #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@106: #except Exception, e: etisserant@106: # logger.write_error("Couldn't load plugin base parameters %s :\n %s" % (PlugName, str(e))) etisserant@106: lbessard@17: etisserant@14: # Get the xml tree etisserant@20: if self.PlugParams: etisserant@106: #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@106: #except Exception, e: etisserant@106: # logger.write_error("Couldn't load plugin parameters %s :\n %s" % (PlugName, str(e))) lbessard@17: etisserant@24: def LoadChilds(self, logger): 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) lbessard@86: #try: lbessard@86: self.PlugAddChild(pname, ptype, logger) lbessard@86: #except Exception, e: lbessard@86: # logger.write_error("Could not add child \"%s\", type %s :\n%s\n"%(pname, ptype, str(e))) 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: 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@75: iec2c_path = os.path.join(base_folder, "matiec", "iec2c"+exe_ext) etisserant@20: ieclib_path = os.path.join(base_folder, "matiec", "lib") etisserant@20: etisserant@20: # import for project creation timestamping etisserant@20: from time import localtime etisserant@20: from datetime import datetime etisserant@20: # import necessary stuff from PLCOpenEditor etisserant@20: from PLCControler import PLCControler etisserant@20: from PLCOpenEditor import PLCOpenEditor, ProjectDialog etisserant@20: from TextViewer import TextViewer lbessard@41: from plcopen.structures import IEC_KEYWORDS, AddPluginBlockList, ClearPluginTypes, PluginTypes etisserant@49: import runtime etisserant@22: import re etisserant@20: 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: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: etisserant@106: etisserant@106: etisserant@106: etisserant@106: etisserant@106: etisserant@106: etisserant@106: etisserant@106: etisserant@106: etisserant@106: etisserant@106: etisserant@106: lbessard@86: lbessard@86: lbessard@86: lbessard@86: lbessard@86: etisserant@13: etisserant@13: etisserant@13: etisserant@13: """ etisserant@13: etisserant@20: def __init__(self, frame): lbessard@41: PLCControler.__init__(self) lbessard@41: etisserant@20: self.MandatoryParams = None etisserant@20: self.AppFrame = frame etisserant@20: etisserant@20: """ etisserant@20: This method are not called here... but in NewProject and OpenProject etisserant@20: self._AddParamsMembers() etisserant@20: self.PluggedChilds = {} etisserant@20: """ etisserant@20: 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" lbessard@17: etisserant@20: # After __init__ root plugin is not valid etisserant@20: self.ProjectPath = None etisserant@20: self.PLCEditor = None etisserant@106: etisserant@106: # copy PluginMethods so that it can be later customized etisserant@106: self.PluginMethods = [dic.copy() for dic in self.PluginMethods] etisserant@110: etisserant@110: self.runningPLC = None etisserant@110: lbessard@17: lbessard@17: def HasProjectOpened(self): lbessard@17: """ lbessard@17: Return if a project is actually opened lbessard@17: """ etisserant@20: return self.ProjectPath != None etisserant@23: etisserant@23: def GetPlugRoot(self): etisserant@23: return self etisserant@23: 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} etisserant@20: etisserant@20: def NewProject(self, ProjectPath): 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: """ lbessard@17: # Verify that choosen folder is empty lbessard@17: if not os.path.isdir(ProjectPath) or len(os.listdir(ProjectPath)) > 0: lbessard@17: return "Folder choosen 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() etisserant@20: 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 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: etisserant@24: def LoadProject(self, ProjectPath, logger): lbessard@17: """ lbessard@17: Load a project contained in a folder lbessard@17: @param ProjectPath: path of the project folder lbessard@17: """ lbessard@17: # 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): lbessard@17: return "Folder choosen 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 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@106: result = self.LoadXMLParams(logger) lbessard@17: if result: lbessard@17: return result etisserant@13: #Load and init all the childs etisserant@24: self.LoadChilds(logger) lbessard@41: self.RefreshPluginsBlockLists() lbessard@17: return None lbessard@17: lbessard@17: def SaveProject(self): lbessard@41: if not self.SaveXMLFile(): lbessard@41: self.SaveXMLFile(os.path.join(self.ProjectPath, 'plc.xml')) lbessard@25: if self.PLCEditor: lbessard@25: self.PLCEditor.RefreshTitle() lbessard@17: self.PlugRequestSave() 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@62: ClearPluginTypes() lbessard@62: AddPluginBlockList(self.BlockTypesFactory()) lbessard@62: for child in self.IterChilds(): lbessard@62: AddPluginBlockList(child.BlockTypesFactory()) lbessard@62: if self.PLCEditor is not None: lbessard@62: self.PLCEditor.RefreshEditor() lbessard@41: 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: etisserant@24: def PlugGenerate_C(self, buildpath, locations, logger): etisserant@18: """ etisserant@18: Generate C code etisserant@18: @param locations: List of complete variables locations \ etisserant@18: [(IEC_loc, IEC_Direction, IEC_Type, Name)]\ etisserant@18: ex: [((0,0,4,5),'I','STRING','__IX_0_0_4_5'),...] etisserant@18: @return: [(C_file_name, CFLAGS),...] , LDFLAGS_TO_APPEND etisserant@18: """ etisserant@51: return [(C_file_name, self.CFLAGS) for C_file_name in self.PLCGeneratedCFiles ] , "", False etisserant@20: etisserant@20: def _getBuildPath(self): etisserant@20: return os.path.join(self.ProjectPath, "build") etisserant@20: 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 lbessard@65: return os.path.join(self._getBuildPath(), "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@97: LOCATED_MODEL = re.compile("__LOCATED_VAR\((?P[A-Z]*),(?P[_A-Za-z0-9]*),(?P[QMI])(?:,(?P[XBWD]))?,(?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@20: def _Generate_SoftPLC(self, logger): 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: @param logger: the log pseudo file etisserant@20: """ etisserant@20: lbessard@41: # Update PLCOpenEditor Plugin Block types before generate ST code lbessard@41: self.RefreshPluginsBlockLists() lbessard@41: etisserant@20: 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@65: result = self.GenerateProgram(self._getIECgeneratedcodepath()) etisserant@113: if result is not None: etisserant@20: # Failed ! etisserant@113: logger.write_error("Error in ST/IL/SFC code generator :\n%s\n"%result) etisserant@20: return False lbessard@65: plc_file = open(self._getIECcodepath(), "w") 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@65: plc_file.write(open(self._getIECgeneratedcodepath(), "r").read()) lbessard@65: plc_file.close() etisserant@47: logger.write("Compiling IEC Program in to C code...\n") 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@110: logger, etisserant@110: "%s \"%s\" -I \"%s\" \"%s\""%( etisserant@110: iec2c_path, etisserant@110: self._getIECcodepath(), etisserant@110: ieclib_path, buildpath), etisserant@110: no_stdout=True).spin() etisserant@20: if status: etisserant@20: # Failed ! etisserant@22: logger.write_error("Error : IEC to C compiler returned %d\n"%status) etisserant@20: return False 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: etisserant@115: logger.write_error("Error : At least one configuration and one ressource 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) etisserant@20: 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 etisserant@96: self.CFLAGS = "\"-I"+ieclib_path+"\"" etisserant@18: return True etisserant@18: etisserant@18: def _build(self, logger): etisserant@20: """ etisserant@20: Method called by user to (re)build SoftPLC and plugin tree etisserant@20: """ 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@18: etisserant@24: logger.flush() etisserant@22: logger.write("Start build in %s\n" % buildpath) etisserant@110: etisserant@110: self.EnableMethod("_Clean", True) etisserant@110: self.EnableMethod("_showIECcode", True) etisserant@18: etisserant@20: # Generate SoftPLC code etisserant@20: if not self._Generate_SoftPLC(logger): etisserant@22: logger.write_error("SoftPLC code generation failed !\n") etisserant@20: return False etisserant@20: etisserant@110: etisserant@47: #logger.write("SoftPLC code generation successfull\n") etisserant@47: etisserant@47: logger.write("Generating plugins code ...\n") etisserant@18: etisserant@20: # Generate C code and compilation params from plugin hierarchy etisserant@24: try: etisserant@47: LocationCFilesAndCFLAGS,LDFLAGS = self._Generate_C( etisserant@24: buildpath, etisserant@24: self.PLCGeneratedLocatedVars, etisserant@24: logger) etisserant@24: except Exception, msg: etisserant@24: logger.write_error("Plugins code generation Failed !\n") etisserant@24: logger.write_error(str(msg)) etisserant@24: return False etisserant@18: etisserant@47: etisserant@47: #debug etisserant@47: #import pprint etisserant@47: #pp = pprint.PrettyPrinter(indent=4) etisserant@47: #logger.write("LocationCFilesAndCFLAGS :\n"+pp.pformat(LocationCFilesAndCFLAGS)+"\n") etisserant@47: #logger.write("LDFLAGS :\n"+pp.pformat(LDFLAGS)+"\n") etisserant@47: etisserant@49: # Generate main etisserant@51: locstrs = map(lambda x:"_".join(map(str,x)), [loc for loc,Cfiles,DoCalls in LocationCFilesAndCFLAGS if loc and DoCalls]) etisserant@49: plc_main = runtime.code("plc_common_main") % { etisserant@57: "calls_prototypes":"\n".join( etisserant@57: ["int __init_%(s)s(int argc,char **argv);\nvoid __cleanup_%(s)s();\nvoid __retrive_%(s)s();\nvoid __publish_%(s)s();"% etisserant@51: {'s':locstr} for locstr in locstrs]), etisserant@57: "retrive_calls":" \n".join(["__retrive_%(s)s();"%{'s':locstr} for locstr in locstrs]), etisserant@57: "publish_calls":" \n".join(["__publish_%(s)s();"%{'s':locstr} for locstr in locstrs]), etisserant@57: "init_calls":" \n".join(["init_level++; if(res = __init_%(s)s(argc,argv)) return res;"%{'s':locstr} for locstr in locstrs]), etisserant@57: "cleanup_calls":" \n".join(["if(init_level-- > 0) __cleanup_%(s)s();"%{'s':locstr} for locstr in locstrs])} etisserant@49: target_name = self.BeremizRoot.TargetType.content["name"] etisserant@49: plc_main += runtime.code("plc_%s_main"%target_name) etisserant@49: etisserant@49: main_path = os.path.join(buildpath, "main.c" ) etisserant@49: f = open(main_path,'w') etisserant@49: f.write(plc_main) etisserant@49: f.close() etisserant@49: # First element is necessarely root etisserant@49: LocationCFilesAndCFLAGS[0][1].insert(0,(main_path, self.CFLAGS)) etisserant@49: etisserant@20: # Compile the resulting code into object files. etisserant@47: compiler = self.BeremizRoot.getCompiler() etisserant@75: _CFLAGS = self.BeremizRoot.getCFLAGS() etisserant@75: linker = self.BeremizRoot.getLinker() etisserant@75: _LDFLAGS = self.BeremizRoot.getLDFLAGS() etisserant@51: obns = [] etisserant@51: objs = [] etisserant@51: for Location, CFilesAndCFLAGS, DoCalls in LocationCFilesAndCFLAGS: etisserant@47: if Location: etisserant@47: logger.write("Plugin : " + self.GetChildByIECLocation(Location).GetCurrentName() + " " + str(Location)+"\n") etisserant@47: else: etisserant@47: logger.write("PLC :\n") etisserant@47: etisserant@47: for CFile, CFLAGS in CFilesAndCFLAGS: etisserant@47: bn = os.path.basename(CFile) etisserant@51: obn = os.path.splitext(bn)[0]+".o" etisserant@51: obns.append(obn) etisserant@51: logger.write(" [CC] "+bn+" -> "+obn+"\n") etisserant@49: objectfilename = os.path.splitext(CFile)[0]+".o" etisserant@110: etisserant@110: status, result, err_result = ProcessLogger( etisserant@110: logger, etisserant@110: "\"%s\" -c \"%s\" -o \"%s\" %s %s"% etisserant@110: (compiler, CFile, objectfilename, _CFLAGS, CFLAGS) etisserant@110: ).spin() etisserant@110: etisserant@49: if status != 0: etisserant@49: logger.write_error("Build failed\n") etisserant@49: return False etisserant@51: objs.append(objectfilename) etisserant@51: # Link all the object files into one executable etisserant@51: logger.write("Linking :\n") etisserant@51: exe = self.GetProjectName() etisserant@51: if target_name == "Win32": etisserant@51: exe += ".exe" etisserant@51: exe_path = os.path.join(buildpath, exe) etisserant@51: logger.write(" [CC] " + ' '.join(obns)+" -> " + exe + "\n") etisserant@110: status, result, err_result = ProcessLogger( etisserant@110: logger, etisserant@110: "\"%s\" \"%s\" -o \"%s\" %s"% etisserant@110: (linker, etisserant@110: '" "'.join(objs), etisserant@110: exe_path, etisserant@110: ' '.join(LDFLAGS+[_LDFLAGS])) etisserant@110: ).spin() etisserant@51: if status != 0: etisserant@51: logger.write_error("Build failed\n") etisserant@109: self.EnableMethod("_Run", False) etisserant@51: return False etisserant@51: etisserant@109: self.EnableMethod("_Run", True) etisserant@49: return True etisserant@47: etisserant@20: etisserant@20: def _showIECcode(self, logger): etisserant@20: plc_file = self._getIECcodepath() etisserant@20: new_dialog = wx.Frame(None) lbessard@74: ST_viewer = TextViewer(new_dialog, "", None, None) etisserant@20: #ST_viewer.Enable(False) etisserant@20: ST_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 ! *)' lbessard@65: ST_viewer.SetText(text = text) lbessard@65: lbessard@65: new_dialog.Show() lbessard@65: lbessard@65: def _editIECrawcode(self, logger): lbessard@65: new_dialog = wx.Frame(None) lbessard@66: lbessard@66: buildpath = self._getBuildPath() lbessard@66: # Eventually create build dir lbessard@66: if not os.path.exists(buildpath): lbessard@66: os.mkdir(buildpath) lbessard@66: lbessard@65: controler = MiniTextControler(self._getIECrawcodepath()) lbessard@74: ST_viewer = TextViewer(new_dialog, "", None, controler) lbessard@65: #ST_viewer.Enable(False) lbessard@65: ST_viewer.SetKeywords(IEC_KEYWORDS) lbessard@65: ST_viewer.RefreshView() etisserant@20: etisserant@20: new_dialog.Show() etisserant@20: etisserant@20: def _EditPLC(self, logger): lbessard@62: if self.PLCEditor is None: lbessard@41: self.RefreshPluginsBlockLists() lbessard@25: def _onclose(): lbessard@25: self.PLCEditor = None lbessard@25: def _onsave(): lbessard@25: self.SaveProject() lbessard@41: self.PLCEditor = PLCOpenEditor(self.AppFrame, self) etisserant@20: self.PLCEditor.RefreshProjectTree() etisserant@20: self.PLCEditor.RefreshFileMenu() etisserant@20: self.PLCEditor.RefreshEditMenu() etisserant@20: self.PLCEditor.RefreshToolBar() lbessard@25: self.PLCEditor._onclose = _onclose lbessard@25: self.PLCEditor._onsave = _onsave etisserant@20: self.PLCEditor.Show() etisserant@20: etisserant@22: def _Clean(self, logger): greg@108: if os.path.isdir(os.path.join(self._getBuildPath())): greg@108: logger.write("Cleaning the build directory\n") greg@108: shutil.rmtree(os.path.join(self._getBuildPath())) greg@108: else: greg@108: logger.write_error("Build directory already clean\n") etisserant@110: self.EnableMethod("_showIECcode", False) etisserant@110: self.EnableMethod("_Clean", False) etisserant@110: self.EnableMethod("_Run", False) etisserant@22: etisserant@22: def _Run(self, logger): greg@107: command_start_plc = os.path.join(self._getBuildPath(),self.GetProjectName() + exe_ext) greg@107: if os.path.isfile(command_start_plc): etisserant@110: logger.write("Starting PLC\n") etisserant@110: def this_plc_finish_callback(*args): etisserant@110: if self.runningPLC is not None: etisserant@110: self.runningPLC = None etisserant@113: self.reset_finished() etisserant@110: self.runningPLC = ProcessLogger( etisserant@110: logger, etisserant@110: command_start_plc, etisserant@110: finish_callback = this_plc_finish_callback) etisserant@110: self.EnableMethod("_Clean", False) etisserant@110: self.EnableMethod("_Run", False) etisserant@110: self.EnableMethod("_Stop", True) etisserant@110: self.EnableMethod("_build", False) greg@107: else: greg@108: logger.write_error("%s doesn't exist\n" %command_start_plc) etisserant@22: etisserant@110: def reset_finished(self): etisserant@110: self.EnableMethod("_Clean", True) etisserant@110: self.EnableMethod("_Run", True) etisserant@110: self.EnableMethod("_Stop", False) etisserant@110: self.EnableMethod("_build", True) etisserant@110: etisserant@105: def _Stop(self, logger): etisserant@110: if self.runningPLC is not None: greg@107: logger.write("Stopping PLC\n") etisserant@110: was_runningPLC = self.runningPLC etisserant@110: self.runningPLC = None etisserant@110: was_runningPLC.kill() etisserant@110: self.reset_finished() etisserant@105: lbessard@65: PluginMethods = [ lbessard@82: {"bitmap" : os.path.join("images", "editPLC"), lbessard@65: "name" : "Edit PLC", lbessard@65: "tooltip" : "Edit PLC program with PLCOpenEditor", etisserant@105: "method" : "_EditPLC"}, lbessard@82: {"bitmap" : os.path.join("images", "Build"), lbessard@65: "name" : "Build", lbessard@65: "tooltip" : "Build project into build folder", etisserant@105: "method" : "_build"}, lbessard@82: {"bitmap" : os.path.join("images", "Clean"), lbessard@65: "name" : "Clean", lbessard@65: "tooltip" : "Clean project build folder", etisserant@105: "method" : "_Clean"}, lbessard@82: {"bitmap" : os.path.join("images", "Run"), lbessard@65: "name" : "Run", etisserant@109: "enabled" : False, lbessard@65: "tooltip" : "Run PLC from build folder", etisserant@105: "method" : "_Run"}, etisserant@105: {"bitmap" : os.path.join("images", "Stop"), etisserant@105: "name" : "Stop", etisserant@109: "enabled" : False, etisserant@105: "tooltip" : "Stop Running PLC", etisserant@105: "method" : "_Stop"}, lbessard@82: {"bitmap" : os.path.join("images", "ShowIECcode"), lbessard@65: "name" : "Show IEC code", etisserant@110: "enabled" : False, lbessard@65: "tooltip" : "Show IEC code generated by PLCGenerator", etisserant@105: "method" : "_showIECcode"}, lbessard@74: {"name" : "Edit raw IEC code", lbessard@74: "tooltip" : "Edit raw IEC code added to code generated by PLCGenerator", etisserant@105: "method" : "_editIECrawcode"} lbessard@65: ]