Moved XSLT model query python code so that XSLT part can be reused for other transformations
authorEdouard Tisserant
Fri, 09 Aug 2019 12:07:33 +0200
changeset 2627 3ba6a2d26507
parent 2626 39d78c530cbb
child 2628 40c4449001df
Moved XSLT model query python code so that XSLT part can be reused for other transformations
XSLTransform.py
plcopen/Makefile
plcopen/XSLTModelQuery.py
plcopen/yslt_noindent.yml2
yslt_noindent.yml2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/XSLTransform.py	Fri Aug 09 12:07:33 2019 +0200
@@ -0,0 +1,25 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# This file is part of Beremiz.
+# See COPYING file for copyrights details.
+
+from __future__ import absolute_import
+from lxml import etree
+
+class XSLTransform(object):
+    """ a class to handle XSLT queries on project and libs """
+    def __init__(self, xsltpath, xsltext):
+
+        # parse and compile. "beremiz" arbitrary namespace for extensions
+        self.xslt = etree.XSLT(
+            etree.parse(
+                xsltpath,
+                etree.XMLParser()),
+            extensions={("beremiz", name): call for name, call in xsltext})
+
+    def transform(self, root, **kwargs):
+        res = self.xslt(root, **{k: etree.XSLT.strparam(v) for k, v in kwargs.iteritems()})
+        # print(self.xslt.error_log)
+        return res
+
+
--- a/plcopen/Makefile	Tue Sep 24 11:55:59 2019 +0200
+++ b/plcopen/Makefile	Fri Aug 09 12:07:33 2019 +0200
@@ -1,13 +1,23 @@
 #! gmake
 
-yml := ../../yml2
+# Makefile to generate XSLT stylesheets from ysl2 files in the same directory
+
+# This uses YML2. 
+#   hg clone https://pep.foundation/dev/repos/yml2/
+
+# It should be just fine if yml2 is cloned just asside beremiz
+# otherwise, point yml2path to yml2 source directory
+#   make yml2path=path/to/yml/dir
+
+yml2path ?= $(abspath ../../yml2)
+
 ysl2files := $(wildcard *.ysl2)
 xsltfiles := $(patsubst %.ysl2, %.xslt, $(ysl2files))
 
 all:$(xsltfiles)
 
-%.xslt: %.ysl2 yslt_noindent.yml2
-	$(yml)/yml2c -I $(yml) $< -o $@.tmp
+%.xslt: %.ysl2 ../yslt_noindent.yml2
+	$(yml2path)/yml2c -I $(yml2path):../ $< -o $@.tmp
 	xmlstarlet fo $@.tmp > $@
 	rm $@.tmp
 	
--- a/plcopen/XSLTModelQuery.py	Tue Sep 24 11:55:59 2019 +0200
+++ b/plcopen/XSLTModelQuery.py	Fri Aug 09 12:07:33 2019 +0200
@@ -8,13 +8,14 @@
 from lxml import etree
 import util.paths as paths
 from plcopen.structures import StdBlckLibs
+from XSLTransform import XSLTransform
 
 ScriptDirectory = paths.AbsDir(__file__)
 
-
-class XSLTModelQuery(object):
+class XSLTModelQuery(XSLTransform):
     """ a class to handle XSLT queries on project and libs """
     def __init__(self, controller, xsltpath, ext=None):
+
         # arbitrary set debug to false, updated later
         self.debug = False
 
@@ -31,19 +32,12 @@
         if ext is not None:
             xsltext.extend(ext)
 
-        # parse and compile. "beremiz" arbitrary namespace for extensions
-        self.xslt = etree.XSLT(
-            etree.parse(
-                os.path.join(ScriptDirectory, xsltpath),
-                etree.XMLParser()),
-            extensions={("beremiz", name): call for name, call in xsltext})
-
+        XSLTransform.__init__(self,
+                           os.path.join(ScriptDirectory, xsltpath),
+                           xsltext)
     def _process_xslt(self, root, debug, **kwargs):
         self.debug = debug
-        res = self.xslt(root, **{k: etree.XSLT.strparam(v) for k, v in kwargs.iteritems()})
-        # print(self.xslt.error_log)
-        return res
-
+        return self.transform(root, **kwargs)
 
 # -------------------------------------------------------------------------------
 #           Helpers functions for translating list of arguments
--- a/plcopen/yslt_noindent.yml2	Tue Sep 24 11:55:59 2019 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-include yslt.yml2
-
-in xsl {
-    decl istylesheet (
-        *output="xml",
-        version="1.0",
-        xmlns:xsl="http://www.w3.org/1999/XSL/Transform",
-        xmlns:exsl='http://exslt.org/common',
-        extension-element-prefixes='exsl'
-    ) alias stylesheet {
-        output *output;
-        content;
-    };
-
-    decl template(match) {
-        content;
-    };
-
-    decl function(name) alias template {
-        content;
-    };
-   
-    decl call(name) alias call-template {
-        content;
-    };
-
-    decl apply(select) alias apply-templates {
-        content;
-    };
-}
-
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/yslt_noindent.yml2	Fri Aug 09 12:07:33 2019 +0200
@@ -0,0 +1,32 @@
+include yslt.yml2
+
+in xsl {
+    decl istylesheet (
+        *output="xml",
+        version="1.0",
+        xmlns:xsl="http://www.w3.org/1999/XSL/Transform",
+        xmlns:exsl='http://exslt.org/common',
+        extension-element-prefixes='exsl'
+    ) alias stylesheet {
+        output *output;
+        content;
+    };
+
+    decl template(match) {
+        content;
+    };
+
+    decl function(name) alias template {
+        content;
+    };
+   
+    decl call(name) alias call-template {
+        content;
+    };
+
+    decl apply(select) alias apply-templates {
+        content;
+    };
+}
+
+