#!/usr/bin/env python




##################################################
## 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 xpdeint.Operators._OperatorContainer import _OperatorContainer
from xpdeint.ParserException import ParserException

##################################################
## MODULE CONSTANTS
VFFSL=valueFromFrameOrSearchList
VFSL=valueFromSearchList
VFN=valueForName
currentTime=time.time
__CHEETAH_version__ = '2.4.4'
__CHEETAH_versionTuple__ = (2, 4, 4, 'development', 0)
__CHEETAH_genTime__ = 1484975071.969837
__CHEETAH_genTimestamp__ = 'Sat Jan 21 16:04:31 2017'
__CHEETAH_src__ = '/home/mattias/xmds-2.2.3/admin/staging/xmds-2.2.3/xpdeint/Operators/OperatorContainer.tmpl'
__CHEETAH_srcLastModified__ = 'Thu Oct  3 21:15:17 2013'
__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 27, 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 28, 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 37, col 3
            targetVectors.update(ipOperator.targetVectors)
            setOfVectorsToLoopOver.update(ipOperator.targetVectors)
            setOfVectorsToLoopOver.add(ipOperator.operatorVector)
            # 
            if VFFSL(SL,"haveMultipleIPOperatorFractions",True): # generated from line 42, col 5
                write(u'''unsigned long _''')
                _v = VFFSL(SL,"ipOperator.id",True) # u'${ipOperator.id}' on line 43, col 16
                if _v is not None: write(_filter(_v, rawExpr=u'${ipOperator.id}')) # from line 43, col 16.
                write(u'''_exponentIndex = ''')
                _v = VFFSL(SL,"ipOperator.exponentIndex",True) # u'${ipOperator.exponentIndex}' on line 43, col 49
                if _v is not None: write(_filter(_v, rawExpr=u'${ipOperator.exponentIndex}')) # from line 43, col 49.
                write(u''';
''')
        write(u'''
''')
        _v = VFFSL(SL,"transformVectorsToBasis",False)(targetVectors, basis) # u'${transformVectorsToBasis(targetVectors, basis)}' on line 47, col 1
        if _v is not None: write(_filter(_v, rawExpr=u'${transformVectorsToBasis(targetVectors, basis)}')) # from line 47, col 1.
        # 
        write(u'''
''')
        _v = VFFSL(SL,"applyIPOperator",False)(setOfVectorsToLoopOver, basis) # u'${applyIPOperator(setOfVectorsToLoopOver, basis)}' on line 50, col 1
        if _v is not None: write(_filter(_v, rawExpr=u'${applyIPOperator(setOfVectorsToLoopOver, basis)}')) # from line 50, col 1.
        write(u'''
''')
        # 
        
        ########################################
        ## 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 56, 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(u'''if (_exponent > 0) {
  ''')
        _v = VFFSL(SL,"doIPLoop",False)(setOfVectorsToLoopOver, operatorBasis, '*') # u"${doIPLoop(setOfVectorsToLoopOver, operatorBasis, '*'), autoIndent=True}" on line 59, col 3
        if _v is not None: write(_filter(_v, autoIndent=True, rawExpr=u"${doIPLoop(setOfVectorsToLoopOver, operatorBasis, '*'), autoIndent=True}")) # from line 59, col 3.
        write(u'''} else {
  ''')
        _v = VFFSL(SL,"doIPLoop",False)(setOfVectorsToLoopOver, operatorBasis, '/') # u"${doIPLoop(setOfVectorsToLoopOver, operatorBasis, '/'), autoIndent=True}" on line 61, col 3
        if _v is not None: write(_filter(_v, autoIndent=True, rawExpr=u"${doIPLoop(setOfVectorsToLoopOver, operatorBasis, '/'), autoIndent=True}")) # from line 61, col 3.
        write(u'''}
