ConfigTreeNode.py
changeset 1581 2295fdc5c271
parent 1511 91538d0c242c
child 1722 89824afffef2
equal deleted inserted replaced
1580:f37b88d3edc6 1581:2295fdc5c271
    51             </xsd:complexType>
    51             </xsd:complexType>
    52           </xsd:element>
    52           </xsd:element>
    53         </xsd:schema>""")
    53         </xsd:schema>""")
    54 
    54 
    55 NameTypeSeparator = '@'
    55 NameTypeSeparator = '@'
    56 XSDSchemaErrorMessage = _("%s XML file doesn't follow XSD schema at line %d:\n%s")
    56 XSDSchemaErrorMessage = _("{a1} XML file doesn't follow XSD schema at line %{a2}:\n{a3}")
    57 
    57 
    58 class ConfigTreeNode:
    58 class ConfigTreeNode:
    59     """
    59     """
    60     This class is the one that define confnodes.
    60     This class is the one that define confnodes.
    61     """
    61     """
   419         # Rename confnode dir if exist
   419         # Rename confnode dir if exist
   420         if not dontexist:
   420         if not dontexist:
   421             shutil.move(oldname, self.CTNPath())
   421             shutil.move(oldname, self.CTNPath())
   422         # warn user he has two left hands
   422         # warn user he has two left hands
   423         if DesiredName != res:
   423         if DesiredName != res:
   424             self.GetCTRoot().logger.write_warning(_("A child named \"%s\" already exist -> \"%s\"\n")%(DesiredName,res))
   424             msg = _("A child named \"{a1}\" already exists -> \"{a2}\"\n").format(a1 = DesiredName, a2 = res)
       
   425             self.GetCTRoot().logger.write_warning(msg)
   425         return res
   426         return res
   426 
   427 
   427     def GetAllChannels(self):
   428     def GetAllChannels(self):
   428         AllChannels=[]
   429         AllChannels=[]
   429         for CTNInstance in self.CTNParent.IterChildren():
   430         for CTNInstance in self.CTNParent.IterChildren():
   523         CTNChildrenTypes = dict(zip(transpose[0],zip(transpose[1],transpose[2])))
   524         CTNChildrenTypes = dict(zip(transpose[0],zip(transpose[1],transpose[2])))
   524         # Check that adding this confnode is allowed
   525         # Check that adding this confnode is allowed
   525         try:
   526         try:
   526             CTNClass, CTNHelp = CTNChildrenTypes[CTNType]
   527             CTNClass, CTNHelp = CTNChildrenTypes[CTNType]
   527         except KeyError:
   528         except KeyError:
   528             raise Exception, _("Cannot create child %s of type %s ")%(CTNName, CTNType)
   529             raise Exception, _("Cannot create child {a1} of type {a2} ").format(a1 = CTNName, a2 = CTNType)
   529         
   530         
   530         # if CTNClass is a class factory, call it. (prevent unneeded imports)
   531         # if CTNClass is a class factory, call it. (prevent unneeded imports)
   531         if type(CTNClass) == types.FunctionType:
   532         if type(CTNClass) == types.FunctionType:
   532             CTNClass = CTNClass()
   533             CTNClass = CTNClass()
   533         
   534         
   534         # Eventualy Initialize child instance list for this class of confnode
   535         # Eventualy Initialize child instance list for this class of confnode
   535         ChildrenWithSameClass = self.Children.setdefault(CTNType, list())
   536         ChildrenWithSameClass = self.Children.setdefault(CTNType, list())
   536         # Check count
   537         # Check count
   537         if getattr(CTNClass, "CTNMaxCount", None) and len(ChildrenWithSameClass) >= CTNClass.CTNMaxCount:
   538         if getattr(CTNClass, "CTNMaxCount", None) and len(ChildrenWithSameClass) >= CTNClass.CTNMaxCount:
   538             raise Exception, _("Max count (%d) reached for this confnode of type %s ")%(CTNClass.CTNMaxCount, CTNType)
   539             msg = _("Max count ({a1}) reached for this confnode of type {a2} ").format(a1 = CTNClass.CTNMaxCount, a2 = CTNType)
       
   540             raise Exception, msg
   539         
   541         
   540         # create the final class, derived of provided confnode and template
   542         # create the final class, derived of provided confnode and template
   541         class FinalCTNClass(CTNClass, ConfigTreeNode):
   543         class FinalCTNClass(CTNClass, ConfigTreeNode):
   542             """
   544             """
   543             ConfNode class is derivated into FinalCTNClass before being instanciated
   545             ConfNode class is derivated into FinalCTNClass before being instanciated
   559                 if os.path.isdir(_self.CTNPath(NewCTNName)): #and os.path.isfile(_self.ConfNodeXmlFilePath(CTNName)):
   561                 if os.path.isdir(_self.CTNPath(NewCTNName)): #and os.path.isfile(_self.ConfNodeXmlFilePath(CTNName)):
   560                     #Load the confnode.xml file into parameters members
   562                     #Load the confnode.xml file into parameters members
   561                     _self.LoadXMLParams(NewCTNName)
   563                     _self.LoadXMLParams(NewCTNName)
   562                     # Basic check. Better to fail immediately.
   564                     # Basic check. Better to fail immediately.
   563                     if (_self.BaseParams.getName() != NewCTNName):
   565                     if (_self.BaseParams.getName() != NewCTNName):
   564                         raise Exception, _("Project tree layout do not match confnode.xml %s!=%s ")%(NewCTNName, _self.BaseParams.getName())
   566                         msg = _("Project tree layout do not match confnode.xml {a1}!={a2} ").\
       
   567                               format(a1 = NewCTNName, a2 = _self.BaseParams.getName())
       
   568                         raise Exception, msg
   565 
   569 
   566                     # Now, self.CTNPath() should be OK
   570                     # Now, self.CTNPath() should be OK
   567                     
   571                     
   568                     # Check that IEC_Channel is not already in use.
   572                     # Check that IEC_Channel is not already in use.
   569                     _self.FindNewIEC_Channel(_self.BaseParams.getIEC_Channel())
   573                     _self.FindNewIEC_Channel(_self.BaseParams.getIEC_Channel())
   612         if self.MandatoryParams:
   616         if self.MandatoryParams:
   613             try:
   617             try:
   614                 basexmlfile = open(self.ConfNodeBaseXmlFilePath(CTNName), 'r')
   618                 basexmlfile = open(self.ConfNodeBaseXmlFilePath(CTNName), 'r')
   615                 self.BaseParams, error = _BaseParamsParser.LoadXMLString(basexmlfile.read())
   619                 self.BaseParams, error = _BaseParamsParser.LoadXMLString(basexmlfile.read())
   616                 if error is not None:
   620                 if error is not None:
   617                     self.GetCTRoot().logger.write_warning(
   621                     (fname, lnum, src) = ((ConfNodeName + " BaseParams",) + error)
   618                         XSDSchemaErrorMessage % ((ConfNodeName + " BaseParams",) + error))
   622                     self.GetCTRoot().logger.write_warning(XSDSchemaErrorMessage.format(a1 = fname, a2 = lnum, a3 = src))
   619                 self.MandatoryParams = ("BaseParams", self.BaseParams)
   623                 self.MandatoryParams = ("BaseParams", self.BaseParams)
   620                 basexmlfile.close()
   624                 basexmlfile.close()
   621             except Exception, exc:
   625             except Exception, exc:
   622                 self.GetCTRoot().logger.write_error(_("Couldn't load confnode base parameters %s :\n %s") % (ConfNodeName, unicode(exc)))
   626                 msg = _("Couldn't load confnode base parameters {a1} :\n {a2}").format(a1 =  ConfNodeName, a2 = unicode(exc))
       
   627                 self.GetCTRoot().logger.write_error(msg)
   623                 self.GetCTRoot().logger.write_error(traceback.format_exc())
   628                 self.GetCTRoot().logger.write_error(traceback.format_exc())
   624         
   629         
   625         # Get the xml tree
   630         # Get the xml tree
   626         if self.CTNParams:
   631         if self.CTNParams:
   627             try:
   632             try:
   628                 xmlfile = open(self.ConfNodeXmlFilePath(CTNName), 'r')
   633                 xmlfile = open(self.ConfNodeXmlFilePath(CTNName), 'r')
   629                 obj, error = self.Parser.LoadXMLString(xmlfile.read())
   634                 obj, error = self.Parser.LoadXMLString(xmlfile.read())
   630                 if error is not None:
   635                 if error is not None:
   631                     self.GetCTRoot().logger.write_warning(
   636                     (fname, lnum, src) = ((ConfNodeName,) + error)
   632                         XSDSchemaErrorMessage % ((ConfNodeName,) + error))
   637                     self.GetCTRoot().logger.write_warning(XSDSchemaErrorMessage.format(a1 = fname, a2 = lnum, a3 = src))
   633                 name = obj.getLocalTag()
   638                 name = obj.getLocalTag()
   634                 setattr(self, name, obj)
   639                 setattr(self, name, obj)
   635                 self.CTNParams = (name, obj)
   640                 self.CTNParams = (name, obj)
   636                 xmlfile.close()
   641                 xmlfile.close()
   637             except Exception, exc:
   642             except Exception, exc:
   638                 self.GetCTRoot().logger.write_error(_("Couldn't load confnode parameters %s :\n %s") % (ConfNodeName, unicode(exc)))
   643                 msg = _("Couldn't load confnode parameters {a1} :\n {a2}").format(a1 = ConfNodeName, a2 = unicode(exc))
       
   644                 self.GetCTRoot().logger.write_error(msg)
   639                 self.GetCTRoot().logger.write_error(traceback.format_exc())
   645                 self.GetCTRoot().logger.write_error(traceback.format_exc())
   640         
   646         
   641     def LoadChildren(self):
   647     def LoadChildren(self):
   642         # Iterate over all CTNName@CTNType in confnode directory, and try to open them
   648         # Iterate over all CTNName@CTNType in confnode directory, and try to open them
   643         for CTNDir in os.listdir(self.CTNPath()):
   649         for CTNDir in os.listdir(self.CTNPath()):
   645                CTNDir.count(NameTypeSeparator) == 1:
   651                CTNDir.count(NameTypeSeparator) == 1:
   646                 pname, ptype = CTNDir.split(NameTypeSeparator)
   652                 pname, ptype = CTNDir.split(NameTypeSeparator)
   647                 try:
   653                 try:
   648                     self.CTNAddChild(pname, ptype)
   654                     self.CTNAddChild(pname, ptype)
   649                 except Exception, exc:
   655                 except Exception, exc:
   650                     self.GetCTRoot().logger.write_error(_("Could not add child \"%s\", type %s :\n%s\n")%(pname, ptype, unicode(exc)))
   656                     msg = _("Could not add child \"{a1}\", type {a2} :\n{a3}\n").format(a1 = pname, a2 = ptype, a3 = unicode(exc))
       
   657                     self.GetCTRoot().logger.write_error(msg)
   651                     self.GetCTRoot().logger.write_error(traceback.format_exc())
   658                     self.GetCTRoot().logger.write_error(traceback.format_exc())
   652 
   659