Laurent@814: #!/usr/bin/env python Laurent@814: # -*- coding: utf-8 -*- edouard@1943: # This file is part of Beremiz. andrej@1571: # See COPYING file for copyrights details. andrej@1850: andrej@1881: from __future__ import absolute_import edouard@1944: from plcopen.XSLTModelQuery import XSLTModelQuery, _StringValue, _BoolValue, _translate_args edouard@1944: from plcopen.types_enums import CLASS_TYPES, POU_TYPES, VAR_CLASS_INFOS andrej@1736: Edouard@1953: Laurent@1348: def class_extraction(value): edouard@1944: class_type = CLASS_TYPES.get(value) Laurent@1348: if class_type is not None: Laurent@1348: return class_type Edouard@1411: Laurent@1348: pou_type = POU_TYPES.get(value) Laurent@1348: if pou_type is not None: Laurent@1348: return pou_type Edouard@1411: Laurent@1348: var_type = VAR_CLASS_INFOS.get(value) Laurent@1348: if var_type is not None: Laurent@1348: return var_type[1] Edouard@1411: Laurent@1348: return None Laurent@1348: andrej@1736: Laurent@1348: class _VariablesTreeItemInfos(object): Laurent@1348: __slots__ = ["name", "var_class", "type", "edit", "debug", "variables"] andrej@1751: Laurent@1348: def __init__(self, *args): Laurent@1348: for attr, value in zip(self.__slots__, args): Laurent@1348: setattr(self, attr, value if value is not None else "") andrej@1751: Laurent@1348: def copy(self): andrej@1872: return _VariablesTreeItemInfos(*[getattr(self, attr) for attr in self.__slots__]) Laurent@1348: andrej@1736: andrej@1831: class VariablesTreeInfosFactory(object): Edouard@1411: Laurent@1348: def __init__(self): Laurent@1348: self.Root = None Edouard@1411: Laurent@1348: def GetRoot(self): Laurent@1348: return self.Root Edouard@1411: Laurent@1348: def SetRoot(self, context, *args): Laurent@1348: self.Root = _VariablesTreeItemInfos( Laurent@1348: *([''] + _translate_args( Edouard@1411: [class_extraction, _StringValue] + [_BoolValue] * 2, Laurent@1348: args) + [[]])) Laurent@1348: Laurent@1348: def AddVariable(self, context, *args): Laurent@1348: if self.Root is not None: Laurent@1348: self.Root.variables.append(_VariablesTreeItemInfos( Laurent@1348: *(_translate_args( Edouard@1411: [_StringValue, class_extraction, _StringValue] + Laurent@1378: [_BoolValue] * 2, args) + [[]]))) Laurent@1316: andrej@1736: edouard@1943: class POUVariablesCollector(XSLTModelQuery): Laurent@1321: def __init__(self, controller): edouard@1943: XSLTModelQuery.__init__(self, edouard@1943: controller, edouard@1943: "pou_variables.xslt", Edouard@1953: [(name, self.FactoryCaller(name)) Edouard@1953: for name in ["SetRoot", "AddVariable"]]) Edouard@1411: edouard@1945: def FactoryCaller(self, funcname): edouard@1945: def CallFactory(*args): edouard@1945: return getattr(self.factory, funcname)(*args) edouard@1945: return CallFactory edouard@1945: edouard@1943: def Collect(self, root, debug): edouard@1943: self.factory = VariablesTreeInfosFactory() edouard@1943: self._process_xslt(root, debug) edouard@1943: res = self.factory.GetRoot() edouard@1943: self.factory = None edouard@1943: return res