PLCControler.py
changeset 1939 db478d17bc3a
parent 1937 986dbad48ab7
child 1940 8dc4ebc97777
equal deleted inserted replaced
1938:87136f8697e3 1939:db478d17bc3a
   265                 *(_translate_args(
   265                 *(_translate_args(
   266                     [_StringValue, class_extraction, _StringValue] +
   266                     [_StringValue, class_extraction, _StringValue] +
   267                     [_BoolValue] * 2, args) + [[]])))
   267                     [_BoolValue] * 2, args) + [[]])))
   268 
   268 
   269 
   269 
   270 class InstancesPathCollector(object):
   270 class XSLTModelQuery(object):
       
   271     """ a class to handle XSLT queries on project and libs """
       
   272     def __init__(self, controller, xsltpath, ext = []):
       
   273         # arbitrary set debug to false, updated later
       
   274         self.debug = False
       
   275 
       
   276         # merge xslt extensions for library access to query specific ones
       
   277         xsltext = [
       
   278             ("GetProject", lambda *_ignored: 
       
   279                 controller.GetProject(self.debug)),
       
   280             ("GetStdLibs", lambda *_ignored: 
       
   281                 [lib for lib in StdBlckLibs.values()]),
       
   282             ("GetExtensions", lambda *_ignored: 
       
   283                 [ctn["types"] for ctn in controller.ConfNodeTypes])
       
   284         ] + ext
       
   285 
       
   286         # parse and compile. "beremiz" arbitrary namespace for extensions 
       
   287         self.xslt = etree.XSLT(
       
   288             etree.parse(
       
   289                 os.path.join(ScriptDirectory, "plcopen", xsltpath),
       
   290                 etree.XMLParser()),
       
   291             extensions={ ("beremiz", name):call for name, call in xsltext})
       
   292 
       
   293     def _process_xslt(self, root, debug, **kwargs):
       
   294         self.debug = debug
       
   295         return self.xslt(root,**{k:etree.XSLT.strparam(v) for k,v in kwargs.iteritems()})
       
   296 
       
   297 class InstancesPathCollector(XSLTModelQuery):
   271     """ object for collecting instances path list"""
   298     """ object for collecting instances path list"""
   272     def __init__(self, controller):
   299     def __init__(self, controller):
   273         self.Instances = []
   300         self.Instances = []
   274         self.controller = controller
   301         XSLTModelQuery.__init__(self,
   275         parser = etree.XMLParser()
   302                                 controller,
   276         # arbitrary set debug to false, updated later
   303                                 "instances_path.xslt",
   277         self.debug = False
   304                                 [("AddInstance", self.AddInstance)])
   278 
       
   279         # TODO compile XSLT once for all at __init__
       
   280         self.instances_path_xslt_tree = etree.XSLT(
       
   281             etree.parse(
       
   282                 os.path.join(ScriptDirectory, "plcopen", "instances_path.xslt"),
       
   283                 parser),
       
   284             extensions={
       
   285                 ("instances_ns", "AddInstance"): self.AddInstance,
       
   286                 ("instances_ns", "GetProject"): self.GetProject,
       
   287                 ("instances_ns", "GetStdLibs"): self.GetStdLibs,
       
   288                 ("instances_ns", "GetExtensions"): self.GetExtensions})
       
   289 
   305 
   290     def AddInstance(self, context, *args):
   306     def AddInstance(self, context, *args):
   291         self.Instances.append(args[0][0])
   307         self.Instances.append(args[0][0])
   292 
   308 
   293     def GetProject(self, context, *args):
       
   294         return self.controller.GetProject(self.debug)
       
   295 
       
   296     def GetStdLibs(self, context, *args):
       
   297         return [lib for lib in StdBlckLibs.values()]
       
   298 
       
   299     def GetExtensions(self, context, *args):
       
   300         return [ctn["types"] for ctn in self.controller.ConfNodeTypes]
       
   301 
       
   302     def Collect(self, root, name, debug):
   309     def Collect(self, root, name, debug):
   303         self.debug = debug
   310         self._process_xslt(root, debug, instance_type = name)
   304         self.instances_path_xslt_tree(
       
   305             root, instance_type=etree.XSLT.strparam(name))
       
   306         res = self.Instances
   311         res = self.Instances
   307         self.Instances = []
   312         self.Instances = []
   308         return res
   313         return res
   309 
       
   310 
   314 
   311 class InstanceTagName(object):
   315 class InstanceTagName(object):
   312     """Helpers object for generating instance tagname"""
   316     """Helpers object for generating instance tagname"""
   313 
   317 
   314     def __init__(self, controller):
   318     def __init__(self, controller):
   835                 return factory.GetRoot()
   839                 return factory.GetRoot()
   836 
   840 
   837         return None
   841         return None
   838 
   842 
   839     def GetInstanceList(self, root, name, debug=False):
   843     def GetInstanceList(self, root, name, debug=False):
   840         project = self.GetProject(debug)
   844         return self.InstancesPathCollector.Collect(root, name, debug)
   841         if project is not None:
       
   842             return self.InstancesPathCollector.Collect(root, name, debug)
       
   843         return []
       
   844 
   845 
   845     def SearchPouInstances(self, tagname, debug=False):
   846     def SearchPouInstances(self, tagname, debug=False):
   846         project = self.GetProject(debug)
   847         project = self.GetProject(debug)
   847         if project is not None:
   848         if project is not None:
   848             words = tagname.split("::")
   849             words = tagname.split("::")