PLCControler.py
changeset 1350 7280349a3375
parent 1348 aee0a7eb833a
child 1351 a546a63ce1bf
equal deleted inserted replaced
1349:be6c3a036844 1350:7280349a3375
   240 
   240 
   241 #-------------------------------------------------------------------------------
   241 #-------------------------------------------------------------------------------
   242 #            Helpers object for generating instances path list
   242 #            Helpers object for generating instances path list
   243 #-------------------------------------------------------------------------------
   243 #-------------------------------------------------------------------------------
   244 
   244 
   245 class InstanceDefinition(etree.XSLTExtension):
   245 class InstancesPathFactory:
   246     
   246     
   247     def __init__(self, controller, debug):
   247     def __init__(self, instances):
   248         etree.XSLTExtension.__init__(self)
   248         self.Instances = instances
   249         self.Controller = controller
   249         
   250         self.Debug = debug
   250     def AddInstance(self, context, *args):
   251     
   251         self.Instances.append(args[0][0])
   252     def execute(self, context, self_node, input_node, output_parent):
       
   253         instance_infos = etree.Element('infos')
       
   254         self.process_children(context, instance_infos)
       
   255         
       
   256         pou_infos = self.Controller.GetPou(instance_infos.get("name"), self.Debug)
       
   257         if pou_infos is not None:
       
   258             pou_instance = etree.Element('pou_instance',
       
   259                 pou_path=instance_infos.get("path"))
       
   260             pou_instance.append(deepcopy(pou_infos))
       
   261             self.apply_templates(context, pou_instance, output_parent)
       
   262             return
       
   263             
       
   264         datatype_infos = self.Controller.GetDataType(instance_infos.get("name"), self.Debug)
       
   265         if datatype_infos is not None:
       
   266             datatype_instance = etree.Element('datatype_instance',
       
   267                 datatype_path=instance_infos.get("path"))
       
   268             datatype_instance.append(deepcopy(datatype_infos))
       
   269             self.apply_templates(context, datatype_instance, output_parent)
       
   270             return
       
   271 
       
   272 instances_path_xslt = etree.parse(
       
   273     os.path.join(ScriptDirectory, "plcopen", "instances_path.xslt"))
       
   274 
   252 
   275 #-------------------------------------------------------------------------------
   253 #-------------------------------------------------------------------------------
   276 #            Helpers object for generating instance tagname
   254 #            Helpers object for generating instance tagname
   277 #-------------------------------------------------------------------------------
   255 #-------------------------------------------------------------------------------
   278 
   256 
   793         return None
   771         return None
   794 
   772 
   795     def GetInstanceList(self, root, name, debug = False):
   773     def GetInstanceList(self, root, name, debug = False):
   796         project = self.GetProject(debug)
   774         project = self.GetProject(debug)
   797         if project is not None:
   775         if project is not None:
       
   776             instances = []
       
   777             factory = InstancesPathFactory(instances)
       
   778             
       
   779             parser = etree.XMLParser()
       
   780             parser.resolvers.add(LibraryResolver(self, debug))
       
   781             
   798             instances_path_xslt_tree = etree.XSLT(
   782             instances_path_xslt_tree = etree.XSLT(
   799                 instances_path_xslt, 
   783                 etree.parse(
       
   784                     os.path.join(ScriptDirectory, "plcopen", "instances_path.xslt"),
       
   785                     parser), 
   800                 extensions = {
   786                 extensions = {
   801                     ("instances_ns", "instance_definition"): 
   787                     ("instances_ns", "AddInstance"): factory.AddInstance})
   802                     InstanceDefinition(self, debug)})
       
   803             
   788             
   804             return instances_path_xslt_tree(root, 
   789             instances_path_xslt_tree(root, 
   805                 instance_type=etree.XSLT.strparam(name)).getroot()
   790                 instance_type=etree.XSLT.strparam(name))
       
   791             
       
   792             if len(instances) > 0:
       
   793                 return instances
       
   794         
   806         return None
   795         return None
   807 
   796 
   808     def SearchPouInstances(self, tagname, debug = False):
   797     def SearchPouInstances(self, tagname, debug = False):
   809         project = self.GetProject(debug)
   798         project = self.GetProject(debug)
   810         if project is not None:
   799         if project is not None:
   811             words = tagname.split("::")
   800             words = tagname.split("::")
   812             if words[0] == "P":
   801             if words[0] == "P":
   813                 result = self.GetInstanceList(project, words[1])
   802                 return self.GetInstanceList(project, words[1])
   814                 if result is not None:
       
   815                     return [instance.get("path") for instance in result]
       
   816                 return []
       
   817             elif words[0] == 'C':
   803             elif words[0] == 'C':
   818                 return [words[1]]
   804                 return [words[1]]
   819             elif words[0] == 'R':
   805             elif words[0] == 'R':
   820                 return ["%s.%s" % (words[1], words[2])]
   806                 return ["%s.%s" % (words[1], words[2])]
   821             elif words[0] in ['T', 'A']:
   807             elif words[0] in ['T', 'A']: