ConfigTreeNode.py
changeset 1744 69dfdb26f600
parent 1742 92932cd370a4
child 1746 45d6f5fba016
equal deleted inserted replaced
1743:c3c3d1318130 1744:69dfdb26f600
   143 
   143 
   144     def OnCTNSave(self, from_project_path=None):
   144     def OnCTNSave(self, from_project_path=None):
   145         #Default, do nothing and return success
   145         #Default, do nothing and return success
   146         return True
   146         return True
   147 
   147 
   148     def GetParamsAttributes(self, path = None):
   148     def GetParamsAttributes(self, path=None):
   149         if path:
   149         if path:
   150             parts = path.split(".", 1)
   150             parts = path.split(".", 1)
   151             if self.MandatoryParams and parts[0] == self.MandatoryParams[0]:
   151             if self.MandatoryParams and parts[0] == self.MandatoryParams[0]:
   152                 return self.MandatoryParams[1].getElementInfos(parts[0], parts[1])
   152                 return self.MandatoryParams[1].getElementInfos(parts[0], parts[1])
   153             elif self.CTNParams and parts[0] == self.CTNParams[0]:
   153             elif self.CTNParams and parts[0] == self.CTNParams[0]:
   422         # Rename confnode dir if exist
   422         # Rename confnode dir if exist
   423         if not dontexist:
   423         if not dontexist:
   424             shutil.move(oldname, self.CTNPath())
   424             shutil.move(oldname, self.CTNPath())
   425         # warn user he has two left hands
   425         # warn user he has two left hands
   426         if DesiredName != res:
   426         if DesiredName != res:
   427             msg = _("A child named \"{a1}\" already exists -> \"{a2}\"\n").format(a1 = DesiredName, a2 = res)
   427             msg = _("A child named \"{a1}\" already exists -> \"{a2}\"\n").format(a1=DesiredName, a2=res)
   428             self.GetCTRoot().logger.write_warning(msg)
   428             self.GetCTRoot().logger.write_warning(msg)
   429         return res
   429         return res
   430 
   430 
   431     def GetAllChannels(self):
   431     def GetAllChannels(self):
   432         AllChannels = []
   432         AllChannels = []
   527         CTNChildrenTypes = dict(zip(transpose[0], zip(transpose[1], transpose[2])))
   527         CTNChildrenTypes = dict(zip(transpose[0], zip(transpose[1], transpose[2])))
   528         # Check that adding this confnode is allowed
   528         # Check that adding this confnode is allowed
   529         try:
   529         try:
   530             CTNClass, CTNHelp = CTNChildrenTypes[CTNType]
   530             CTNClass, CTNHelp = CTNChildrenTypes[CTNType]
   531         except KeyError:
   531         except KeyError:
   532             raise Exception, _("Cannot create child {a1} of type {a2} ").format(a1 = CTNName, a2 = CTNType)
   532             raise Exception, _("Cannot create child {a1} of type {a2} ").format(a1=CTNName, a2=CTNType)
   533 
   533 
   534         # if CTNClass is a class factory, call it. (prevent unneeded imports)
   534         # if CTNClass is a class factory, call it. (prevent unneeded imports)
   535         if type(CTNClass) == types.FunctionType:
   535         if type(CTNClass) == types.FunctionType:
   536             CTNClass = CTNClass()
   536             CTNClass = CTNClass()
   537 
   537 
   538         # Eventualy Initialize child instance list for this class of confnode
   538         # Eventualy Initialize child instance list for this class of confnode
   539         ChildrenWithSameClass = self.Children.setdefault(CTNType, list())
   539         ChildrenWithSameClass = self.Children.setdefault(CTNType, list())
   540         # Check count
   540         # Check count
   541         if getattr(CTNClass, "CTNMaxCount", None) and len(ChildrenWithSameClass) >= CTNClass.CTNMaxCount:
   541         if getattr(CTNClass, "CTNMaxCount", None) and len(ChildrenWithSameClass) >= CTNClass.CTNMaxCount:
   542             msg = _("Max count ({a1}) reached for this confnode of type {a2} ").format(a1 = CTNClass.CTNMaxCount, a2 = CTNType)
   542             msg = _("Max count ({a1}) reached for this confnode of type {a2} ").format(a1=CTNClass.CTNMaxCount, a2=CTNType)
   543             raise Exception, msg
   543             raise Exception, msg
   544 
   544 
   545         # create the final class, derived of provided confnode and template
   545         # create the final class, derived of provided confnode and template
   546         class FinalCTNClass(CTNClass, ConfigTreeNode):
   546         class FinalCTNClass(CTNClass, ConfigTreeNode):
   547             """
   547             """
   565                     # Load the confnode.xml file into parameters members
   565                     # Load the confnode.xml file into parameters members
   566                     _self.LoadXMLParams(NewCTNName)
   566                     _self.LoadXMLParams(NewCTNName)
   567                     # Basic check. Better to fail immediately.
   567                     # Basic check. Better to fail immediately.
   568                     if (_self.BaseParams.getName() != NewCTNName):
   568                     if (_self.BaseParams.getName() != NewCTNName):
   569                         msg = _("Project tree layout do not match confnode.xml {a1}!={a2} ").\
   569                         msg = _("Project tree layout do not match confnode.xml {a1}!={a2} ").\
   570                               format(a1 = NewCTNName, a2 = _self.BaseParams.getName())
   570                               format(a1=NewCTNName, a2=_self.BaseParams.getName())
   571                         raise Exception, msg
   571                         raise Exception, msg
   572 
   572 
   573                     # Now, self.CTNPath() should be OK
   573                     # Now, self.CTNPath() should be OK
   574 
   574 
   575                     # Check that IEC_Channel is not already in use.
   575                     # Check that IEC_Channel is not already in use.
   606     def ClearChildren(self):
   606     def ClearChildren(self):
   607         for child in self.IterChildren():
   607         for child in self.IterChildren():
   608             child.ClearChildren()
   608             child.ClearChildren()
   609         self.Children = {}
   609         self.Children = {}
   610 
   610 
   611     def LoadXMLParams(self, CTNName = None):
   611     def LoadXMLParams(self, CTNName=None):
   612         methode_name = os.path.join(self.CTNPath(CTNName), "methods.py")
   612         methode_name = os.path.join(self.CTNPath(CTNName), "methods.py")
   613         if os.path.isfile(methode_name):
   613         if os.path.isfile(methode_name):
   614             execfile(methode_name)
   614             execfile(methode_name)
   615 
   615 
   616         ConfNodeName = CTNName if CTNName is not None else self.CTNName()
   616         ConfNodeName = CTNName if CTNName is not None else self.CTNName()
   620             try:
   620             try:
   621                 basexmlfile = open(self.ConfNodeBaseXmlFilePath(CTNName), 'r')
   621                 basexmlfile = open(self.ConfNodeBaseXmlFilePath(CTNName), 'r')
   622                 self.BaseParams, error = _BaseParamsParser.LoadXMLString(basexmlfile.read())
   622                 self.BaseParams, error = _BaseParamsParser.LoadXMLString(basexmlfile.read())
   623                 if error is not None:
   623                 if error is not None:
   624                     (fname, lnum, src) = ((ConfNodeName + " BaseParams",) + error)
   624                     (fname, lnum, src) = ((ConfNodeName + " BaseParams",) + error)
   625                     self.GetCTRoot().logger.write_warning(XSDSchemaErrorMessage.format(a1 = fname, a2 = lnum, a3 = src))
   625                     self.GetCTRoot().logger.write_warning(XSDSchemaErrorMessage.format(a1=fname, a2=lnum, a3=src))
   626                 self.MandatoryParams = ("BaseParams", self.BaseParams)
   626                 self.MandatoryParams = ("BaseParams", self.BaseParams)
   627                 basexmlfile.close()
   627                 basexmlfile.close()
   628             except Exception, exc:
   628             except Exception, exc:
   629                 msg = _("Couldn't load confnode base parameters {a1} :\n {a2}").format(a1 =  ConfNodeName, a2 = unicode(exc))
   629                 msg = _("Couldn't load confnode base parameters {a1} :\n {a2}").format(a1=ConfNodeName, a2=unicode(exc))
   630                 self.GetCTRoot().logger.write_error(msg)
   630                 self.GetCTRoot().logger.write_error(msg)
   631                 self.GetCTRoot().logger.write_error(traceback.format_exc())
   631                 self.GetCTRoot().logger.write_error(traceback.format_exc())
   632 
   632 
   633         # Get the xml tree
   633         # Get the xml tree
   634         if self.CTNParams:
   634         if self.CTNParams:
   635             try:
   635             try:
   636                 xmlfile = open(self.ConfNodeXmlFilePath(CTNName), 'r')
   636                 xmlfile = open(self.ConfNodeXmlFilePath(CTNName), 'r')
   637                 obj, error = self.Parser.LoadXMLString(xmlfile.read())
   637                 obj, error = self.Parser.LoadXMLString(xmlfile.read())
   638                 if error is not None:
   638                 if error is not None:
   639                     (fname, lnum, src) = ((ConfNodeName,) + error)
   639                     (fname, lnum, src) = ((ConfNodeName,) + error)
   640                     self.GetCTRoot().logger.write_warning(XSDSchemaErrorMessage.format(a1 = fname, a2 = lnum, a3 = src))
   640                     self.GetCTRoot().logger.write_warning(XSDSchemaErrorMessage.format(a1=fname, a2=lnum, a3=src))
   641                 name = obj.getLocalTag()
   641                 name = obj.getLocalTag()
   642                 setattr(self, name, obj)
   642                 setattr(self, name, obj)
   643                 self.CTNParams = (name, obj)
   643                 self.CTNParams = (name, obj)
   644                 xmlfile.close()
   644                 xmlfile.close()
   645             except Exception, exc:
   645             except Exception, exc:
   646                 msg = _("Couldn't load confnode parameters {a1} :\n {a2}").format(a1 = ConfNodeName, a2 = unicode(exc))
   646                 msg = _("Couldn't load confnode parameters {a1} :\n {a2}").format(a1=ConfNodeName, a2=unicode(exc))
   647                 self.GetCTRoot().logger.write_error(msg)
   647                 self.GetCTRoot().logger.write_error(msg)
   648                 self.GetCTRoot().logger.write_error(traceback.format_exc())
   648                 self.GetCTRoot().logger.write_error(traceback.format_exc())
   649 
   649 
   650     def LoadChildren(self):
   650     def LoadChildren(self):
   651         # Iterate over all CTNName@CTNType in confnode directory, and try to open them
   651         # Iterate over all CTNName@CTNType in confnode directory, and try to open them
   654                CTNDir.count(NameTypeSeparator) == 1:
   654                CTNDir.count(NameTypeSeparator) == 1:
   655                 pname, ptype = CTNDir.split(NameTypeSeparator)
   655                 pname, ptype = CTNDir.split(NameTypeSeparator)
   656                 try:
   656                 try:
   657                     self.CTNAddChild(pname, ptype)
   657                     self.CTNAddChild(pname, ptype)
   658                 except Exception, exc:
   658                 except Exception, exc:
   659                     msg = _("Could not add child \"{a1}\", type {a2} :\n{a3}\n").format(a1 = pname, a2 = ptype, a3 = unicode(exc))
   659                     msg = _("Could not add child \"{a1}\", type {a2} :\n{a3}\n").format(a1=pname, a2=ptype, a3=unicode(exc))
   660                     self.GetCTRoot().logger.write_error(msg)
   660                     self.GetCTRoot().logger.write_error(msg)
   661                     self.GetCTRoot().logger.write_error(traceback.format_exc())
   661                     self.GetCTRoot().logger.write_error(traceback.format_exc())