XSLTransform.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Fri, 20 Nov 2020 11:17:40 +0100
changeset 2696 9bd639e9124e
parent 2627 3ba6a2d26507
child 2707 c26195654ae9
permissions -rw-r--r--
Project tree: avoid flickering and glitches in notebook when selecting some still not opened ConfigTreeNode
2627
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
     3
# This file is part of Beremiz.
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
     4
# See COPYING file for copyrights details.
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
     5
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
     6
from __future__ import absolute_import
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
     7
from lxml import etree
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
     8
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
     9
class XSLTransform(object):
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    10
    """ a class to handle XSLT queries on project and libs """
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    11
    def __init__(self, xsltpath, xsltext):
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    12
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    13
        # parse and compile. "beremiz" arbitrary namespace for extensions
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    14
        self.xslt = etree.XSLT(
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    15
            etree.parse(
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    16
                xsltpath,
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    17
                etree.XMLParser()),
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    18
            extensions={("beremiz", name): call for name, call in xsltext})
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    19
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    20
    def transform(self, root, **kwargs):
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    21
        res = self.xslt(root, **{k: etree.XSLT.strparam(v) for k, v in kwargs.iteritems()})
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    22
        # print(self.xslt.error_log)
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    23
        return res
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    24
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents:
diff changeset
    25