''')
        # 
        
        ########################################
        ## 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 66, 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 69, col 3
            _v = VFFSL(SL,"loopOverVectorsInBasisWithInnerContent",False)(VFFSL(SL,"setOfVectorsToLoopOver",True), VFFSL(SL,"operatorBasis",True), VFFSL(SL,"insideEvaluateOperatorLoops",False)(VFFSL(SL,"operation",True))) # u'${loopOverVectorsInBasisWithInnerContent($setOfVectorsToLoopOver, $operatorBasis, $insideEvaluateOperatorLoops($operation))}' on line 70, col 1
            if _v is not None: write(_filter(_v, rawExpr=u'${loopOverVectorsInBasisWithInnerContent($setOfVectorsToLoopOver, $operatorBasis, $insideEvaluateOperatorLoops($operation))}')) # from line 70, col 1.
        else: # generated from line 71, 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))) # u'${loopOverFieldInBasisWithVectorsAndInnerContent($loopingField, $operatorBasis, $setOfVectorsToLoopOver, $insideEvaluateOperatorLoops($operation))}' on line 72, col 1
            if _v is not None: write(_filter(_v, rawExpr=u'${loopOverFieldInBasisWithVectorsAndInnerContent($loopingField, $operatorBasis, $setOfVectorsToLoopOver, $insideEvaluateOperatorLoops($operation))}')) # from line 72, 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 77, 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 82, col 3
            for operatorComponentNumber, (operatorComponentName, operatorComponentVectors) in enumerate(VFN(VFFSL(SL,"ipOperator.operatorComponents",True),"iteritems",False)()): # generated from line 83, col 5
                for targetVectorComponents in operatorComponentVectors.itervalues(): # generated from line 84, col 7
                    for targetVectorComponent in targetVectorComponents: # generated from line 85, col 9
                        ## START CAPTURE REGION: _20113695 operation at line 86, col 11 in the source.
                        _orig_trans_20113695 = trans
                        _wasBuffering_20113695 = self._CHEETAH__isBuffering
                        self._CHEETAH__isBuffering = True
                        trans = _captureCollector_20113695 = DummyTransaction()
                        write = _captureCollector_20113695.response().write
                        write(u'''/* ''')
                        _v = VFFSL(SL,"operatorComponentName",True) # u'${operatorComponentName}' on line 87, col 4
                        if _v is not None: write(_filter(_v, rawExpr=u'${operatorComponentName}')) # from line 87, col 4.
                        write(u'''[''')
                        _v = VFFSL(SL,"targetVectorComponent",True) # u'$targetVectorComponent' on line 87, col 29
                        if _v is not None: write(_filter(_v, rawExpr=u'$targetVectorComponent')) # from line 87, col 29.
                        write(u'''] */ _''')
                        _v = VFFSL(SL,"ipOperator.operatorVector.id",True) # u'${ipOperator.operatorVector.id}' on line 87, col 57
                        if _v is not None: write(_filter(_v, rawExpr=u'${ipOperator.operatorVector.id}')) # from line 87, col 57.
                        write(u'''[_''')
                        _v = VFFSL(SL,"ipOperator.operatorVector.id",True) # u'${ipOperator.operatorVector.id}' on line 87, col 90
                        if _v is not None: write(_filter(_v, rawExpr=u'${ipOperator.operatorVector.id}')) # from line 87, col 90.
                        write(u'''_index_pointer + ''')
                        _v = VFFSL(SL,"operatorComponentNumber",True) # u'$operatorComponentNumber' on line 87, col 138
                        if _v is not None: write(_filter(_v, rawExpr=u'$operatorComponentNumber')) # from line 87, col 138.
                        if VFFSL(SL,"haveMultipleIPOperatorFractions",True): # generated from line 88, col 13
                            write(u''' + _''')
                            _v = VFFSL(SL,"ipOperator.id",True) # u'${ipOperator.id}' on line 89, col 5
                            if _v is not None: write(_filter(_v, rawExpr=u'${ipOperator.id}')) # from line 89, col 5.
                            write(u'''_exponentIndex''')
                        write(u''']''')
                        trans = _orig_trans_20113695
                        write = trans.response().write
                        self._CHEETAH__isBuffering = _wasBuffering_20113695 
                        operation = _captureCollector_20113695.response().getvalue()
                        del _orig_trans_20113695
                        del _captureCollector_20113695
                        del _wasBuffering_20113695
                        # 
                        operationsDict.setdefault(targetVectorComponent, [])
                        operationsDict[targetVectorComponent].append(operation)
        # 
        for targetVectorComponent, operations in operationsDict.iteritems(): # generated from line 101, col 3
            joinString = '\n' + ' '*len(targetVectorComponent) + ' *  '
            _v = VFFSL(SL,"targetVectorComponent",True) # u'${targetVectorComponent}' on line 103, col 1
            if _v is not None: write(_filter(_v, rawExpr=u'${targetVectorComponent}')) # from line 103, col 1.
            write(u''' ''')
            _v = VFFSL(SL,"operationString",True) # u'${operationString}' on line 103, col 26
            if _v is not None: write(_filter(_v, rawExpr=u'${operationString}')) # from line 103, col 26.
            write(u'''= ''')
            _v = VFN(VFFSL(SL,"joinString",True),"join",False)(operations) # u'${joinString.join(operations)}' on line 103, col 46
            if _v is not None: write(_filter(_v, rawExpr=u'${joinString.join(operations)}')) # from line 103, col 46.
            write(u''';
''')
        # 
        
        ########################################
        ## 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
        
        # 
        # 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(u'''




''')
        
        ########################################
        ## 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://www.CheetahTemplate.org/

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


