etisserant@14: """
etisserant@14: Base definitions for beremiz plugins
etisserant@14: """
etisserant@14:
etisserant@13: import os
lbessard@17: import plugins
etisserant@14: import types
etisserant@14: import shutil
etisserant@14: from xml.dom import minidom
etisserant@14: from xmlclass import GenerateClassesFromXSDstring
etisserant@14:
lbessard@17: from PLCControler import PLCControler
lbessard@17:
etisserant@14: _BaseParamsClass = GenerateClassesFromXSDstring("""
etisserant@14:
etisserant@14:
etisserant@14:
etisserant@14:
lbessard@17:
etisserant@14:
etisserant@14:
etisserant@14:
etisserant@14: """)[0]["BaseParams"]
etisserant@14:
etisserant@14: NameTypeSeparator = '@'
etisserant@14:
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):
etisserant@14: Classes = GenerateClassesFromXSDstring(self.XSD)[0]
lbessard@19: self.PlugParams = None
lbessard@17: Classes = [(name, XSDclass) for name, XSDclass in Classes.items() if XSDclass.IsBaseClass]
lbessard@17: if len(Classes) == 1:
lbessard@17: name, XSDclass = Classes[0]
lbessard@17: obj = XSDclass()
lbessard@17: self.PlugParams = (name, obj)
lbessard@17: 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 = {}
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@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@19: if 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:
lbessard@19: def SetParamsAttribute(self, path, value):
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)
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
lbessard@17: basexmlfilepath = self.PluginBaseXmlFilePath()
lbessard@17: if basexmlfilepath:
lbessard@17: BaseXMLFile = open(basexmlfilepath,'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
lbessard@17: xmlfilepath = self.PluginXmlFilePath()
lbessard@17: if xmlfilepath:
lbessard@17: XMLFile = open(xmlfilepath,'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@18: def PlugGenerate_C(self, buildpath, current_location, locations, logger):
etisserant@14: """
etisserant@14: Generate C code
etisserant@14: @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
etisserant@14: @param locations: List of complete variables locations \
lbessard@17: [(IEC_loc, IEC_Direction, IEC_Type, Name)]\
etisserant@16: 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@18: logger.write_warning(".".join(map(lambda x:str(x), current_location)) + " -> Nothing yo do")
etisserant@14: return [],""
etisserant@14:
etisserant@18: def _Generate_C(self, buildpath, current_location, locations, logger):
etisserant@14: # Generate plugins [(Cfiles, CFLAGS)], LDFLAGS
etisserant@18: PlugCFilesAndCFLAGS, PlugLDFLAGS = self.PlugGenerate_C(buildpath, current_location, locations, logger)
etisserant@14: # recurse through all childs, and stack their results
etisserant@14: for PlugChild in self.IterChilds():
etisserant@18: # Compute child's IEC location
etisserant@16: new_location = current_location + (self.BaseParams.getIEC_Channel())
etisserant@14: # Get childs [(Cfiles, CFLAGS)], LDFLAGS
etisserant@14: CFilesAndCFLAGS, LDFLAGS = \
etisserant@14: PlugChild._Generate_C(
etisserant@14: #keep the same path
etisserant@14: buildpath,
etisserant@14: # but update location (add curent IEC channel at the end)
etisserant@16: new_location,
etisserant@14: # filter locations that start with current IEC location
etisserant@18: [ (l,d,t,n) for l,d,t,n in locations if l[0:len(new_location)] == new_location ],
etisserant@18: #propagete logger
etisserant@18: logger)
etisserant@14: # stack the result
etisserant@14: PlugCFilesAndCFLAGS += CFilesAndCFLAGS
etisserant@14: PlugLDFLAGS += LDFLAGS
etisserant@14:
etisserant@14: return PlugCFilesAndCFLAGS,PlugLDFLAGS
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@14: def _GetChildBySomething(self, sep, something, matching):
etisserant@14: toks = matching.split(sep,1)
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@14: if len(toks) == 2:
etisserant@14: # Recurse in order to find the latest object
etisserant@14: return PlugInstance._GetChildBySomething( sep, 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@14: return self._GetChildBySomething('.',"Name", Name)
etisserant@14:
etisserant@14: def GetChildByIECLocation(self, Location):
etisserant@14: return self._GetChildBySomething('_',"IEC_Channel", Name)
etisserant@14:
lbessard@17: def GetPlugInfos(self):
lbessard@17: childs = []
lbessard@17: for child in self.IterChilds():
lbessard@17: childs.append(child.GetPlugInfos())
lbessard@17: return {"name" : self.BaseParams.getName(), "type" : None, "values" : childs}
lbessard@17:
etisserant@14: 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@14: 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@14: if res < 0 : 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@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@14: def PlugRemoveChild(self, PlugName):
etisserant@14: # Fetch the plugin
etisserant@14: PlugInstance = self.GetChildByName(PlugName)
etisserant@14: # Ask to his parent to remove it
etisserant@14: PlugInstance.PlugParent._doRemoveChild(PlugInstance)
etisserant@14:
etisserant@14: def PlugAddChild(self, PlugName, PlugType):
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@14: PlugChildsTypes = dict(self.PlugChildsTypes)
etisserant@14: # Check that adding this plugin is allowed
etisserant@14: try:
etisserant@14: PlugClass = 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@14: # Call the base plugin template init - change XSD into class members
etisserant@14: PlugTemplate.__init__(_self)
etisserant@14: # If dir have already be made, and file exist
etisserant@14: if os.path.isdir(_self.PlugPath(PlugName)) and os.path.isfile(_self.PluginXmlFilePath(PlugName)):
etisserant@14: #Load the plugin.xml file into parameters members
lbessard@17: _self.LoadXMLParams(PlugName)
etisserant@15: # Check that IEC_Channel is not already in use.
etisserant@15: 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@14: _self.LoadChilds()
etisserant@14: else:
etisserant@14: # If plugin do not have corresponding file/dirs - they will be created on Save
etisserant@14: # Set plugin name
etisserant@14: _self.BaseParams.setName(PlugName)
lbessard@17: os.mkdir(_self.PlugPath())
etisserant@14: # Find an IEC number
etisserant@14: _self.FindNewIEC_Channel(0)
etisserant@14: # Call the plugin real __init__
lbessard@17: if getattr(PlugClass, "__init__", None):
lbessard@17: PlugClass.__init__(_self)
lbessard@17: _self.PlugRequestSave()
etisserant@14:
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:
lbessard@17: def LoadXMLParams(self, PlugName = None, test = True):
lbessard@17: # Get the base xml tree
lbessard@17: basexmlfilepath = self.PluginBaseXmlFilePath(PlugName)
lbessard@17: if basexmlfilepath:
lbessard@17: basexmlfile = open(basexmlfilepath, 'r')
lbessard@17: basetree = minidom.parse(basexmlfile)
lbessard@17: self.MandatoryParams[1].loadXMLTree(basetree.childNodes[0])
lbessard@17: basexmlfile.close()
lbessard@17:
etisserant@14: # Get the xml tree
lbessard@17: xmlfilepath = self.PluginXmlFilePath(PlugName)
lbessard@17: if xmlfilepath:
lbessard@17: xmlfile = open(xmlfilepath, 'r')
lbessard@17: tree = minidom.parse(xmlfile)
lbessard@17: self.PlugParams[1].loadXMLTree(tree.childNodes[0])
lbessard@17: xmlfile.close()
lbessard@17:
lbessard@17: if test:
lbessard@17: # Basic check. Better to fail immediately.
lbessard@17: if not PlugName:
lbessard@17: PlugName = os.path.split(self.PlugPath())[1].split(NameTypeSeparator)[0]
lbessard@17: if (self.BaseParams.getName() != PlugName):
lbessard@17: raise Exception, "Project tree layout do not match plugin.xml %s!=%s "%(PlugName, self.BaseParams.getName())
lbessard@17: # Now, self.PlugPath() should be OK
etisserant@14:
etisserant@14: 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@14: try:
lbessard@17: self.PlugAddChild(*PlugDir.split(NameTypeSeparator))
etisserant@14: except Exception, e:
etisserant@14: print e
etisserant@13:
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@13: class PluginsRoot(PlugTemplate):
etisserant@13:
etisserant@14: # For root object, available Childs Types are modules of the plugin packages.
lbessard@17: PlugChildsTypes = [(name, _GetClassFunction(name)) for name in plugins.__all__]
etisserant@13:
etisserant@13: XSD = """
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
lbessard@17:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@14:
lbessard@17:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@14:
lbessard@17:
lbessard@17:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
lbessard@17:
lbessard@17:
lbessard@17:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
lbessard@17:
lbessard@17:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13:
etisserant@13: """
etisserant@13:
lbessard@17: def __init__(self):
lbessard@17: PlugTemplate.__init__(self)
etisserant@13: # self is the parent
etisserant@13: self.PlugParent = None
etisserant@13: # Keep track of the plugin type name
etisserant@13: self.PlugType = "Beremiz"
lbessard@17:
lbessard@17: self.ProjectPath = ""
lbessard@17: self.PLCManager = None
lbessard@17:
lbessard@17: def HasProjectOpened(self):
lbessard@17: """
lbessard@17: Return if a project is actually opened
lbessard@17: """
lbessard@17: return self.ProjectPath != ""
lbessard@17:
lbessard@17: def GetProjectPath(self):
lbessard@17: return self.ProjectPath
lbessard@17:
lbessard@17: def NewProject(self, ProjectPath, PLCParams):
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!"
lbessard@17: # Create Controler for PLCOpen program
lbessard@17: self.PLCManager = PLCControler()
lbessard@17: self.PLCManager.CreateNewProject(PLCParams.pop("projectName"))
lbessard@17: self.PLCManager.SetProjectProperties(properties = PLCParams)
etisserant@13: # Change XSD into class members
etisserant@13: self._AddParamsMembers()
etisserant@13: self.PluggedChilds = {}
etisserant@13: # No IEC channel, name, etc...
etisserant@13: self.MandatoryParams = []
lbessard@17: # Keep track of the root plugin (i.e. project path)
lbessard@17: self.ProjectPath = ProjectPath
lbessard@17: self.BaseParams.setName(os.path.split(ProjectPath)[1])
lbessard@17: return None
lbessard@17:
lbessard@17: def LoadProject(self, ProjectPath):
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: # Create Controler for PLCOpen program
lbessard@17: self.PLCManager = PLCControler()
lbessard@17: # Load PLCOpen file
lbessard@17: result = self.PLCManager.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: # No IEC channel, name, etc...
lbessard@17: self.MandatoryParams = None
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
lbessard@17: result = self.LoadXMLParams(test = False)
lbessard@17: if result:
lbessard@17: return result
etisserant@13: #Load and init all the childs
lbessard@17: self.LoadChilds()
lbessard@17: self.BaseParams.setName(os.path.split(ProjectPath)[1])
lbessard@17: return None
lbessard@17:
lbessard@17: def SaveProject(self):
lbessard@17: if not self.PLCManager.SaveXMLFile():
lbessard@17: self.PLCManager.SaveXMLFile(os.path.join(self.ProjectPath, 'plc.xml'))
lbessard@17: self.PlugRequestSave()
lbessard@17:
lbessard@17: def PlugPath(self, PlugName=None):
etisserant@13: return self.ProjectPath
lbessard@17:
lbessard@17: def PluginBaseXmlFilePath(self, PlugName=None):
lbessard@17: return None
lbessard@17:
etisserant@13: def PluginXmlFilePath(self, PlugName=None):
etisserant@13: return os.path.join(self.PlugPath(PlugName), "beremiz.xml")
etisserant@18:
etisserant@18: def PlugGenerate_C(self, buildpath, current_location, locations, logger):
etisserant@18: """
etisserant@18: Generate C code
etisserant@18: @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5)
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@18: return [(C_file_name, "") for C_file_name in self.PLCGeneratedCFiles ] , ""
etisserant@18:
etisserant@18: def _Generate_SoftPLC(self, buildpath, logger):
etisserant@18:
etisserant@18: LOCATED_MODEL = re.compile("__LOCATED_VAR\((?P[A-Z]*),(?P[_A-Za-z0-9]*),(?P[QMI])(?:,(?P[XBWD]))?,(?P[,0-9]*)\)")
etisserant@18:
etisserant@18: if self.PLCManager:
etisserant@18: logger.write("Generating SoftPLC IEC-61131 ST/IL/SFC code...\n")
etisserant@18: plc_file = os.path.join(self.TargetDir, "plc.st")
etisserant@18: result = self.PLCManager.GenerateProgram(plc_file)
etisserant@18: if not result:
etisserant@18: logger.write_error("Error : ST/IL/SFC code generator returned %d"%result)
etisserant@18: return False
etisserant@18: logger.write("Compiling ST Program in to C Program...\n")
etisserant@18: status, result, err_result = self.LogCommand("%s %s -I %s %s"%(iec2cc_path, plc_file, ieclib_path, self.TargetDir))
etisserant@18: if status:
etisserant@18: new_dialog = wx.Frame(None)
etisserant@18: ST_viewer = TextViewer(new_dialog, None, None)
etisserant@18: #ST_viewer.Enable(False)
etisserant@18: ST_viewer.SetKeywords(IEC_KEYWORDS)
etisserant@18: ST_viewer.SetText(file(plc_file).read())
etisserant@18: new_dialog.Show()
etisserant@18: raise Exception, "Error : IEC to C compiler returned %d"%status
etisserant@18: C_files = result.splitlines()
etisserant@18: C_files.remove("POUS.c")
etisserant@18: C_files = map(lambda filename:os.path.join(self.TargetDir, filename), C_files)
etisserant@18: logger.write("Extracting Located Variables...\n")
etisserant@18: location_file = open(os.path.join(self.TargetDir,"LOCATED_VARIABLES.h"))
etisserant@18: locations = []
etisserant@18: lines = [line.strip() for line in location_file.readlines()]
etisserant@18: for line in lines:
etisserant@18: result = LOCATED_MODEL.match(line)
etisserant@18: if result:
etisserant@18: resdict = result.groupdict()
etisserant@18: # rewrite location as a tuple of integers
etisserant@18: resdict['LOC'] = tuple(map(int,resdict['LOC'].split(',')))
etisserant@18: if not resdict['SIZE']:
etisserant@18: resdict['SIZE'] = 'X'
etisserant@18: locations.append(resdict)
etisserant@18: self.PLCGeneratedCFiles = C_files
etisserant@18: self.PLCGeneratedLocatedVars = locations
etisserant@18: return True
etisserant@18:
etisserant@18: def _build(self, logger):
etisserant@18: buildpath = os.path.join(self.ProjectPath, "build")
etisserant@18: if not os.path.exists(buildpath):
etisserant@18: os.mkdir(buildpath)
etisserant@18:
etisserant@18: logger.write("Start build in %s" % buildpath)
etisserant@18:
etisserant@18: if not self._Generate_SoftPLC(buildpath, logger):
etisserant@18: logger.write("SoftPLC code generation failed !")
etisserant@18: return
etisserant@18:
etisserant@18: logger.write("SoftPLC code generation successfull")
etisserant@18:
etisserant@18: try:
etisserant@18: CFilesAndCFLAGS, LDFLAGS = self._Generate_C(
etisserant@18: buildpath,
etisserant@18: (),
etisserant@18: self.PLCGeneratedLocatedVars,
etisserant@18: logger)
etisserant@18: except Exception, msg:
etisserant@18: logger.write_error("Plugins code generation Failed !")
etisserant@18: logger.write_error(str(msg))
etisserant@18: return
etisserant@18:
etisserant@18: logger.write_error("Plugins code generation successfull")
etisserant@18:
etisserant@18: for CFile, CFLAG in CFilesAndCFLAGS:
etisserant@18: print CFile,CFLAG
etisserant@18:
etisserant@18: LDFLAGS
etisserant@18:
etisserant@18: PluginMethods = [("Build",_build), ("Clean",None), ("Run",None), ("EditPLC",None), ("Simulate",None)]
etisserant@18: