#!/usr/bin/env python3




##################################################
## DEPENDENCIES
import sys
import os
import os.path
try:
    import builtins as builtin
except ImportError:
    import __builtin__ as builtin
from os.path import getmtime, exists
import time
import types
from Cheetah.Version import MinCompatibleVersion as RequiredCheetahVersion
from Cheetah.Version import MinCompatibleVersionTuple as RequiredCheetahVersionTuple
from Cheetah.Template import Template
from Cheetah.DummyTransaction import *
from Cheetah.NameMapper import NotFound, valueForName, valueFromSearchList, valueFromFrameOrSearchList
from Cheetah.CacheRegion import CacheRegion
import Cheetah.Filters as Filters
import Cheetah.ErrorCatchers as ErrorCatchers
from Cheetah.compat import unicode
from xpdeint.Operators._OperatorContainer import _OperatorContainer
from xpdeint.ParserException import ParserException

##################################################
## MODULE CONSTANTS
VFFSL=valueFromFrameOrSearchList
VFSL=valueFromSearchList
VFN=valueForName
currentTime=time.time
__CHEETAH_version__ = '3.2.1'
__CHEETAH_versionTuple__ = (3, 2, 1, 'final', 0)
__CHEETAH_genTime__ = 1556840495.3880143
__CHEETAH_genTimestamp__ = 'Fri May  3 09:41:35 2019'
__CHEETAH_src__ = '/home/mattias/xmds-2.2.3/admin/staging/xmds-2.3.0/xpdeint/Operators/OperatorContainer.tmpl'
__CHEETAH_srcLastModified__ = 'Thu Apr  4 16:29:24 2019'
__CHEETAH_docstring__ = 'Autogenerated by Cheetah: The Python-Powered Template Engine'

if __CHEETAH_versionTuple__ < RequiredCheetahVersionTuple:
    raise AssertionError(
      'This template was compiled with Cheetah version'
      ' %s. Templates compiled before version %s must be recompiled.'%(
         __CHEETAH_version__, RequiredCheetahVersion))

##################################################
## CLASSES

