#!/usr/bin/env python
# -*- coding: UTF-8 -*-



##################################################
## DEPENDENCIES
import sys
import os
import os.path
from os.path import getmtime, exists
import time
import types
import __builtin__
from Cheetah.Version import MinCompatibleVersion as RequiredCheetahVersion
from Cheetah.Version import MinCompatibleVersionTuple as RequiredCheetahVersionTuple
from Cheetah.Template import Template
from Cheetah.DummyTransaction import DummyTransaction
from Cheetah.NameMapper import NotFound, valueForName, valueFromSearchList, valueFromFrameOrSearchList
from Cheetah.CacheRegion import CacheRegion
import Cheetah.Filters as Filters
import Cheetah.ErrorCatchers as ErrorCatchers
from nav.web.templates.MainTemplate import MainTemplate

##################################################
## MODULE CONSTANTS
try:
    True, False
except NameError:
    True, False = (1==1), (1==0)
VFFSL=valueFromFrameOrSearchList
VFSL=valueFromSearchList
VFN=valueForName
currentTime=time.time
__CHEETAH_version__ = '2.0.1'
__CHEETAH_versionTuple__ = (2, 0, 1, 'final', 0)
__CHEETAH_genTime__ = 1225031611.483299
__CHEETAH_genTimestamp__ = 'Sun Oct 26 14:33:31 2008'
__CHEETAH_src__ = 'MatrixTemplate.tmpl'
__CHEETAH_srcLastModified__ = 'Mon Jan 29 14:32:21 2007'
__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 MatrixTemplate(MainTemplate):

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


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

        MainTemplate.__init__(self, *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 content(self, **KWS):



        ## CHEETAH: generated from #block content 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
        
        write('''<h1>Graphic view of ''')
        _v = VFFSL(SL,"network",True) # '$network' on line 28, col 21
        if _v is not None: write(_filter(_v, rawExpr='$network')) # from line 28, col 21.
        write('''</h1>
<p>This table shows how the subnets are utilized.</p>

<table style="border: 1px solid black; font-size: 0.8em;">
<tr>
<td>&nbsp;</td>
''')
        for number in VFFSL(SL,"numbers",True): # generated from line 34, col 1
            write('''<th>''')
            _v = VFFSL(SL,"number",True) # '$number' on line 35, col 5
            if _v is not None: write(_filter(_v, rawExpr='$number')) # from line 35, col 5.
            write('''</th>
''')
        write('''</tr>

''')
        ignore = {}
        for x in range(VFFSL(SL,"start",True),VFFSL(SL,"end",True)): # generated from line 40, col 1
            if VFN(VFFSL(SL,"unntak",True),"has_key",False)(VFFSL(SL,"x",True)): # generated from line 41, col 1
                write('''<tr>
<td>''')
                _v = VFFSL(SL,"netlink",False)(VFFSL(SL,"bnet",True),VFFSL(SL,"x",True)) # '$netlink($bnet,$x)' on line 43, col 5
                if _v is not None: write(_filter(_v, rawExpr='$netlink($bnet,$x)')) # from line 43, col 5.
                write('''</td>
<td colspan="8" style="text-align:center;background-color:''')
                _v = VFFSL(SL,"load",False)(0,'large') # "$load(0,'large')" on line 44, col 59
                if _v is not None: write(_filter(_v, rawExpr="$load(0,'large')")) # from line 44, col 59.
                write('''">Many small subnets</td>
</tr>
''')
            elif VFN(VFFSL(SL,"ignore",True),"has_key",False)(VFFSL(SL,"x",True)): # generated from line 46, col 1
                pass
                write('''
''')
            elif VFN(VFFSL(SL,"big_net_rowspan",True),"has_key",False)(VFFSL(SL,"x",True)): # generated from line 49, col 1
                y = VFFSL(SL,"numbers",True)[0]
                write('''<tr>
<td>''')
                _v = VFFSL(SL,"netlink",False)(VFFSL(SL,"bnet",True),VFFSL(SL,"x",True)) # '$netlink($bnet,$x)' on line 52, col 5
                if _v is not None: write(_filter(_v, rawExpr='$netlink($bnet,$x)')) # from line 52, col 5.
                write('''</td>
<td rowspan="''')
                _v = VFFSL(SL,"big_net_rowspan",True)[VFFSL(SL,"x",True)] # '$big_net_rowspan[$x]' on line 53, col 14
                if _v is not None: write(_filter(_v, rawExpr='$big_net_rowspan[$x]')) # from line 53, col 14.
                write('''" colspan="8" ''')
                if VFN(VFFSL(SL,"subnet",True),"has_key",False)(VFFSL(SL,"x",True)) and VFN(VFFSL(SL,"subnet",True)[VFFSL(SL,"x",True)],"has_key",False)(VFFSL(SL,"y",True)): # generated from line 54, col 1
                    write('''style="text-align:center;background-color:''')
                    _v = VFFSL(SL,"load",False)(VFN(VFFSL(SL,"subnet",True)[VFFSL(SL,"x",True)][VFFSL(SL,"y",True)],"percent",True),VFN(VFFSL(SL,"subnet",True)[VFFSL(SL,"x",True)][VFFSL(SL,"y",True)],"nettype",True)) # '$load($subnet[$x][$y].percent,$subnet[$x][$y].nettype)' on line 55, col 43
                    if _v is not None: write(_filter(_v, rawExpr='$load($subnet[$x][$y].percent,$subnet[$x][$y].nettype)')) # from line 55, col 43.
                    write('''">''')
                    _v = VFFSL(SL,"netinfo",False)(VFFSL(SL,"subnet",True)[VFFSL(SL,"x",True)][VFFSL(SL,"y",True)]) # '$netinfo($subnet[$x][$y])' on line 55, col 99
                    if _v is not None: write(_filter(_v, rawExpr='$netinfo($subnet[$x][$y])')) # from line 55, col 99.
                    write('''</td>
''')
                write('''</tr>
''')
                rowspan = VFFSL(SL,"big_net_rowspan",True)[VFFSL(SL,"x",True)]
                write('''
''')
                for i in range(1,VFFSL(SL,"rowspan",True)): # generated from line 60, col 1
                    x = VFFSL(SL,"x",True) + 1
                    ignore[x] = 1
                    write('''<tr>
<td>''')
                    _v = VFFSL(SL,"bnet",True) # '$bnet' on line 64, col 5
                    if _v is not None: write(_filter(_v, rawExpr='$bnet')) # from line 64, col 5.
                    write('''.''')
                    _v = VFFSL(SL,"x",True) # '$x' on line 64, col 11
                    if _v is not None: write(_filter(_v, rawExpr='$x')) # from line 64, col 11.
                    write('''</td>
</tr>
''')
                write('''
''')
                # # ordinary line
            else: # generated from line 69, col 1
                write('''
<tr>
<td>''')
                _v = VFFSL(SL,"netlink",False)(VFFSL(SL,"bnet",True),VFFSL(SL,"x",True)) # '$netlink($bnet,$x)' on line 72, col 5
                if _v is not None: write(_filter(_v, rawExpr='$netlink($bnet,$x)')) # from line 72, col 5.
                write('''</td>
''')
                # #set $i = 0
                local_colspan = 0
                write('''
''')
                for i in range(0, len(VFFSL(SL,"numbers",True))): # generated from line 76, col 1
                    y = VFFSL(SL,"numbers",True)[VFFSL(SL,"i",True)]
                    write('''
''')
                    if VFN(VFFSL(SL,"subnet",True),"has_key",False)(VFFSL(SL,"x",True)) and VFN(VFFSL(SL,"subnet",True)[VFFSL(SL,"x",True)],"has_key",False)(VFFSL(SL,"y",True)) and VFFSL(SL,"subnet",True)[VFFSL(SL,"x",True)][VFFSL(SL,"y",True)]: # generated from line 79, col 1
                        write('''<td colspan="''')
                        _v = VFFSL(SL,"colspan",True)[VFN(VFFSL(SL,"subnet",True)[VFFSL(SL,"x",True)][VFFSL(SL,"y",True)],"bits",True)] # '$colspan[$subnet[$x][$y].bits]' on line 80, col 14
                        if _v is not None: write(_filter(_v, rawExpr='$colspan[$subnet[$x][$y].bits]')) # from line 80, col 14.
                        write('''" style="background-color:''')
                        _v = VFFSL(SL,"load",False)(VFN(VFFSL(SL,"subnet",True)[VFFSL(SL,"x",True)][VFFSL(SL,"y",True)],"percent",True),VFN(VFFSL(SL,"subnet",True)[VFFSL(SL,"x",True)][VFFSL(SL,"y",True)],"nettype",True)) # '$load($subnet[$x][$y].percent,$subnet[$x][$y].nettype)' on line 80, col 70
                        if _v is not None: write(_filter(_v, rawExpr='$load($subnet[$x][$y].percent,$subnet[$x][$y].nettype)')) # from line 80, col 70.
                        write(''';text-align:center;">
''')
                        _v = VFFSL(SL,"netinfo",False)(VFFSL(SL,"subnet",True)[VFFSL(SL,"x",True)][VFFSL(SL,"y",True)]) # '$netinfo($subnet[$x][$y])' on line 81, col 1
                        if _v is not None: write(_filter(_v, rawExpr='$netinfo($subnet[$x][$y])')) # from line 81, col 1.
                        write('''

''')
                        local_colspan = VFFSL(SL,"colspan",True)[VFN(VFFSL(SL,"subnet",True)[VFFSL(SL,"x",True)][VFFSL(SL,"y",True)],"bits",True)] - 1
                        write('''</td>

''')
                        #  do not make empty cols where there is a another cell spanning this col
                    else: # generated from line 87, col 1
                        write('''
''')
                        if VFFSL(SL,"local_colspan",True) > 0: # generated from line 89, col 1
                            local_colspan = VFFSL(SL,"local_colspan",True) - 1
                        else: # generated from line 91, col 1
                            write('''<td>&nbsp;</td>
''')
                        write('''
''')
                    # #numbers
                write('''</tr>
''')
            # #hele x
        write('''
</table>
''')
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def netlink(self, input1, input2, **KWS):



        ## CHEETAH: generated from #def $netlink($input1,$input2) at line 104, 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('''<a href="/report/prefix?host(netaddr)=''')
        _v = VFFSL(SL,"input1",True) # '${input1}' on line 105, col 39
        if _v is not None: write(_filter(_v, rawExpr='${input1}')) # from line 105, col 39.
        write('''.''')
        _v = VFFSL(SL,"input2",True) # '${input2}' on line 105, col 49
        if _v is not None: write(_filter(_v, rawExpr='${input2}')) # from line 105, col 49.
        write('''.%&op_host(netaddr)=like">''')
        _v = VFFSL(SL,"input1",True) # '${input1}' on line 105, col 84
        if _v is not None: write(_filter(_v, rawExpr='${input1}')) # from line 105, col 84.
        write('''.''')
        _v = VFFSL(SL,"input2",True) # '${input2}' on line 105, col 94
        if _v is not None: write(_filter(_v, rawExpr='${input2}')) # from line 105, col 94.
        write('''</a>
''')
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def netinfo(self, subnetelement, **KWS):



        ## CHEETAH: generated from #def $netinfo($subnetelement) at line 108, 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('''<a href="/report/prefix?prefixid=''')
        _v = VFFSL(SL,"subnetelement.prefixid",True) # '$subnetelement.prefixid' on line 109, col 34
        if _v is not None: write(_filter(_v, rawExpr='$subnetelement.prefixid')) # from line 109, col 34.
        write('''">.''')
        _v = VFFSL(SL,"subnetelement.suffix",True) # '$subnetelement.suffix' on line 109, col 60
        if _v is not None: write(_filter(_v, rawExpr='$subnetelement.suffix')) # from line 109, col 60.
        write('''/''')
        _v = VFFSL(SL,"subnetelement.bits",True) # '$subnetelement.bits' on line 109, col 82
        if _v is not None: write(_filter(_v, rawExpr='$subnetelement.bits')) # from line 109, col 82.
        write('''</a> (<a href="/machinetracker/?prefixid=''')
        _v = VFFSL(SL,"subnetelement.prefixid",True) # '$subnetelement.prefixid' on line 109, col 142
        if _v is not None: write(_filter(_v, rawExpr='$subnetelement.prefixid')) # from line 109, col 142.
        write('''" title="''')
        _v = VFFSL(SL,"subnetelement.machines",True) # '$subnetelement.machines' on line 109, col 174
        if _v is not None: write(_filter(_v, rawExpr='$subnetelement.machines')) # from line 109, col 174.
        write('''/''')
        _v = VFFSL(SL,"subnetelement.max",True) # '$subnetelement.max' on line 109, col 198
        if _v is not None: write(_filter(_v, rawExpr='$subnetelement.max')) # from line 109, col 198.
        write('''">''')
        _v = VFFSL(SL,"subnetelement.percent",True) # '$subnetelement.percent' on line 109, col 218
        if _v is not None: write(_filter(_v, rawExpr='$subnetelement.percent')) # from line 109, col 218.
        write('''%</a>)
''')
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def load(self, percent, nettype, **KWS):



        ## CHEETAH: generated from #def $load($percent,$nettype) at line 113, 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 VFFSL(SL,"nettype",True)=="static" or VFFSL(SL,"nettype",True)=="scope" or VFFSL(SL,"nettype",True)=="reserved": # generated from line 114, col 1
            if VFN(VFFSL(SL,"configuration.extras",True),"has_key",False)("other"): # generated from line 115, col 1
                _v = VFN(VFFSL(SL,"configuration",True),"extras",True)["other"] # '$configuration.extras["other"]' on line 116, col 1
                if _v is not None: write(_filter(_v, rawExpr='$configuration.extras["other"]')) # from line 116, col 1.
        elif VFFSL(SL,"nettype",True)=="large": # generated from line 118, col 1
            if VFN(VFFSL(SL,"configuration.extras",True),"has_key",False)("large"): # generated from line 119, col 1
                _v = VFN(VFFSL(SL,"configuration",True),"extras",True)["large"] # '$configuration.extras["large"]' on line 120, col 1
                if _v is not None: write(_filter(_v, rawExpr='$configuration.extras["large"]')) # from line 120, col 1.
        else: # generated from line 122, col 1
            lims = VFFSL(SL,"configuration.limits",True)
            limkeys = VFFSL(SL,"configuration.limits.keys",True)
            _v = VFN(VFFSL(SL,"limkeys",True),"sort",False)() # '$limkeys.sort()' on line 125, col 1
            if _v is not None: write(_filter(_v, rawExpr='$limkeys.sort()')) # from line 125, col 1.
            _v = VFN(VFFSL(SL,"limkeys",True),"reverse",False)() # '$limkeys.reverse()' on line 126, col 1
            if _v is not None: write(_filter(_v, rawExpr='$limkeys.reverse()')) # from line 126, col 1.
            for limit in VFFSL(SL,"limkeys",True): # generated from line 127, col 1
                if int(VFFSL(SL,"percent",True))>=int(VFFSL(SL,"limit",True)): # generated from line 128, col 1
                    _v = VFFSL(SL,"lims",True)[VFFSL(SL,"limit",True)] # '$lims[$limit]' on line 129, col 1
                    if _v is not None: write(_filter(_v, rawExpr='$lims[$limit]')) # from line 129, col 1.
                    break
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def additionalCSS(self, **KWS):



        ## CHEETAH: generated from #block additionalCSS at line 137, 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('''<link rel="stylesheet" type="text/css" href="/style/MatrixTemplate.css" />
''')
        
        ########################################
        ## 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
        
        #  $Id: MatrixTemplate.tmpl 3835 2007-01-29 14:32:21Z mortenv $
        # 
        #  Copyright 2003-2005 Norwegian University of Science and Technology
        # 
        #  This file is part of Network Administration Visualized (NAV)
        # 
        #  NAV 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.
        # 
        #  NAV 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 NAV; if not, write to the Free Software
        #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
        # 
        # 
        #  Authors: Sigurd Gartmann <sigurd-nav@brogar.org>
        # 
        write('''
''')
        self.content(trans=trans)
        write('''





''')
        self.additionalCSS(trans=trans)
        
        ########################################
        ## 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_MatrixTemplate= 'writeBody'

## END CLASS DEFINITION

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


# 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=MatrixTemplate()).run()


