XSLTransform.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Tue, 16 Feb 2021 09:38:28 +0100
branchsvghmi
changeset 3144 2af6afaccaf2
parent 2834 6ac6a9dff594
child 3165 2db69e2c5673
permissions -rw-r--r--
SVGHMI: i18n: ensure langs always appea in same order, and add two variables that are updated automatically when selecting a new language : lang_name and lang_code. Also fixed i18n startup, prevent wrong lang numer to crash loading.
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
2834
6ac6a9dff594 SVGHMI: be a bit more tolerant with missing HMI paths or missing elements in widgets : continue build (with warning) and fail silently at runtime.
Edouard Tisserant
parents: 2752
diff changeset
    25
    def get_error_log(self):
6ac6a9dff594 SVGHMI: be a bit more tolerant with missing HMI paths or missing elements in widgets : continue build (with warning) and fail silently at runtime.
Edouard Tisserant
parents: 2752
diff changeset
    26
        return self.xslt.error_log
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
    27
2834
6ac6a9dff594 SVGHMI: be a bit more tolerant with missing HMI paths or missing elements in widgets : continue build (with warning) and fail silently at runtime.
Edouard Tisserant
parents: 2752
diff changeset
    28