XSLTransform.py
author Edouard Tisserant
Wed, 18 Dec 2019 13:31:22 +0100
branchsvghmi
changeset 2821 d92d201d22e1
parent 2752 a8c9b7f0a54a
child 2707 c26195654ae9
permissions -rw-r--r--
Add --on-plc-start --on-plc-stop and --status-change to runtime command line, calling given command respectively on start stop or any event. Command line string is python formated (to eventually include status with {}) before being split (supports quoted strings) and passed to Popen for non-blocking execution.
2752
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
     3
# This file is part of Beremiz.
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
     4
# See COPYING file for copyrights details.
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
     5
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
     6
from __future__ import absolute_import
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
     7
from lxml import etree
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
     8
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
     9
class XSLTransform(object):
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    10
    """ a class to handle XSLT queries on project and libs """
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    11
    def __init__(self, xsltpath, xsltext):
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    12
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    13
        # parse and compile. "beremiz" arbitrary namespace for extensions
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    14
        self.xslt = etree.XSLT(
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    15
            etree.parse(
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    16
                xsltpath,
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    17
                etree.XMLParser()),
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    18
            extensions={("beremiz", name): call for name, call in xsltext})
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    19
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    20
    def transform(self, root, **kwargs):
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    21
        res = self.xslt(root, **{k: etree.XSLT.strparam(v) for k, v in kwargs.iteritems()})
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    22
        # print(self.xslt.error_log)
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    23
        return res
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    24
a8c9b7f0a54a Moved XSLT model query python code so that XSLT part can be reused for other transformations (i.e. in SVGHMI)
Edouard Tisserant
parents:
diff changeset
    25