#!/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
import re

##################################################
## 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__ = 1225031609.2390959
__CHEETAH_genTimestamp__ = 'Sun Oct 26 14:33:29 2008'
__CHEETAH_src__ = 'SortedStatsTemplate.tmpl'
__CHEETAH_srcLastModified__ = 'Wed Jan 31 14:40:17 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 SortedStatsTemplate(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 additionalCSS(self, **KWS):



        ## CHEETAH: generated from #def additionalCSS at line 6, 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
        
        _v = VFFSL(SL,"default_table_CSS",False)() # '$default_table_CSS()' on line 7, col 1
        if _v is not None: write(_filter(_v, rawExpr='$default_table_CSS()')) # from line 7, col 1.
        write('''
''')
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def content(self, **KWS):



        ## CHEETAH: generated from #def content at line 10, 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('''
<h2>Statistics</h2>

<ul>
    <li><a href="/cricket/">Cricket</a> - Traffic statistics from router
    interfaces and switch ports, CPU utilization, memory usage and more</li>
</ul>

<h3>Ranked Statistics</h3>

<form action="" method="get">

<table>
    <tr>
        <td>Choose view</td>
        <td>Timeframe</td>
        <td>Rows</td>
        <td>Live</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
''')
        #  Drop-down to choose view
        write('''        <td><select name="view">
''')
        sectionslist = VFN(VFFSL(SL,"config",True),"sections",False)()
        _v = VFN(VFFSL(SL,"sectionslist",True),"sort",False)() # '$sectionslist.sort()' on line 35, col 1
        if _v is not None: write(_filter(_v, rawExpr='$sectionslist.sort()')) # from line 35, col 1.
        write('''
