plcopen/VariableInfoCollector.py
author Edouard Tisserant
Tue, 20 Feb 2018 11:24:10 +0100
changeset 1951 bbd1e1744c91
child 1953 5736d25bb393
permissions -rw-r--r--
GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
1951
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
     3
# This file is part of Beremiz.
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
     4
# See COPYING file for copyrights details.
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
     5
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
     6
from __future__ import absolute_import
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
     7
from plcopen.XSLTModelQuery import XSLTModelQuery, _StringValue, _BoolValue, _translate_args
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
     8
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
     9
# -------------------------------------------------------------------------------
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    10
#                 Helpers object for generating pou var list
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    11
# -------------------------------------------------------------------------------
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    12
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    13
class _VariableInfos(object):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    14
    __slots__ = ["Name", "Class", "Option", "Location", "InitialValue",
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    15
                 "Edit", "Documentation", "Type", "Tree", "Number"]
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    16
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    17
    def __init__(self, *args):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    18
        for attr, value in zip(self.__slots__, args):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    19
            setattr(self, attr, value if value is not None else "")
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    20
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    21
    def copy(self):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    22
        return _VariableInfos(*[getattr(self, attr) for attr in self.__slots__])
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    23
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    24
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    25
class VariablesInfosFactory(object):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    26
    """ Helpers object for generating pou var list """
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    27
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    28
    def __init__(self, variables):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    29
        self.Variables = variables
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    30
        self.TreeStack = []
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    31
        self.Type = None
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    32
        self.Dimensions = None
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    33
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    34
    def SetType(self, context, *args):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    35
        self.Type = args[0][0]
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    36
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    37
    def GetType(self):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    38
        if len(self.Dimensions) > 0:
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    39
            return ("array", self.Type, self.Dimensions)
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    40
        return self.Type
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    41
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    42
    def GetTree(self):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    43
        return (self.TreeStack.pop(-1), self.Dimensions)
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    44
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    45
    def AddDimension(self, context, *args):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    46
        self.Dimensions.append(tuple(
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    47
            _translate_args([_StringValue] * 2, args)))
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    48
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    49
    def AddTree(self, context, *args):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    50
        self.TreeStack.append([])
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    51
        self.Dimensions = []
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    52
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    53
    def AddVarToTree(self, context, *args):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    54
        var = (args[0][0], self.Type, self.GetTree())
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    55
        self.TreeStack[-1].append(var)
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    56
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    57
    def AddVariable(self, context, *args):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    58
        self.Variables.append(_VariableInfos(*(
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    59
            _translate_args([_StringValue] * 5 + [_BoolValue] + [_StringValue], args) +
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    60
            [self.GetType(), self.GetTree()])))
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    61
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    62
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    63
class VariableInfoCollector(XSLTModelQuery):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    64
    def __init__(self, controller):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    65
        XSLTModelQuery.__init__(self,
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    66
                                controller,
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    67
                                "variables_infos.xslt",
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    68
                                [(name, self.FactoryCaller(name)) 
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    69
                                    for name in [
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    70
                                        "SetType",
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    71
                                        "AddDimension",
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    72
                                        "AddTree",
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    73
                                        "AddVarToTree",
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    74
                                        "AddVariable"]])
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    75
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    76
    def FactoryCaller(self, funcname):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    77
        def CallFactory(*args):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    78
            return getattr(self.factory, funcname)(*args)
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    79
        return CallFactory
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    80
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    81
    def Collect(self, root, debug, variables, tree):
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    82
        self.factory = VariablesInfosFactory(variables)
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    83
        self._process_xslt(root, debug, tree=str(tree))
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    84
        res = self.factory
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    85
        self.factory = None
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    86
        return res
bbd1e1744c91 GetVariableDictionary and GetPouInterfaceReturnType optimized with new VariableInfoCollector based on XSLTModelQuery. Moved corresponding definitions out of PLCControler.py.
Edouard Tisserant
parents:
diff changeset
    87