plcopen/POUVariablesCollector.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Sat, 17 Feb 2018 23:58:47 +0100
changeset 1943 9dc0e38552b2
parent 1942 PLCControler.py@a4382ae1ba82
child 1944 6162e34fb246
permissions -rw-r--r--
GetPouVariables optimized with XSLTModelQuery
814
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     1
#!/usr/bin/env python
5743cbdff669 Integration of PLCOpenEditor into Beremiz
Laurent Bessard
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
1943
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
     3
# This file is part of Beremiz.
1571
486f94a8032c fix license notices in source files and license files under GPLv2+
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1508
diff changeset
     4
# See COPYING file for copyrights details.
1850
614396cbffbf fix pylint warning '(unused-import), Unused import connectors'
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1847
diff changeset
     5
1881
091005ec69c4 fix pylint py3k conversion warning: "(no-absolute-import) import missing `from __future__ import absolute_import`"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1879
diff changeset
     6
from __future__ import absolute_import
1943
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
     7
from plcopen.XSLTModelQuery import XSLTModelQuery
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
     8
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
     9
def class_extraction(value):
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    10
    class_type = {
1316
df9d02bd3eb7 Replaced old pou instance variable list generating process by xslt stylesheet
Laurent Bessard
parents: 1315
diff changeset
    11
        "configuration": ITEM_CONFIGURATION,
df9d02bd3eb7 Replaced old pou instance variable list generating process by xslt stylesheet
Laurent Bessard
parents: 1315
diff changeset
    12
        "resource": ITEM_RESOURCE,
df9d02bd3eb7 Replaced old pou instance variable list generating process by xslt stylesheet
Laurent Bessard
parents: 1315
diff changeset
    13
        "action": ITEM_ACTION,
df9d02bd3eb7 Replaced old pou instance variable list generating process by xslt stylesheet
Laurent Bessard
parents: 1315
diff changeset
    14
        "transition": ITEM_TRANSITION,
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    15
        "program": ITEM_PROGRAM}.get(value)
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    16
    if class_type is not None:
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    17
        return class_type
1411
805d13d216c0 Fixed POU paste exception
Edouard Tisserant
parents: 1406
diff changeset
    18
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    19
    pou_type = POU_TYPES.get(value)
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    20
    if pou_type is not None:
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    21
        return pou_type
1411
805d13d216c0 Fixed POU paste exception
Edouard Tisserant
parents: 1406
diff changeset
    22
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    23
    var_type = VAR_CLASS_INFOS.get(value)
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    24
    if var_type is not None:
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    25
        return var_type[1]
1411
805d13d216c0 Fixed POU paste exception
Edouard Tisserant
parents: 1406
diff changeset
    26
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    27
    return None
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    28
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
    29
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    30
class _VariablesTreeItemInfos(object):
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    31
    __slots__ = ["name", "var_class", "type", "edit", "debug", "variables"]
1751
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
    32
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    33
    def __init__(self, *args):
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    34
        for attr, value in zip(self.__slots__, args):
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    35
            setattr(self, attr, value if value is not None else "")
1751
c28db6f7616b clean-up: fix PEP8 E301 expected 1 blank line, found 0
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1749
diff changeset
    36
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    37
    def copy(self):
1872
866fb3ab8778 fix pylint error "(undefined-variable) Undefined variable 'X'"
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1852
diff changeset
    38
        return _VariablesTreeItemInfos(*[getattr(self, attr) for attr in self.__slots__])
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    39
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
    40
1831
56b48961cc68 fix (old-style-class) Old-style class defined error for most parts of
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1785
diff changeset
    41
class VariablesTreeInfosFactory(object):
1411
805d13d216c0 Fixed POU paste exception
Edouard Tisserant
parents: 1406
diff changeset
    42
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    43
    def __init__(self):
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    44
        self.Root = None
1411
805d13d216c0 Fixed POU paste exception
Edouard Tisserant
parents: 1406
diff changeset
    45
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    46
    def GetRoot(self):
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    47
        return self.Root
1411
805d13d216c0 Fixed POU paste exception
Edouard Tisserant
parents: 1406
diff changeset
    48
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    49
    def SetRoot(self, context, *args):
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    50
        self.Root = _VariablesTreeItemInfos(
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    51
            *([''] + _translate_args(
1411
805d13d216c0 Fixed POU paste exception
Edouard Tisserant
parents: 1406
diff changeset
    52
                [class_extraction, _StringValue] + [_BoolValue] * 2,
1348
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    53
                args) + [[]]))
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    54
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    55
    def AddVariable(self, context, *args):
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    56
        if self.Root is not None:
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    57
            self.Root.variables.append(_VariablesTreeItemInfos(
aee0a7eb833a Fixed pou variables instance information loading stylesheet
Laurent Bessard
parents: 1347
diff changeset
    58
                *(_translate_args(
1411
805d13d216c0 Fixed POU paste exception
Edouard Tisserant
parents: 1406
diff changeset
    59
                    [_StringValue, class_extraction, _StringValue] +
1378
cbc0f64a25eb Fixed bug with non-ascii characters in program comments
Laurent Bessard
parents: 1373
diff changeset
    60
                    [_BoolValue] * 2, args) + [[]])))
1316
df9d02bd3eb7 Replaced old pou instance variable list generating process by xslt stylesheet
Laurent Bessard
parents: 1315
diff changeset
    61
1736
7e61baa047f0 clean-up: fix PEP8 E302 expected 2 blank lines, found 1
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1735
diff changeset
    62
1321
83f41ea00b97 Replaced old pou instance type tagname computing by xslt stylesheet
Laurent Bessard
parents: 1319
diff changeset
    63
1943
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    64
class POUVariablesCollector(XSLTModelQuery):
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    65
    """ object for collecting instances path list"""
1321
83f41ea00b97 Replaced old pou instance type tagname computing by xslt stylesheet
Laurent Bessard
parents: 1319
diff changeset
    66
    def __init__(self, controller):
1943
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    67
        XSLTModelQuery.__init__(self,
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    68
                                controller,
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    69
                                "pou_variables.xslt",
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    70
                                [(name, lambda *x : getattr(self.factory, name)(*x)) 
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    71
                                    for name in ["SetRoot", "AddVariable"]])
1411
805d13d216c0 Fixed POU paste exception
Edouard Tisserant
parents: 1406
diff changeset
    72
1943
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    73
    def Collect(self, root, debug):
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    74
        self.factory = VariablesTreeInfosFactory()
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    75
        self._process_xslt(root, debug)
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    76
        res = self.factory.GetRoot()
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    77
        self.factory = None
9dc0e38552b2 GetPouVariables optimized with XSLTModelQuery
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 1942
diff changeset
    78
        return res
1411
805d13d216c0 Fixed POU paste exception
Edouard Tisserant
parents: 1406
diff changeset
    79