XSLTransform.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 20 Oct 2020 00:23:52 +0200
changeset 2692 d4bede6cd3f1
parent 2627 3ba6a2d26507
child 2707 c26195654ae9
permissions -rw-r--r--
PY_EXT: OnChange is now a coma separated list of callables. Updated tests/python accordingly.
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