--- a/PLCControler.py Fri Feb 16 03:24:11 2018 +0100
+++ b/PLCControler.py Fri Feb 16 13:00:06 2018 +0100
@@ -267,47 +267,51 @@
[_BoolValue] * 2, args) + [[]])))
-class InstancesPathCollector(object):
+class XSLTModelQuery(object): + """ a class to handle XSLT queries on project and libs """ + def __init__(self, controller, xsltpath, ext = []): + # arbitrary set debug to false, updated later + # merge xslt extensions for library access to query specific ones + ("GetProject", lambda *_ignored: + controller.GetProject(self.debug)), + ("GetStdLibs", lambda *_ignored: + [lib for lib in StdBlckLibs.values()]), + ("GetExtensions", lambda *_ignored: + [ctn["types"] for ctn in controller.ConfNodeTypes]) + # parse and compile. "beremiz" arbitrary namespace for extensions + self.xslt = etree.XSLT( + os.path.join(ScriptDirectory, "plcopen", xsltpath), + extensions={ ("beremiz", name):call for name, call in xsltext}) + def _process_xslt(self, root, debug, **kwargs): + return self.xslt(root,**{k:etree.XSLT.strparam(v) for k,v in kwargs.iteritems()}) +class InstancesPathCollector(XSLTModelQuery): """ object for collecting instances path list"""
def __init__(self, controller):
- self.controller = controller
- parser = etree.XMLParser()
- # arbitrary set debug to false, updated later
- # TODO compile XSLT once for all at __init__
- self.instances_path_xslt_tree = etree.XSLT(
- os.path.join(ScriptDirectory, "plcopen", "instances_path.xslt"),
- ("instances_ns", "AddInstance"): self.AddInstance,
- ("instances_ns", "GetProject"): self.GetProject,
- ("instances_ns", "GetStdLibs"): self.GetStdLibs,
- ("instances_ns", "GetExtensions"): self.GetExtensions})
+ XSLTModelQuery.__init__(self, + [("AddInstance", self.AddInstance)]) def AddInstance(self, context, *args):
self.Instances.append(args[0][0])
- def GetProject(self, context, *args):
- return self.controller.GetProject(self.debug)
- def GetStdLibs(self, context, *args):
- return [lib for lib in StdBlckLibs.values()]
- def GetExtensions(self, context, *args):
- return [ctn["types"] for ctn in self.controller.ConfNodeTypes]
def Collect(self, root, name, debug):
- self.instances_path_xslt_tree(
- root, instance_type=etree.XSLT.strparam(name))
+ self._process_xslt(root, debug, instance_type = name)
class InstanceTagName(object):
"""Helpers object for generating instance tagname"""
@@ -837,10 +841,7 @@
def GetInstanceList(self, root, name, debug=False):
- project = self.GetProject(debug)
- if project is not None:
- return self.InstancesPathCollector.Collect(root, name, debug)
+ return self.InstancesPathCollector.Collect(root, name, debug) def SearchPouInstances(self, tagname, debug=False):
project = self.GetProject(debug)
--- a/plcopen/instances_path.xslt Fri Feb 16 03:24:11 2018 +0100
+++ b/plcopen/instances_path.xslt Fri Feb 16 13:00:06 2018 +0100
@@ -1,5 +1,5 @@
-<xsl:stylesheet xmlns:exsl="http://exslt.org/common" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:ppx="http://www.plcopen.org/xml/tc6_0201" xmlns:ns="instances_ns" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" extension-element-prefixes="ns" version="1.0" exclude-result-prefixes="ns">
+<xsl:stylesheet xmlns:exsl="http://exslt.org/common" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:ppx="http://www.plcopen.org/xml/tc6_0201" xmlns:ns="beremiz" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" extension-element-prefixes="ns" version="1.0" exclude-result-prefixes="ns"> <xsl:output method="xml"/>
<xsl:param name="instance_type"/>
<xsl:template match="text()"/>
--- a/plcopen/instances_path.ysl2 Fri Feb 16 03:24:11 2018 +0100
+++ b/plcopen/instances_path.ysl2 Fri Feb 16 13:00:06 2018 +0100
@@ -1,7 +1,7 @@
include yslt_noindent.yml2
istylesheet xmlns:ppx="http://www.plcopen.org/xml/tc6_0201"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
- xmlns:ns="instances_ns"
extension-element-prefixes="ns"
exclude-result-prefixes="ns" {
@@ -17,6 +17,7 @@
variable "all", "$project | $stdlib | $extensions";
apply "ppx:instances/ppx:configurations/ppx:configuration";