plcopen/POUVariablesCollector.py
changeset 1943 9dc0e38552b2
parent 1942 a4382ae1ba82
child 1944 6162e34fb246
equal deleted inserted replaced
1942:a4382ae1ba82 1943:9dc0e38552b2
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 # This file is part of Beremiz.
       
     4 # See COPYING file for copyrights details.
       
     5 
       
     6 from __future__ import absolute_import
       
     7 from plcopen.XSLTModelQuery import XSLTModelQuery
       
     8 
       
     9 def class_extraction(value):
       
    10     class_type = {
       
    11         "configuration": ITEM_CONFIGURATION,
       
    12         "resource": ITEM_RESOURCE,
       
    13         "action": ITEM_ACTION,
       
    14         "transition": ITEM_TRANSITION,
       
    15         "program": ITEM_PROGRAM}.get(value)
       
    16     if class_type is not None:
       
    17         return class_type
       
    18 
       
    19     pou_type = POU_TYPES.get(value)
       
    20     if pou_type is not None:
       
    21         return pou_type
       
    22 
       
    23     var_type = VAR_CLASS_INFOS.get(value)
       
    24     if var_type is not None:
       
    25         return var_type[1]
       
    26 
       
    27     return None
       
    28 
       
    29 
       
    30 class _VariablesTreeItemInfos(object):
       
    31     __slots__ = ["name", "var_class", "type", "edit", "debug", "variables"]
       
    32 
       
    33     def __init__(self, *args):
       
    34         for attr, value in zip(self.__slots__, args):
       
    35             setattr(self, attr, value if value is not None else "")
       
    36 
       
    37     def copy(self):
       
    38         return _VariablesTreeItemInfos(*[getattr(self, attr) for attr in self.__slots__])
       
    39 
       
    40 
       
    41 class VariablesTreeInfosFactory(object):
       
    42 
       
    43     def __init__(self):
       
    44         self.Root = None
       
    45 
       
    46     def GetRoot(self):
       
    47         return self.Root
       
    48 
       
    49     def SetRoot(self, context, *args):
       
    50         self.Root = _VariablesTreeItemInfos(
       
    51             *([''] + _translate_args(
       
    52                 [class_extraction, _StringValue] + [_BoolValue] * 2,
       
    53                 args) + [[]]))
       
    54 
       
    55     def AddVariable(self, context, *args):
       
    56         if self.Root is not None:
       
    57             self.Root.variables.append(_VariablesTreeItemInfos(
       
    58                 *(_translate_args(
       
    59                     [_StringValue, class_extraction, _StringValue] +
       
    60                     [_BoolValue] * 2, args) + [[]])))
       
    61 
       
    62 
       
    63 
       
    64 class POUVariablesCollector(XSLTModelQuery):
       
    65     """ object for collecting instances path list"""
       
    66     def __init__(self, controller):
       
    67         XSLTModelQuery.__init__(self,
       
    68                                 controller,
       
    69                                 "pou_variables.xslt",
       
    70                                 [(name, lambda *x : getattr(self.factory, name)(*x)) 
       
    71                                     for name in ["SetRoot", "AddVariable"]])
       
    72 
       
    73     def Collect(self, root, debug):
       
    74         self.factory = VariablesTreeInfosFactory()
       
    75         self._process_xslt(root, debug)
       
    76         res = self.factory.GetRoot()
       
    77         self.factory = None
       
    78         return res
       
    79