class OperatorContainer(_OperatorContainer):

    ##################################################
    ## CHEETAH GENERATED METHODS


    def __init__(self, *args, **KWs):

        super(OperatorContainer, self).__init__(*args, **KWs)
        if not self._CHEETAH__instanceInitialized:
            cheetahKWArgs = {}
            allowedKWs = 'searchList namespaces filter filtersLib errorCatcher'.split()
            for k,v in KWs.items():
                if k in allowedKWs: cheetahKWArgs[k] = v
            self._initCheetahInstance(**cheetahKWArgs)
        

    def evaluateIPOperators(self, arguments=None, parentFunction=None, **KWs):



        ## CHEETAH: generated from @def evaluateIPOperators(arguments = None, parentFunction = None, **KWs) at line 29, col 1.
        trans = KWs.get("trans")
        if (not trans and not self._CHEETAH__isBuffering and not callable(self.transaction)):
            trans = self.transaction # is None unless self.awake() was called
        if not trans:
            trans = DummyTransaction()
            _dummyTrans = True
        else: _dummyTrans = False
        write = trans.response().write
        SL = self._CHEETAH__searchList
        _filter = self._CHEETAH__currentFilter
        
        ########################################
        ## START - generated method body
        
        if len(VFFSL(SL,"ipOperators",True)) == 0: # generated from line 30, col 3
            return ''
        # 
        basis = VFFSL(SL,"ipOperatorBasis",True)
        targetVectors = set()
        setOfVectorsToLoopOver = set()
        haveMultipleIPOperatorFractions = len(VFFSL(SL,"parent.ipPropagationStepFractions",True)) > 1
        # 
        for ipOperator in VFFSL(SL,"ipOperators",True): # generated from line 39, col 3
            targetVectors.update(ipOperator.targetVectors)
            setOfVectorsToLoopOver.update(ipOperator.targetVectors)
            setOfVectorsToLoopOver.add(ipOperator.operatorVector)
            # 
            if VFFSL(SL,"haveMultipleIPOperatorFractions",True): # generated from line 44, col 5
                write('''unsigned long _''')
                _v = VFFSL(SL,"ipOperator.id",True) # '${ipOperator.id}' on line 45, col 16
                if _v is not None: write(_filter(_v, rawExpr='${ipOperator.id}')) # from line 45, col 16.
                write('''_exponentIndex = ''')
                _v = VFFSL(SL,"ipOperator.exponentIndex",True) # '${ipOperator.exponentIndex}' on line 45, col 49
                if _v is not None: write(_filter(_v, rawExpr='${ipOperator.exponentIndex}')) # from line 45, col 49.
                write(''';
''')
        write('''
''')
        _v = VFFSL(SL,"transformVectorsToBasis",False)(targetVectors, basis) # '${transformVectorsToBasis(targetVectors, basis)}' on line 49, col 1
        if _v is not None: write(_filter(_v, rawExpr='${transformVectorsToBasis(targetVectors, basis)}')) # from line 49, col 1.
        # 
        write('''
''')
        _v = VFFSL(SL,"applyIPOperator",False)(setOfVectorsToLoopOver, basis) # '${applyIPOperator(setOfVectorsToLoopOver, basis)}' on line 52, col 1
        if _v is not None: write(_filter(_v, rawExpr='${applyIPOperator(setOfVectorsToLoopOver, basis)}')) # from line 52, col 1.
        write('''
''')
        # 
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def applyIPOperator(self, setOfVectorsToLoopOver, operatorBasis, **KWS):



        ## CHEETAH: generated from @def applyIPOperator($setOfVectorsToLoopOver, $operatorBasis) at line 58, col 1.
        trans = KWS.get("trans")
        if (not trans and not self._CHEETAH__isBuffering and not callable(self.transaction)):
            trans = self.transaction # is None unless self.awake() was called
        if not trans:
            trans = DummyTransaction()
            _dummyTrans = True
        else: _dummyTrans = False
        write = trans.response().write
        SL = self._CHEETAH__searchList
        _filter = self._CHEETAH__currentFilter
        
        ########################################
        ## START - generated method body
        
        # 
        write('''if (_exponent > 0) {
  ''')
        _v = VFFSL(SL,"doIPLoop",False)(setOfVectorsToLoopOver, operatorBasis, '*') # "${doIPLoop(setOfVectorsToLoopOver, operatorBasis, '*'), autoIndent=True}" on line 61, col 3
        if _v is not None: write(_filter(_v, autoIndent=True, rawExpr="${doIPLoop(setOfVectorsToLoopOver, operatorBasis, '*'), autoIndent=True}")) # from line 61, col 3.
        write('''} else {
  ''')
        _v = VFFSL(SL,"doIPLoop",False)(setOfVectorsToLoopOver, operatorBasis, '/') # "${doIPLoop(setOfVectorsToLoopOver, operatorBasis, '/'), autoIndent=True}" on line 63, col 3
        if _v is not None: write(_filter(_v, autoIndent=True, rawExpr="${doIPLoop(setOfVectorsToLoopOver, operatorBasis, '/'), autoIndent=True}")) # from line 63, col 3.
        write('''}
''')
        # 
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def doIPLoop(self, setOfVectorsToLoopOver, operatorBasis, operation, **KWS):



        ## CHEETAH: generated from @def doIPLoop($setOfVectorsToLoopOver, $operatorBasis, $operation) at line 68, col 1.
        trans = KWS.get("trans")
        if (not trans and not self._CHEETAH__isBuffering and not callable(self.transaction)):
            trans = self.transaction # is None unless self.awake() was called
        if not trans:
            trans = DummyTransaction()
            _dummyTrans = True
        else: _dummyTrans = False
        write = trans.response().write
        SL = self._CHEETAH__searchList
        _filter = self._CHEETAH__currentFilter
        
        ########################################
        ## START - generated method body
        
        # 
        loopingField = VFFSL(SL,"field",True)
        if all(v.field.isEquivalentToField(loopingField) for v in setOfVectorsToLoopOver): # generated from line 71, col 3
            _v = VFFSL(SL,"loopOverVectorsInBasisWithInnerContent",False)(VFFSL(SL,"setOfVectorsToLoopOver",True), VFFSL(SL,"operatorBasis",True), VFFSL(SL,"insideEvaluateOperatorLoops",False)(VFFSL(SL,"operation",True))) # '${loopOverVectorsInBasisWithInnerContent($setOfVectorsToLoopOver, $operatorBasis, $insideEvaluateOperatorLoops($operation))}' on line 72, col 1
            if _v is not None: write(_filter(_v, rawExpr='${loopOverVectorsInBasisWithInnerContent($setOfVectorsToLoopOver, $operatorBasis, $insideEvaluateOperatorLoops($operation))}')) # from line 72, col 1.
        else: # generated from line 73, col 3
            _v = VFFSL(SL,"loopOverFieldInBasisWithVectorsAndInnerContent",False)(VFFSL(SL,"loopingField",True), VFFSL(SL,"operatorBasis",True), VFFSL(SL,"setOfVectorsToLoopOver",True), VFFSL(SL,"insideEvaluateOperatorLoops",False)(VFFSL(SL,"operation",True))) # '${loopOverFieldInBasisWithVectorsAndInnerContent($loopingField, $operatorBasis, $setOfVectorsToLoopOver, $insideEvaluateOperatorLoops($operation))}' on line 74, col 1
            if _v is not None: write(_filter(_v, rawExpr='${loopOverFieldInBasisWithVectorsAndInnerContent($loopingField, $operatorBasis, $setOfVectorsToLoopOver, $insideEvaluateOperatorLoops($operation))}')) # from line 74, col 1.
        # 
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def insideEvaluateOperatorLoops(self, operationString, **KWS):



        ## CHEETAH: generated from @def insideEvaluateOperatorLoops($operationString) at line 79, col 1.
        trans = KWS.get("trans")
        if (not trans and not self._CHEETAH__isBuffering and not callable(self.transaction)):
            trans = self.transaction # is None unless self.awake() was called
        if not trans:
            trans = DummyTransaction()
            _dummyTrans = True
        else: _dummyTrans = False
        write = trans.response().write
        SL = self._CHEETAH__searchList
        _filter = self._CHEETAH__currentFilter
        
        ########################################
        ## START - generated method body
        
        # 
        operationsDict = dict()
        haveMultipleIPOperatorFractions = len(VFFSL(SL,"parent.ipPropagationStepFractions",True)) > 1
        # 
        for ipOperator in VFFSL(SL,"ipOperators",True): # generated from line 84, col 3
            for operatorComponentNumber, (operatorComponentName, operatorComponentVectors) in enumerate(VFN(VFFSL(SL,"ipOperator.operatorComponents",True),"items",False)()): # generated from line 85, col 5
                for targetVectorComponents in operatorComponentVectors.values(): # generated from line 86, col 7
                    for targetVectorComponent in targetVectorComponents: # generated from line 87, col 9
                        ## START CAPTURE REGION: _35818351 operation at line 88, col 11 in the source.
                        _orig_trans_35818351 = trans
                        _wasBuffering_35818351 = self._CHEETAH__isBuffering
                        self._CHEETAH__isBuffering = True
                        trans = _captureCollector_35818351 = DummyTransaction()
                        write = _captureCollector_35818351.response().write
                        write('''/* ''')
                        _v = VFFSL(SL,"operatorComponentName",True) # '${operatorComponentName}' on line 89, col 4
                        if _v is not None: write(_filter(_v, rawExpr='${operatorComponentName}')) # from line 89, col 4.
                        write('''[''')
                        _v = VFFSL(SL,"targetVectorComponent",True) # '$targetVectorComponent' on line 89, col 29
                        if _v is not None: write(_filter(_v, rawExpr='$targetVectorComponent')) # from line 89, col 29.
                        write('''] */ _''')
                        _v = VFFSL(SL,"ipOperator.operatorVector.id",True) # '${ipOperator.operatorVector.id}' on line 89, col 57
                        if _v is not None: write(_filter(_v, rawExpr='${ipOperator.operatorVector.id}')) # from line 89, col 57.
                        write('''[_''')
                        _v = VFFSL(SL,"ipOperator.operatorVector.id",True) # '${ipOperator.operatorVector.id}' on line 89, col 90
                        if _v is not None: write(_filter(_v, rawExpr='${ipOperator.operatorVector.id}')) # from line 89, col 90.
                        write('''_index_pointer + ''')
                        _v = VFFSL(SL,"operatorComponentNumber",True) # '$operatorComponentNumber' on line 89, col 138
                        if _v is not None: write(_filter(_v, rawExpr='$operatorComponentNumber')) # from line 89, col 138.
                        if VFFSL(SL,"haveMultipleIPOperatorFractions",True): # generated from line 90, col 13
                            write(''' + _''')
                            _v = VFFSL(SL,"ipOperator.id",True) # '${ipOperator.id}' on line 91, col 5
                            if _v is not None: write(_filter(_v, rawExpr='${ipOperator.id}')) # from line 91, col 5.
                            write('''_exponentIndex''')
                        write(''']''')
                        trans = _orig_trans_35818351
                        write = trans.response().write
                        self._CHEETAH__isBuffering = _wasBuffering_35818351 
                        operation = _captureCollector_35818351.response().getvalue()
                        del _orig_trans_35818351
                        del _captureCollector_35818351
                        del _wasBuffering_35818351
                        # 
                        operationsDict.setdefault(targetVectorComponent, [])
                        operationsDict[targetVectorComponent].append(operation)
        # 
        for targetVectorComponent, operations in operationsDict.items(): # generated from line 103, col 3
            joinString = '\n' + ' '*len(targetVectorComponent) + ' *  '
            _v = VFFSL(SL,"targetVectorComponent",True) # '${targetVectorComponent}' on line 105, col 1
            if _v is not None: write(_filter(_v, rawExpr='${targetVectorComponent}')) # from line 105, col 1.
            write(''' ''')
            _v = VFFSL(SL,"operationString",True) # '${operationString}' on line 105, col 26
            if _v is not None: write(_filter(_v, rawExpr='${operationString}')) # from line 105, col 26.
            write('''= ''')
            _v = VFN(VFFSL(SL,"joinString",True),"join",False)(operations) # '${joinString.join(operations)}' on line 105, col 46
            if _v is not None: write(_filter(_v, rawExpr='${joinString.join(operations)}')) # from line 105, col 46.
            write(''';
''')
        # 
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def writeBody(self, **KWS):



        ## CHEETAH: main method generated for this template
        trans = KWS.get("trans")
        if (not trans and not self._CHEETAH__isBuffering and not callable(self.transaction)):
            trans = self.transaction # is None unless self.awake() was called
        if not trans:
            trans = DummyTransaction()
            _dummyTrans = True
        else: _dummyTrans = False
        write = trans.response().write
        SL = self._CHEETAH__searchList
        _filter = self._CHEETAH__currentFilter
        
        ########################################
        ## START - generated method body
        
        write('''
''')
        # 
        # OperatorContainer.tmpl
        # 
        # Operator Container
        # 
        # Created by Graham Dennis on 2013-10-02.
        # 
        # Copyright (c) 2013, Graham Dennis
        # 
        # This program is free software: you can redistribute it and/or modify
        # it under the terms of the GNU General Public License as published by
        # the Free Software Foundation, either version 2 of the License, or
        # (at your option) any later version.
        # 
        # This program is distributed in the hope that it will be useful,
        # but WITHOUT ANY WARRANTY; without even the implied warranty of
        # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        # GNU General Public License for more details.
        # 
        # You should have received a copy of the GNU General Public License
        # along with this program.  If not, see <http://www.gnu.org/licenses/>.
        # 
        write('''




''')
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        
    ##################################################
    ## CHEETAH GENERATED ATTRIBUTES


    _CHEETAH__instanceInitialized = False

    _CHEETAH_version = __CHEETAH_version__

    _CHEETAH_versionTuple = __CHEETAH_versionTuple__

    _CHEETAH_genTime = __CHEETAH_genTime__

    _CHEETAH_genTimestamp = __CHEETAH_genTimestamp__

    _CHEETAH_src = __CHEETAH_src__

    _CHEETAH_srcLastModified = __CHEETAH_srcLastModified__

    _mainCheetahMethod_for_OperatorContainer = 'writeBody'

## END CLASS DEFINITION

if not hasattr(OperatorContainer, '_initCheetahAttributes'):
    templateAPIClass = getattr(OperatorContainer,
                               '_CHEETAH_templateClass',
                               Template)
    templateAPIClass._addCheetahPlumbingCodeToClass(OperatorContainer)


# CHEETAH was developed by Tavis Rudd and Mike Orr
# with code, advice and input from many other volunteers.
# For more information visit http://cheetahtemplate.org/

##################################################
## if run from command line:
if __name__ == '__main__':
    from Cheetah.TemplateCmdLineIface import CmdLineIface
    CmdLineIface(templateObj=OperatorContainer()).run()


