XSLTransform.py
author Edouard Tisserant
Thu, 12 Sep 2019 12:56:47 +0200
branchsvghmi
changeset 2774 9857b4b0d979
parent 2752 a8c9b7f0a54a
child 2707 c26195654ae9
permissions -rw-r--r--
SVGHMI: WIP for python<->C data exchange
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