plugger.py
changeset 106 9810689febb0
parent 105 434aed8dc58d
child 107 65fe90d49bf7
equal deleted inserted replaced
105:434aed8dc58d 106:9810689febb0
    92         # Create BaseParam 
    92         # Create BaseParam 
    93         self.BaseParams = _BaseParamsClass()
    93         self.BaseParams = _BaseParamsClass()
    94         self.MandatoryParams = ("BaseParams", self.BaseParams)
    94         self.MandatoryParams = ("BaseParams", self.BaseParams)
    95         self._AddParamsMembers()
    95         self._AddParamsMembers()
    96         self.PluggedChilds = {}
    96         self.PluggedChilds = {}
       
    97         # copy PluginMethods so that it can be later customized
       
    98         self.PluginMethods = [dic.copy() for dic in self.PluginMethods]
    97 
    99 
    98     def PluginBaseXmlFilePath(self, PlugName=None):
   100     def PluginBaseXmlFilePath(self, PlugName=None):
    99         return os.path.join(self.PlugPath(PlugName), "baseplugin.xml")
   101         return os.path.join(self.PlugPath(PlugName), "baseplugin.xml")
   100     
   102     
   101     def PluginXmlFilePath(self, PlugName=None):
   103     def PluginXmlFilePath(self, PlugName=None):
   413         """
   415         """
   414         Create the plugins that may be added as child to this node self
   416         Create the plugins that may be added as child to this node self
   415         @param PlugType: string desining the plugin class name (get name from PlugChildsTypes)
   417         @param PlugType: string desining the plugin class name (get name from PlugChildsTypes)
   416         @param PlugName: string for the name of the plugin instance
   418         @param PlugName: string for the name of the plugin instance
   417         """
   419         """
   418         PlugChildsTypes = dict(self.PlugChildsTypes)
   420         # reorgabize self.PlugChildsTypes tuples from (name, PlugClass, Help)
       
   421         # to ( name, (PlugClass, Help)), an make a dict
       
   422         transpose = zip(*self.PlugChildsTypes)
       
   423         PlugChildsTypes = dict(zip(transpose[0],zip(transpose[1],transpose[2])))
   419         # Check that adding this plugin is allowed
   424         # Check that adding this plugin is allowed
   420         try:
   425         try:
   421             PlugClass = PlugChildsTypes[PlugType]
   426             PlugClass, PlugHelp = PlugChildsTypes[PlugType]
   422         except KeyError:
   427         except KeyError:
   423             raise Exception, "Cannot create child %s of type %s "%(PlugName, PlugType)
   428             raise Exception, "Cannot create child %s of type %s "%(PlugName, PlugType)
   424         
   429         
   425         # if PlugClass is a class factory, call it. (prevent unneeded imports)
   430         # if PlugClass is a class factory, call it. (prevent unneeded imports)
   426         if type(PlugClass) == types.FunctionType:
   431         if type(PlugClass) == types.FunctionType:
   442             def __init__(_self):
   447             def __init__(_self):
   443                 # self is the parent
   448                 # self is the parent
   444                 _self.PlugParent = self
   449                 _self.PlugParent = self
   445                 # Keep track of the plugin type name
   450                 # Keep track of the plugin type name
   446                 _self.PlugType = PlugType
   451                 _self.PlugType = PlugType
       
   452                 # remind the help string, for more fancy display
       
   453                 _self.PlugHelp = PlugHelp
   447                 # Call the base plugin template init - change XSD into class members
   454                 # Call the base plugin template init - change XSD into class members
   448                 PlugTemplate.__init__(_self)
   455                 PlugTemplate.__init__(_self)
   449                 # check name is unique
   456                 # check name is unique
   450                 NewPlugName = _self.FindNewName(PlugName, logger)
   457                 NewPlugName = _self.FindNewName(PlugName, logger)
   451                 # If dir have already be made, and file exist
   458                 # If dir have already be made, and file exist
   452                 if os.path.isdir(_self.PlugPath(NewPlugName)): #and os.path.isfile(_self.PluginXmlFilePath(PlugName)):
   459                 if os.path.isdir(_self.PlugPath(NewPlugName)): #and os.path.isfile(_self.PluginXmlFilePath(PlugName)):
   453                     #Load the plugin.xml file into parameters members
   460                     #Load the plugin.xml file into parameters members
   454                     _self.LoadXMLParams(NewPlugName)
   461                     _self.LoadXMLParams(logger, NewPlugName)
   455                     # Basic check. Better to fail immediately.
   462                     # Basic check. Better to fail immediately.
   456                     if (_self.BaseParams.getName() != NewPlugName):
   463                     if (_self.BaseParams.getName() != NewPlugName):
   457                         raise Exception, "Project tree layout do not match plugin.xml %s!=%s "%(NewPlugName, _self.BaseParams.getName())
   464                         raise Exception, "Project tree layout do not match plugin.xml %s!=%s "%(NewPlugName, _self.BaseParams.getName())
   458 
   465 
   459                     # Now, self.PlugPath() should be OK
   466                     # Now, self.PlugPath() should be OK
   484         PluggedChildsWithSameClass.append(newPluginOpj)
   491         PluggedChildsWithSameClass.append(newPluginOpj)
   485         
   492         
   486         return newPluginOpj
   493         return newPluginOpj
   487             
   494             
   488 
   495 
   489     def LoadXMLParams(self, PlugName = None):
   496     def LoadXMLParams(self, logger, PlugName = None):
   490         methode_name = os.path.join(self.PlugPath(PlugName), "methods.py")
   497         methode_name = os.path.join(self.PlugPath(PlugName), "methods.py")
   491         if os.path.isfile(methode_name):
   498         if os.path.isfile(methode_name):
       
   499             logger.write("Info: %s plugin as some special methods in methods.py\n" % (PlugName or "Root"))
   492             execfile(methode_name)
   500             execfile(methode_name)
   493 
   501 
   494         # Get the base xml tree
   502         # Get the base xml tree
   495         if self.MandatoryParams:
   503         if self.MandatoryParams:
   496             basexmlfile = open(self.PluginBaseXmlFilePath(PlugName), 'r')
   504             #try:
   497             basetree = minidom.parse(basexmlfile)
   505                 basexmlfile = open(self.PluginBaseXmlFilePath(PlugName), 'r')
   498             self.MandatoryParams[1].loadXMLTree(basetree.childNodes[0])
   506                 basetree = minidom.parse(basexmlfile)
   499             basexmlfile.close()
   507                 self.MandatoryParams[1].loadXMLTree(basetree.childNodes[0])
       
   508                 basexmlfile.close()
       
   509             #except Exception, e:
       
   510             #    logger.write_error("Couldn't load plugin base parameters %s :\n %s" % (PlugName, str(e)))
       
   511                 
   500         
   512         
   501         # Get the xml tree
   513         # Get the xml tree
   502         if self.PlugParams:
   514         if self.PlugParams:
   503             xmlfile = open(self.PluginXmlFilePath(PlugName), 'r')
   515             #try:
   504             tree = minidom.parse(xmlfile)
   516                 xmlfile = open(self.PluginXmlFilePath(PlugName), 'r')
   505             self.PlugParams[1].loadXMLTree(tree.childNodes[0])
   517                 tree = minidom.parse(xmlfile)
   506             xmlfile.close()
   518                 self.PlugParams[1].loadXMLTree(tree.childNodes[0])
       
   519                 xmlfile.close()
       
   520             #except Exception, e:
       
   521             #    logger.write_error("Couldn't load plugin parameters %s :\n %s" % (PlugName, str(e)))
   507         
   522         
   508     def LoadChilds(self, logger):
   523     def LoadChilds(self, logger):
   509         # Iterate over all PlugName@PlugType in plugin directory, and try to open them
   524         # Iterate over all PlugName@PlugType in plugin directory, and try to open them
   510         for PlugDir in os.listdir(self.PlugPath()):
   525         for PlugDir in os.listdir(self.PlugPath()):
   511             if os.path.isdir(os.path.join(self.PlugPath(), PlugDir)) and \
   526             if os.path.isdir(os.path.join(self.PlugPath(), PlugDir)) and \
   560     - ...
   575     - ...
   561     
   576     
   562     """
   577     """
   563 
   578 
   564     # For root object, available Childs Types are modules of the plugin packages.
   579     # For root object, available Childs Types are modules of the plugin packages.
   565     PlugChildsTypes = [(name, _GetClassFunction(name)) for name in plugins.__all__]
   580     PlugChildsTypes = [(name, _GetClassFunction(name), help) for name, help in zip(plugins.__all__,plugins.helps)]
   566 
   581 
   567     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
   582     XSD = """<?xml version="1.0" encoding="ISO-8859-1" ?>
   568     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   583     <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   569       <xsd:element name="BeremizRoot">
   584       <xsd:element name="BeremizRoot">
   570         <xsd:complexType>
   585         <xsd:complexType>
   600                     </xsd:complexType>
   615                     </xsd:complexType>
   601                   </xsd:element>
   616                   </xsd:element>
   602                 </xsd:choice>
   617                 </xsd:choice>
   603               </xsd:complexType>
   618               </xsd:complexType>
   604             </xsd:element>
   619             </xsd:element>
       
   620             <xsd:element name="Connection">
       
   621               <xsd:complexType>
       
   622                 <xsd:choice>
       
   623                   <xsd:element name="Local"/>
       
   624                   <xsd:element name="TCP_IP">
       
   625                     <xsd:complexType>
       
   626                       <xsd:attribute name="Host" type="xsd:string" use="required"/>
       
   627                     </xsd:complexType>
       
   628                   </xsd:element>
       
   629                 </xsd:choice>
       
   630               </xsd:complexType>
       
   631             </xsd:element>
   605           </xsd:sequence>
   632           </xsd:sequence>
   606           <xsd:attribute name="Compiler" type="xsd:string" use="optional" default="gcc"/>
   633           <xsd:attribute name="Compiler" type="xsd:string" use="optional" default="gcc"/>
   607           <xsd:attribute name="CFLAGS" type="xsd:string" use="required"/>
   634           <xsd:attribute name="CFLAGS" type="xsd:string" use="required"/>
   608           <xsd:attribute name="Linker" type="xsd:string" use="optional" default="ld"/>
   635           <xsd:attribute name="Linker" type="xsd:string" use="optional" default="ld"/>
   609           <xsd:attribute name="LDFLAGS" type="xsd:string" use="required"/>
   636           <xsd:attribute name="LDFLAGS" type="xsd:string" use="required"/>
   630         self.PlugType = "Beremiz"
   657         self.PlugType = "Beremiz"
   631         
   658         
   632         # After __init__ root plugin is not valid
   659         # After __init__ root plugin is not valid
   633         self.ProjectPath = None
   660         self.ProjectPath = None
   634         self.PLCEditor = None
   661         self.PLCEditor = None
       
   662         
       
   663         # copy PluginMethods so that it can be later customized
       
   664         self.PluginMethods = [dic.copy() for dic in self.PluginMethods]
   635     
   665     
   636     def HasProjectOpened(self):
   666     def HasProjectOpened(self):
   637         """
   667         """
   638         Return if a project is actually opened
   668         Return if a project is actually opened
   639         """
   669         """
   712         # Keep track of the root plugin (i.e. project path)
   742         # Keep track of the root plugin (i.e. project path)
   713         self.ProjectPath = ProjectPath
   743         self.ProjectPath = ProjectPath
   714         # If dir have already be made, and file exist
   744         # If dir have already be made, and file exist
   715         if os.path.isdir(self.PlugPath()) and os.path.isfile(self.PluginXmlFilePath()):
   745         if os.path.isdir(self.PlugPath()) and os.path.isfile(self.PluginXmlFilePath()):
   716             #Load the plugin.xml file into parameters members
   746             #Load the plugin.xml file into parameters members
   717             result = self.LoadXMLParams()
   747             result = self.LoadXMLParams(logger)
   718             if result:
   748             if result:
   719                 return result
   749                 return result
   720             #Load and init all the childs
   750             #Load and init all the childs
   721             self.LoadChilds(logger)
   751             self.LoadChilds(logger)
   722         self.RefreshPluginsBlockLists()
   752         self.RefreshPluginsBlockLists()