etisserant@13: """ etisserant@13: Base definitions for beremiz plugins etisserant@13: """ etisserant@13: etisserant@13: import os etisserant@13: import types etisserant@13: import shutil etisserant@13: from xml.dom import minidom etisserant@13: etisserant@13: _BaseParamsClass = GenerateClassesFromXSDstring(""" etisserant@12: etisserant@12: etisserant@12: etisserant@13: etisserant@13: etisserant@13: etisserant@12: etisserant@12: etisserant@13: """)[0]["BaseParams"] etisserant@13: etisserant@13: NameTypeSeparator = '@' etisserant@13: etisserant@13: class PlugTemplate: etisserant@13: """ etisserant@13: This class is the one that define plugins. etisserant@13: """ etisserant@13: etisserant@13: XSD = None etisserant@13: PlugChildsTypes = [] etisserant@13: PlugMaxCount = None etisserant@13: PluginMethods = [] etisserant@13: etisserant@13: def _AddParamsMembers(self): etisserant@13: Classes = GenerateClassesFromXSDstring(self.XSD)[0] etisserant@13: self.PlugParams = [] etisserant@13: for name, XSDclass in Classes.items(): etisserant@13: if XSDclass.IsBaseClass: etisserant@13: obj = XSDclass() etisserant@13: self.PlugParams.append( (name, obj) ) etisserant@13: setattr(self, name, obj) etisserant@13: etisserant@13: def __init__(self, PlugPath): etisserant@13: # Create BaseParam etisserant@13: self.BaseParams = _BaseParamsClass() etisserant@13: self.MandatoryParams = [("BaseParams", self.BaseParams)] etisserant@13: self._AddParamsMembers() etisserant@13: self.PluggedChilds = {} etisserant@13: etisserant@13: def PluginXmlFilePath(self, PlugName=None): etisserant@13: return os.path.join(self.PlugPath(PlugName), "plugin.xml") etisserant@13: etisserant@13: def PlugPath(self,PlugName=None): etisserant@13: if not PlugName: etisserant@13: PlugName = self.BaseParams.getName() etisserant@13: return os.path.join(self.PlugParent.PlugPath(), PlugName + NameTypeSeparator + self.PlugType) etisserant@13: etisserant@13: def PlugTestModified(self): etisserant@13: return False etisserant@13: etisserant@13: def OnPlugSave(self): etisserant@13: return True etisserant@13: etisserant@13: def PlugRequestSave(self): etisserant@13: # If plugin do not have corresponding directory etisserant@13: if not os.path.isdir(self.PlugPath(PlugName)): etisserant@13: # Create it etisserant@13: os.mkdir(self.PlugPath(PlugName)) etisserant@13: etisserant@13: # generate XML for all XML parameters controllers of the plugin etisserant@13: XMLString = '' etisserant@13: for nodeName, XMLController in self.PlugParams + self.MandatoryParams: etisserant@13: XMLString += XMLController.generateXMLTextMethod(self, nodeName, 0) etisserant@13: XMLFile = open(self.PluginXmlFilePath(PlugName),'w') etisserant@13: XMLFile.write(XMLString) etisserant@13: XMLFile.close() etisserant@13: etisserant@13: # Call the plugin specific OnPlugSave method etisserant@13: self.OnPlugSave() etisserant@13: etisserant@13: # go through all childs and do the same etisserant@13: for PlugChild in self.IterChilds(): etisserant@13: PlugChild.PlugRequestSave() etisserant@13: etisserant@13: def PlugImport(self, src_PlugPath): etisserant@13: shutil.copytree(src_PlugPath, self.PlugPath) etisserant@13: return True etisserant@13: etisserant@13: def PlugGenerate_C(self, buildpath, current_location, locations): etisserant@13: """ etisserant@13: Generate C code etisserant@13: @param current_location: Tupple containing plugin IEC location : %I0.0.4.5 => (0,0,4,5) etisserant@13: @param locations: List of complete variables locations \ etisserant@13: [(IEC_loc, IEC_Direction IEC_Type, Name)]\ etisserant@13: ex: [((0,0,4,5),'I','X','__IX_0_0_4_5'),...] etisserant@13: """ etisserant@13: return [],"" etisserant@13: etisserant@13: def _Generate_C(self, buildpath, current_location, locations): etisserant@13: # Generate plugins [(Cfiles, CFLAGS)], LDFLAGS etisserant@13: PlugCFilesAndCFLAGS, PlugLDFLAGS = self._Generate_C(buildpath, current_location, locations) etisserant@13: # recurse through all childs, and stack their results etisserant@13: for PlugChild in self.IterChilds(): etisserant@13: # Get childs [(Cfiles, CFLAGS)], LDFLAGS etisserant@13: CFilesAndCFLAGS, LDFLAGS = \ etisserant@13: PlugChild._Generate_C( etisserant@13: #keep the same path etisserant@13: buildpath, etisserant@13: # but update location (add curent IEC channel at the end) etisserant@13: current_location + (self.BaseParams.getIEC_Channel()), etisserant@13: # filter locations that start with current IEC location etisserant@13: [ (l,d,t,n) for l,d,t,n in locations if l[0:len(current_location)] == current_location ]) etisserant@13: # stack the result etisserant@13: PlugCFilesAndCFLAGS += CFilesAndCFLAGS etisserant@13: PlugLDFLAGS += LDFLAGS etisserant@13: etisserant@13: return PlugCFilesAndCFLAGS,PlugLDFLAGS etisserant@13: etisserant@13: def BlockTypesFactory(self): etisserant@13: return [] etisserant@13: etisserant@13: def STLibraryFactory(self): etisserant@13: return "" etisserant@13: etisserant@13: def IterChilds(self): etisserant@13: for PlugType, PluggedChilds in self.PluggedChilds.items(): etisserant@13: for PlugInstance in PluggedChilds: etisserant@13: yield PlugInstance etisserant@13: etisserant@13: def _GetChildBySomething(self, sep, something, matching): etisserant@13: toks = matching.split(sep,1) etisserant@13: for PlugInstance in self.IterChilds: etisserant@13: # if match component of the name etisserant@13: if getattr(PlugInstance.BaseParams, something) == toks[0]: etisserant@13: # if Name have other components etisserant@13: if len(toks) == 2: etisserant@13: # Recurse in order to find the latest object etisserant@13: return PlugInstance._GetChildBySomething( sep, something, toks[1]) etisserant@13: # No sub name -> found etisserant@13: return PlugInstance etisserant@13: # Not found etisserant@13: return None etisserant@13: etisserant@13: def GetChildByName(self, Name): etisserant@13: return self._GetChildBySomething('.',"Name", Name) etisserant@13: etisserant@13: def GetChildByIECLocation(self, Location): etisserant@13: return self._GetChildBySomething('_',"IEC_Channel", Name) etisserant@13: etisserant@13: def FindNewIEC_Channel(self, DesiredChannel): etisserant@13: """ etisserant@13: Changes IEC Channel number to DesiredChannel if available, nearest available if not. etisserant@13: @param DesiredChannel: The desired IEC channel (int) etisserant@13: """ etisserant@13: # Get Current IEC channel etisserant@13: CurrentChannel = self.BaseParams.getIEC_Channel() etisserant@13: # Do nothing if no change etisserant@13: if CurrentChannel == DesiredChannel: return CurrentChannel etisserant@13: # Build a list of used Channels out of parent's PluggedChilds etisserant@13: AllChannels=[] etisserant@13: for PlugInstance in self.PlugParent.IterChilds(): etisserant@13: if PlugInstance != self: etisserant@13: AllChannels.append(PlugInstance.BaseParams.getIEC_Channel()) etisserant@13: AllChannels.sort() etisserant@13: etisserant@13: # Now, try to guess the nearest available channel etisserant@13: res = DesiredChannel etisserant@13: while res in AllChannels: # While channel not free etisserant@13: if res < CurrentChannel: # Want to go down ? etisserant@13: res -= 1 # Test for n-1 etisserant@13: if res < 0 : return CurrentChannel # Can't go bellow 0, do nothing etisserant@13: else : # Want to go up ? etisserant@13: res += 1 # Test for n-1 etisserant@13: # Finally set IEC Channel etisserant@13: self.BaseParams.setIEC_Channel(res) etisserant@13: return res etisserant@13: etisserant@13: def OnPlugClose(self): etisserant@13: return True etisserant@13: etisserant@13: def _doRemoveChild(self, PlugInstance): etisserant@13: # Remove all childs of child etisserant@13: for SubPlugInstance in PlugInstance.IterChilds(): etisserant@13: PlugInstance._doRemoveChild(SubPlugInstance) etisserant@13: # Call the OnCloseMethod etisserant@13: PlugInstance.OnPlugClose() etisserant@13: # Delete plugin dir etisserant@13: shutil.rmtree(PlugInstance.PlugPath()) etisserant@13: # Remove child of PluggedChilds etisserant@13: self.PluggedChilds[PlugInstance.PlugType].remove(PlugInstance) etisserant@13: # Forget it... (View have to refresh) etisserant@13: etisserant@13: def PlugRemoveChild(self, PlugName): etisserant@13: # Fetch the plugin etisserant@13: PlugInstance = self.GetChildByName(PlugName) etisserant@13: # Ask to his parent to remove it etisserant@13: PlugInstance.PlugParent._doRemoveChild(PlugInstance) etisserant@13: etisserant@13: def PlugAddChild(self, PlugName, PlugType): etisserant@13: """ etisserant@13: Create the plugins that may be added as child to this node self etisserant@13: @param PlugType: string desining the plugin class name (get name from PlugChildsTypes) etisserant@13: @param PlugName: string for the name of the plugin instance etisserant@13: """ etisserant@13: PlugChildsTypes = dict(self.PlugChildsTypes) etisserant@13: # Check that adding this plugin is allowed etisserant@13: try: etisserant@13: PlugClass = PlugChildsTypes[PlugType] etisserant@13: except KeyError: etisserant@13: raise Exception, "Cannot create child %s of type %s "%(PlugName, PlugType) etisserant@13: etisserant@13: # if PlugClass is a class factory, call it. (prevent unneeded imports) etisserant@13: if type(PlugClass) == types.FunctionType: etisserant@13: PlugClass = PlugClass() etisserant@13: etisserant@13: # Eventualy Initialize child instance list for this class of plugin etisserant@13: PluggedChildsWithSameClass = self.PluggedChilds.setdefault(PlugType,list()) etisserant@13: # Check count etisserant@13: if PlugClass.MaxCount and len(PluggedChildsWithSameClass) >= PlugClass.MaxCount: etisserant@13: raise Exception, "Max count (%d) reached for this plugin of type %s "%(PlugClass.MaxCount, PlugType) etisserant@13: etisserant@13: # create the final class, derived of provided plugin and template etisserant@13: class FinalPlugClass(PlugClass, PlugTemplate): etisserant@13: """ etisserant@13: Plugin class is derivated into FinalPlugClass before being instanciated etisserant@13: This way __init__ is overloaded to ensure PlugTemplate.__init__ is called etisserant@13: before PlugClass.__init__, and to do the file related stuff. etisserant@13: """ etisserant@13: def __init__(_self): etisserant@13: # self is the parent etisserant@13: _self.PlugParent = self etisserant@13: # Keep track of the plugin type name etisserant@13: _self.PlugType = PlugType etisserant@13: # Call the base plugin template init - change XSD into class members etisserant@13: PlugTemplate.__init__(_self) etisserant@13: # If dir have already be made, and file exist etisserant@13: if os.path.isdir(_self.PlugPath(PlugName)) and os.path.isfile(_self.PluginXmlFilePath(PlugName)): etisserant@13: #Load the plugin.xml file into parameters members etisserant@13: _self.LoadXMLParams() etisserant@13: # Call the plugin real __init__ etisserant@13: PlugClass.__init__(_self) etisserant@13: #Load and init all the childs etisserant@13: _self.LoadChilds() etisserant@13: # Check that IEC_Channel is not already in use. etisserant@13: self.FindNewIEC_Channel(self.BaseParams.getIEC_Channel()) etisserant@13: else: etisserant@13: # If plugin do not have corresponding file/dirs - they will be created on Save etisserant@13: # Set plugin name etisserant@13: _self.BaseParams.setName(PlugName) etisserant@13: # Find an IEC number etisserant@13: _self.FindNewIEC_Channel(0) etisserant@13: # Call the plugin real __init__ etisserant@13: PlugClass.__init__(_self) etisserant@13: etisserant@13: # Create the object out of the resulting class etisserant@13: newPluginOpj = FinalPlugClass() etisserant@13: # Store it in PluggedChils etisserant@13: PluggedChildsWithSameClass.append(newPluginOpj) etisserant@13: etisserant@13: return newPluginOpj etisserant@13: etisserant@13: etisserant@13: def LoadXMLParams(self): etisserant@13: # PlugParams have been filled, make a local dict to work with etisserant@13: PlugParams = dict(self.PlugParams + self.MandatoryParams) etisserant@13: # Get the xml tree etisserant@13: xmlfile = open(self.PluginXmlFilePath(PlugName), 'r') etisserant@13: tree = minidom.parse(xmlfile) etisserant@13: xmlfile.close() etisserant@13: # for each root elements etisserant@13: for subtree in tree.childNodes: etisserant@13: # if a plugin specific parameter set etisserant@13: if subtree.nodeName in PlugParams: etisserant@13: #Load into associated xmlclass. etisserant@13: PlugParams[subtree.nodeName].loadXMLTree(subtree) etisserant@13: etisserant@13: # Basic check. Better to fail immediately. etisserant@13: if(self.BaseParams.getName() != PlugName): etisserant@13: raise Exception, "Project tree layout do not match plugin.xml %s!=%s "%(PlugName,self.BaseParams.getName()) etisserant@13: # Now, self.PlugPath() should be OK etisserant@13: etisserant@13: def LoadChilds(self): etisserant@13: # Iterate over all PlugName@PlugType in plugin directory, and try to open them etisserant@13: for PlugDir in os.listdir(self.PlugPath()): etisserant@13: if os.path.isdir(os.path.join(self.PlugPath(),PlugDir)) and \ etisserant@13: PlugDir.count(NameTypeSeparator) == 1: etisserant@13: try: etisserant@13: self.PlugAddChild(*PlugDir.split[NameTypeSeparator]) etisserant@13: except Exception, e: etisserant@13: print e