plcopen/XSLTModelQuery.py
author Edouard Tisserant
Wed, 21 Sep 2022 11:41:28 +0200
changeset 3622 98ce26040117
parent 2627 3ba6a2d26507
child 3750 f62625418bff
permissions -rw-r--r--
SVGHMI: do not discard out-of-page elements inside widgets that are ovelapping pages.
1940
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/env python
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     3
# This file is part of Beremiz.
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     4
# See COPYING file for copyrights details.
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     5
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     6
from __future__ import absolute_import
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     7
import os
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     8
from lxml import etree
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     9
import util.paths as paths
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
from plcopen.structures import StdBlckLibs
2627
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents: 1953
diff changeset
    11
from XSLTransform import XSLTransform
1940
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    13
ScriptDirectory = paths.AbsDir(__file__)
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    14
2627
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents: 1953
diff changeset
    15
class XSLTModelQuery(XSLTransform):
1940
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    16
    """ a class to handle XSLT queries on project and libs """
1953
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1945
diff changeset
    17
    def __init__(self, controller, xsltpath, ext=None):
2627
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents: 1953
diff changeset
    18
1940
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    19
        # arbitrary set debug to false, updated later
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    20
        self.debug = False
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    21
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    22
        # merge xslt extensions for library access to query specific ones
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    23
        xsltext = [
1953
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1945
diff changeset
    24
            ("GetProject", lambda *_ignored:
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1945
diff changeset
    25
             [controller.GetProject(self.debug)]),
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1945
diff changeset
    26
            ("GetStdLibs", lambda *_ignored:
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1945
diff changeset
    27
             [lib for lib in StdBlckLibs.values()]),
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1945
diff changeset
    28
            ("GetExtensions", lambda *_ignored:
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1945
diff changeset
    29
             [ctn["types"] for ctn in controller.ConfNodeTypes])
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1945
diff changeset
    30
        ]
1940
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    31
1953
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1945
diff changeset
    32
        if ext is not None:
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1945
diff changeset
    33
            xsltext.extend(ext)
5736d25bb393 PEP8 and PyLint conformance: whitespaces and stuff
Edouard Tisserant
parents: 1945
diff changeset
    34
2627
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents: 1953
diff changeset
    35
        XSLTransform.__init__(self,
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents: 1953
diff changeset
    36
                           os.path.join(ScriptDirectory, xsltpath),
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents: 1953
diff changeset
    37
                           xsltext)
1940
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
    def _process_xslt(self, root, debug, **kwargs):
8dc4ebc97777 Moved XSLTModelQuery class to own module in plcopen package.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
        self.debug = debug
2627
3ba6a2d26507 Moved XSLT model query python code so that XSLT part can be reused for other transformations
Edouard Tisserant
parents: 1953
diff changeset
    40
        return self.transform(root, **kwargs)
1944
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    41
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    42
# -------------------------------------------------------------------------------
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    43
#           Helpers functions for translating list of arguments
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    44
#                       from xslt to valid arguments
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    45
# -------------------------------------------------------------------------------
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    46
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    47
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    48
def _StringValue(x):
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    49
    return x
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    50
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    51
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    52
def _BoolValue(x):
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    53
    return x in ["true", "0"]
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    54
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    55
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    56
def _translate_args(translations, args):
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    57
    return [translate(arg[0]) if len(arg) > 0 else None
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    58
            for translate, arg in
6162e34fb246 Moved some code from PLCController.py to other modules. Added necessary imports.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1943
diff changeset
    59
            zip(translations, args)]