Edouard@1951: #!/usr/bin/env python
Edouard@1951: # -*- coding: utf-8 -*-
Edouard@1951: # This file is part of Beremiz.
Edouard@1951: # See COPYING file for copyrights details.
Edouard@1951: 
Edouard@1951: from __future__ import absolute_import
Edouard@1951: from plcopen.XSLTModelQuery import XSLTModelQuery, _StringValue, _BoolValue, _translate_args
Edouard@1951: 
Edouard@1951: # -------------------------------------------------------------------------------
Edouard@1951: #                 Helpers object for generating pou var list
Edouard@1951: # -------------------------------------------------------------------------------
Edouard@1951: 
Edouard@1953: 
Edouard@1951: class _VariableInfos(object):
Edouard@1951:     __slots__ = ["Name", "Class", "Option", "Location", "InitialValue",
Edouard@1951:                  "Edit", "Documentation", "Type", "Tree", "Number"]
Edouard@1951: 
Edouard@1951:     def __init__(self, *args):
Edouard@1951:         for attr, value in zip(self.__slots__, args):
Edouard@1951:             setattr(self, attr, value if value is not None else "")
Edouard@1951: 
Edouard@1951:     def copy(self):
Edouard@1951:         return _VariableInfos(*[getattr(self, attr) for attr in self.__slots__])
Edouard@1951: 
Edouard@1951: 
Edouard@1951: class VariablesInfosFactory(object):
Edouard@1951:     """ Helpers object for generating pou var list """
Edouard@1951: 
Edouard@1951:     def __init__(self, variables):
Edouard@1951:         self.Variables = variables
Edouard@1951:         self.TreeStack = []
Edouard@1951:         self.Type = None
Edouard@1951:         self.Dimensions = None
Edouard@1951: 
Edouard@1951:     def SetType(self, context, *args):
Edouard@1951:         self.Type = args[0][0]
Edouard@1951: 
Edouard@1951:     def GetType(self):
Edouard@1951:         if len(self.Dimensions) > 0:
Edouard@1951:             return ("array", self.Type, self.Dimensions)
Edouard@1951:         return self.Type
Edouard@1951: 
Edouard@1951:     def GetTree(self):
Edouard@1951:         return (self.TreeStack.pop(-1), self.Dimensions)
Edouard@1951: 
Edouard@1951:     def AddDimension(self, context, *args):
Edouard@1951:         self.Dimensions.append(tuple(
Edouard@1951:             _translate_args([_StringValue] * 2, args)))
Edouard@1951: 
Edouard@1951:     def AddTree(self, context, *args):
Edouard@1951:         self.TreeStack.append([])
Edouard@1951:         self.Dimensions = []
Edouard@1951: 
Edouard@1951:     def AddVarToTree(self, context, *args):
Edouard@1951:         var = (args[0][0], self.Type, self.GetTree())
Edouard@1951:         self.TreeStack[-1].append(var)
Edouard@1951: 
Edouard@1951:     def AddVariable(self, context, *args):
Edouard@1951:         self.Variables.append(_VariableInfos(*(
Edouard@1951:             _translate_args([_StringValue] * 5 + [_BoolValue] + [_StringValue], args) +
Edouard@1951:             [self.GetType(), self.GetTree()])))
Edouard@1951: 
Edouard@1951: 
Edouard@1951: class VariableInfoCollector(XSLTModelQuery):
Edouard@1951:     def __init__(self, controller):
Edouard@1951:         XSLTModelQuery.__init__(self,
Edouard@1951:                                 controller,
Edouard@1951:                                 "variables_infos.xslt",
Edouard@1953:                                 [(name, self.FactoryCaller(name))
Edouard@1953:                                  for name in [
Edouard@1953:                                      "SetType",
Edouard@1953:                                      "AddDimension",
Edouard@1953:                                      "AddTree",
Edouard@1953:                                      "AddVarToTree",
Edouard@1953:                                      "AddVariable"]])
Edouard@1951: 
Edouard@1951:     def FactoryCaller(self, funcname):
Edouard@1951:         def CallFactory(*args):
Edouard@1951:             return getattr(self.factory, funcname)(*args)
Edouard@1951:         return CallFactory
Edouard@1951: 
Edouard@1951:     def Collect(self, root, debug, variables, tree):
Edouard@1951:         self.factory = VariablesInfosFactory(variables)
Edouard@1951:         self._process_xslt(root, debug, tree=str(tree))
Edouard@1951:         res = self.factory
Edouard@1951:         self.factory = None
Edouard@1951:         return res