''')
        for s in VFFSL(SL,"sectionslist",True): # generated from line 36, col 1
            if VFFSL(SL,"s",True) == 'ss_general': # generated from line 37, col 5
                continue
            #  If we have a view, mark it as selected, else don't bother.
            if VFFSL(SL,"view",True) == VFFSL(SL,"s",True): # generated from line 41, col 5
                write('''            <option value="''')
                _v = VFFSL(SL,"s",True) # '$s' on line 42, col 28
                if _v is not None: write(_filter(_v, rawExpr='$s')) # from line 42, col 28.
                write('''" selected>''')
                _v = VFN(VFFSL(SL,"config",True),"get",False)(VFFSL(SL,"s",True), 'name') # "$config.get($s, 'name')" on line 42, col 41
                if _v is not None: write(_filter(_v, rawExpr="$config.get($s, 'name')")) # from line 42, col 41.
                write('''</option>
''')
            else: # generated from line 43, col 5
                write('''            <option value="''')
                _v = VFFSL(SL,"s",True) # '$s' on line 44, col 28
                if _v is not None: write(_filter(_v, rawExpr='$s')) # from line 44, col 28.
                write('''">''')
                _v = VFN(VFFSL(SL,"config",True),"get",False)(VFFSL(SL,"s",True), 'name') # "$config.get($s, 'name')" on line 44, col 32
                if _v is not None: write(_filter(_v, rawExpr="$config.get($s, 'name')")) # from line 44, col 32.
                write('''</option>
''')
        write('''        </select></td>
''')
        #  Drop-down to choose timeframe
        write('''        <td><select name="fromtime">
''')
        for t in VFN(VFFSL(SL,"fromtimes",True),"keys",False)(): # generated from line 50, col 1
            if VFFSL(SL,"fromtime",True) == VFFSL(SL,"t",True): # generated from line 51, col 5
                write('''            <option value="''')
                _v = VFFSL(SL,"t",True) # '$t' on line 52, col 28
                if _v is not None: write(_filter(_v, rawExpr='$t')) # from line 52, col 28.
                write('''" selected="selected">''')
                _v = VFFSL(SL,"fromtimes",True)[VFFSL(SL,"t",True)] # '$fromtimes[$t]' on line 52, col 52
                if _v is not None: write(_filter(_v, rawExpr='$fromtimes[$t]')) # from line 52, col 52.
                write('''</option>
''')
            else: # generated from line 53, col 5
                write('''            <option value="''')
                _v = VFFSL(SL,"t",True) # '$t' on line 54, col 28
                if _v is not None: write(_filter(_v, rawExpr='$t')) # from line 54, col 28.
                write('''">''')
                _v = VFFSL(SL,"fromtimes",True)[VFFSL(SL,"t",True)] # '$fromtimes[$t]' on line 54, col 32
                if _v is not None: write(_filter(_v, rawExpr='$fromtimes[$t]')) # from line 54, col 32.
                write('''</option>
''')
        write('''        </select></td>
        <td><input type="text" name="numrows" value="''')
        _v = VFFSL(SL,"numrows",True) # '$numrows' on line 58, col 54
        if _v is not None: write(_filter(_v, rawExpr='$numrows')) # from line 58, col 54.
        write('''" size="3" /></td>
''')
        if VFFSL(SL,"forcedview",True) == '1': # generated from line 59, col 1
            write('''        <td><input type="checkbox" name="forcedview" value="1" checked="checked" /></td>
''')
        else: # generated from line 61, col 1
            write('''        <td><input type="checkbox" name="forcedview" value="1" /></td>
''')
        write('''        <td><input type="submit" name="normalview" value="View" /></td>
    </tr>
</table>

</form>

<br />

''')
        if VFFSL(SL,"showArr",True): # generated from line 72, col 1
            write('''<table class="horiztable">
    <caption>
        <span class="cornerkludge"></span>
        Ranked Statistics for ''')
            _v = VFN(VFFSL(SL,"config",True),"get",False)(VFFSL(SL,"view",True), 'name') # "$config.get($view, 'name')" on line 76, col 31
            if _v is not None: write(_filter(_v, rawExpr="$config.get($view, 'name')")) # from line 76, col 31.
            write('''<br />
        <span class="subtitle">''')
            _v = VFFSL(SL,"fromtimes",True)[VFFSL(SL,"fromtime",True)] # '$fromtimes[$fromtime]' on line 77, col 32
            if _v is not None: write(_filter(_v, rawExpr='$fromtimes[$fromtime]')) # from line 77, col 32.
            write('''</span>
    </caption>

    <tr class="header">
        <td class="leftkludge"></td>
        <th>Rank</th>
        <th>Target</th>
        <th>Value</th>
        <th>Unit</th>
        <td class="rightkludge"></td>
    </tr>

''')
            #  Walk the sortedkeys and print the values
            counter = 1
            for line in VFFSL(SL,"sortedKeys",True): # generated from line 91, col 1
                #  Find target to use when making link.
                if VFFSL(SL,"linkview",True): # generated from line 93, col 5
                    target = VFN(VFN(VFFSL(SL,"re",True),"search",False)("cricket-data(/.*)", VFFSL(SL,"line",True)),"groups",False)()[0]
                #  Format the values.
                output = VFN(VFFSL(SL,"re",True),"sub",False)(".*/([^\/]+/[^\/]+)$","\\1", VFFSL(SL,"line",True))
                value = "%.2f" %(VFFSL(SL,"showArr",True)[VFFSL(SL,"line",True)])
                write('''    <tr class="''')
                _v = VFFSL(SL,"rowcycler",True) # '$rowcycler' on line 99, col 16
                if _v is not None: write(_filter(_v, rawExpr='$rowcycler')) # from line 99, col 16.
                write('''">
        <td style="background: white;"></td>
        <td>''')
                _v = VFFSL(SL,"counter",True) # '$counter' on line 101, col 13
                if _v is not None: write(_filter(_v, rawExpr='$counter')) # from line 101, col 13.
                write('''</td>
        <td>
''')
                #  If we got a linkview from the config-file, add a link
                if VFFSL(SL,"linkview",True): # generated from line 104, col 5
                    write("""            <a href='/cricket/?target=""")
                    _v = VFFSL(SL,"target",True) # '$target' on line 105, col 39
                    if _v is not None: write(_filter(_v, rawExpr='$target')) # from line 105, col 39.
                    write(''';view=''')
                    _v = VFFSL(SL,"linkview",True) # '$linkview' on line 105, col 52
                    if _v is not None: write(_filter(_v, rawExpr='$linkview')) # from line 105, col 52.
                    write("""'>""")
                    _v = VFFSL(SL,"output",True) # '$output' on line 105, col 63
                    if _v is not None: write(_filter(_v, rawExpr='$output')) # from line 105, col 63.
                    write('''</a>
''')
                else: # generated from line 106, col 5
                    write('''            ''')
                    _v = VFFSL(SL,"output",True) # '$output' on line 107, col 13
                    if _v is not None: write(_filter(_v, rawExpr='$output')) # from line 107, col 13.
                    write('''
''')
                write('''        </td>
        <td align="right">''')
                _v = VFFSL(SL,"value",True) # '$value' on line 110, col 27
                if _v is not None: write(_filter(_v, rawExpr='$value')) # from line 110, col 27.
                write('''</td>
        <td>''')
                _v = VFFSL(SL,"units",True) # '$units' on line 111, col 13
                if _v is not None: write(_filter(_v, rawExpr='$units')) # from line 111, col 13.
                write('''</td>
        <td style="background: white;"></td>
    </tr>
''')
                counter += 1
                if VFFSL(SL,"counter",True) > int(VFFSL(SL,"numrows",True)): # generated from line 115, col 5
                    break
            write('''</table>
''')
        write('''
''')
        if VFFSL(SL,"exetime",True) != 0: # generated from line 122, col 1
            write('''<p>Got data in ''')
            _v = VFFSL(SL,"exetime",True) # '$exetime' on line 123, col 16
            if _v is not None: write(_filter(_v, rawExpr='$exetime')) # from line 123, col 16.
            write(''' seconds (''')
            _v = VFFSL(SL,"footer",True) # '$footer' on line 123, col 34
            if _v is not None: write(_filter(_v, rawExpr='$footer')) # from line 123, col 34.
            write(''').</p>
''')
        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('''


''')
        
        ########################################
        ## 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_SortedStatsTemplate= 'writeBody'

## END CLASS DEFINITION

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


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


