#!/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 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__ = 1225031604.495527
__CHEETAH_genTimestamp__ = 'Sun Oct 26 14:33:24 2008'
__CHEETAH_src__ = 'l2traceTemplate.tmpl'
__CHEETAH_srcLastModified__ = 'Fri Jan 26 16:31:47 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 l2traceTemplate(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 tableTitle(self, title, hits, columnCount, **KWS):



        ## CHEETAH: generated from #def tableTitle($title, $hits, $columnCount) 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
        
        columnCount += 1
        write('''<br />

<table class="horiztable">
    <caption>
        <span class="cornerkludge"></span>
        ''')
        _v = VFFSL(SL,"title",True) # '$title' on line 65, col 9
        if _v is not None: write(_filter(_v, rawExpr='$title')) # from line 65, col 9.
        write('''<br />
        <span class="subtitle">''')
        _v = VFFSL(SL,"hits",True) # '$hits' on line 66, col 32
        if _v is not None: write(_filter(_v, rawExpr='$hits')) # from line 66, col 32.
        write(''' rows</span>
    </caption>

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

    def tableHeader(self, rowCount, columns, **KWS):



        ## CHEETAH: generated from #def tableHeader($rowCount, $columns) at line 71, 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
        
        rowCount = VFFSL(SL,"rowCount",True) + 1
        write('''    <tr class="header">
        <td class="leftkludge" rowspan="''')
        _v = VFFSL(SL,"rowCount",True) # '$rowCount' on line 74, col 41
        if _v is not None: write(_filter(_v, rawExpr='$rowCount')) # from line 74, col 41.
        write('''"></td>
''')
        for column in VFFSL(SL,"columns",True): # generated from line 75, col 3
            write('''        <th>''')
            _v = VFFSL(SL,"column",True) # '$column' on line 76, col 13
            if _v is not None: write(_filter(_v, rawExpr='$column')) # from line 76, col 13.
            write('''</th>
''')
        write('''        <td class="rightkludge" rowspan="''')
        _v = VFFSL(SL,"rowCount",True) # '$rowCount' on line 78, col 42
        if _v is not None: write(_filter(_v, rawExpr='$rowCount')) # from line 78, col 42.
        write('''"></td>
    </tr>
''')
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def l2traceTable(self, table, **KWS):



        ## CHEETAH: generated from #def l2traceTable($table) at line 82, 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
        
        columns = ('#', 'Level', 'Vlan', 'Port In', 'IP', 'Sysname', 'Port Out')
        columnCount = len(VFFSL(SL,"columns",True))
        write('''
  ''')
        _v = VFFSL(SL,"tableTitle",False)("L2 Trace results", len(VFFSL(SL,"table",True)), VFFSL(SL,"columnCount",True)) # '$tableTitle("L2 Trace results", len($table), $columnCount)' on line 86, col 3
        if _v is not None: write(_filter(_v, rawExpr='$tableTitle("L2 Trace results", len($table), $columnCount)')) # from line 86, col 3.
        write('''
  ''')
        _v = VFFSL(SL,"tableHeader",False)(len(VFFSL(SL,"table",True)), VFFSL(SL,"columns",True)) # '$tableHeader(len($table), $columns)' on line 87, col 3
        if _v is not None: write(_filter(_v, rawExpr='$tableHeader(len($table), $columns)')) # from line 87, col 3.
        write('''

''')
        for row in VFFSL(SL,"table",True): # generated from line 89, col 3
            write('''    <tr class="''')
            _v = VFFSL(SL,"rowcycler",True) # '$rowcycler' on line 90, col 16
            if _v is not None: write(_filter(_v, rawExpr='$rowcycler')) # from line 90, col 16.
            write('''">
        <td style="text-align: right;">''')
            _v = VFFSL(SL,"row.idx",True) # '$row.idx' on line 91, col 40
            if _v is not None: write(_filter(_v, rawExpr='$row.idx')) # from line 91, col 40.
            write('''</td>
        <td style="text-align: center;">''')
            _v = VFFSL(SL,"row.level",True) # '$row.level' on line 92, col 41
            if _v is not None: write(_filter(_v, rawExpr='$row.level')) # from line 92, col 41.
            write('''</td>

''')
            if VFFSL(SL,"row.vlan",True) == "trunk": # generated from line 94, col 5
                write('''        <td style="text-align: right;">''')
                _v = VFFSL(SL,"row.vlan",True) # '$row.vlan' on line 95, col 40
                if _v is not None: write(_filter(_v, rawExpr='$row.vlan')) # from line 95, col 40.
                write('''</td>
''')
            else: # generated from line 96, col 5
                write('''        <td style="text-align: right;"><a href="/report/prefix?vlan=''')
                _v = VFFSL(SL,"row.vlan",True) # '$row.vlan' on line 97, col 69
                if _v is not None: write(_filter(_v, rawExpr='$row.vlan')) # from line 97, col 69.
                write('''" title="Vlan report">''')
                _v = VFFSL(SL,"row.vlan",True) # '$row.vlan' on line 97, col 100
                if _v is not None: write(_filter(_v, rawExpr='$row.vlan')) # from line 97, col 100.
                write('''</a></td>
''')
            write('''
''')
            if VFFSL(SL,"row.netboxid",True): # generated from line 100, col 5
                write('''        <td><a href="/report/swport?b1.netboxid=''')
                _v = VFFSL(SL,"row.netboxid",True) # '$row.netboxid' on line 101, col 49
                if _v is not None: write(_filter(_v, rawExpr='$row.netboxid')) # from line 101, col 49.
                write('''" title="Switch ports">''')
                _v = VFFSL(SL,"row.portIn",True) # '$row.portIn' on line 101, col 85
                if _v is not None: write(_filter(_v, rawExpr='$row.portIn')) # from line 101, col 85.
                write('''</a></td>
''')
            else: # generated from line 102, col 5
                write('''        <td>''')
                _v = VFFSL(SL,"row.portIn",True) # '$row.portIn' on line 103, col 13
                if _v is not None: write(_filter(_v, rawExpr='$row.portIn')) # from line 103, col 13.
                write('''</td>
''')
            write('''
''')
            if VFFSL(SL,"row.hostOk",True): # generated from line 106, col 5
                write('''        <td><a href="/ipinfo/?ip=''')
                _v = VFFSL(SL,"row.ipaddr",True) # '$row.ipaddr' on line 107, col 34
                if _v is not None: write(_filter(_v, rawExpr='$row.ipaddr')) # from line 107, col 34.
                write('''" title="IP info">''')
                _v = VFFSL(SL,"row.ipaddr",True) # '$row.ipaddr' on line 107, col 63
                if _v is not None: write(_filter(_v, rawExpr='$row.ipaddr')) # from line 107, col 63.
                write('''</a></td>
''')
            else: # generated from line 108, col 5
                write('''        <td>''')
                _v = VFFSL(SL,"row.ipaddr",True) # '$row.ipaddr' on line 109, col 13
                if _v is not None: write(_filter(_v, rawExpr='$row.ipaddr')) # from line 109, col 13.
                write('''</td>
''')
            write('''
''')
            if VFFSL(SL,"row.netboxid",True): # generated from line 112, col 5
                write('''        <td><a href="/browse/?netbox=''')
                _v = VFFSL(SL,"row.ipaddr",True) # '$row.ipaddr' on line 113, col 38
                if _v is not None: write(_filter(_v, rawExpr='$row.ipaddr')) # from line 113, col 38.
                write('''" title="Device browser">''')
                _v = VFFSL(SL,"row.sysname",True) # '$row.sysname' on line 113, col 74
                if _v is not None: write(_filter(_v, rawExpr='$row.sysname')) # from line 113, col 74.
                write('''</a></td>
''')
            else: # generated from line 114, col 5
                write('''        <td>''')
                _v = VFFSL(SL,"row.sysname",True) # '$row.sysname' on line 115, col 13
                if _v is not None: write(_filter(_v, rawExpr='$row.sysname')) # from line 115, col 13.
                write('''</td>
''')
            write('''
''')
            if VFFSL(SL,"row.netboxid",True): # generated from line 118, col 5
                write('''        <td><a href="/report/swport?b1.netboxid=''')
                _v = VFFSL(SL,"row.netboxid",True) # '$row.netboxid' on line 119, col 49
                if _v is not None: write(_filter(_v, rawExpr='$row.netboxid')) # from line 119, col 49.
                write('''" title="Switch ports">''')
                _v = VFFSL(SL,"row.portOut",True) # '$row.portOut' on line 119, col 85
                if _v is not None: write(_filter(_v, rawExpr='$row.portOut')) # from line 119, col 85.
                write('''</a></td>
''')
            else: # generated from line 120, col 5
                write('''        <td>''')
                _v = VFFSL(SL,"row.portOut",True) # '$row.portOut' on line 121, col 13
                if _v is not None: write(_filter(_v, rawExpr='$row.portOut')) # from line 121, col 13.
                write('''</td>
''')
            write('''    </tr>
''')
        write('''</table>
''')
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def content(self, **KWS):



        ## CHEETAH: generated from #block content at line 30, 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>Layer 2 Traceroute</h2>

<div id="l2trace">

<div id="forms">

<form id="l2traceform" action="" method="get">
<table style="font-size: 13px;">
 <tr>
  <td><label>From </label></td>
  <td><input type=text name=host_from value=''')
        _v = VFFSL(SL,"form.host_from",True) # '$form.host_from' on line 42, col 45
        if _v is not None: write(_filter(_v, rawExpr='$form.host_from')) # from line 42, col 45.
        write('''></td>
  <td><label>To </label></td>
  <td><input type=text name=host_to value=''')
        _v = VFFSL(SL,"form.host_to",True) # '$form.host_to' on line 44, col 43
        if _v is not None: write(_filter(_v, rawExpr='$form.host_to')) # from line 44, col 43.
        write('''></td>
 </tr>
 <tr>
  <td></td>
  <td colspan=2>
   \t<input type=submit name=send value="Trace">
  </td>
  <td align="center">(optional)</td>
 </tr>
</table>
</form>

</div> <!-- forms -->




<div id="results">
''')
        if VFFSL(SL,"l2tracer",True): # generated from line 129, col 1
            write('''    ''')
            _v = VFFSL(SL,"l2traceTable",False)(VFFSL(SL,"l2tracer.table",True)) # '$l2traceTable($l2tracer.table)' on line 130, col 5
            if _v is not None: write(_filter(_v, rawExpr='$l2traceTable($l2tracer.table)')) # from line 130, col 5.
            write('''
''')
        write('''
''')
        if not VFFSL(SL,"l2tracer",True): # generated from line 133, col 1
            noresults = 0
            write('''
''')
            if VFFSL(SL,"noresults",True): # generated from line 136, col 3
                write('''<p>Your search did not return any results</p>
''')
            else: # generated from line 138, col 3
                write('''<div class="infobox">
<h5>L2 Trace usage</h5>
<ul>
  <li>Enter a hostname or IP address and trace up to its router.
  </li>

  <li>Optionally enter a destination hostname or IP address and trace between the two.
  </li>

  <li>Press the Trace button to start.
  </li>

</ul>
</div>

''')
        write('''</div> <!-- results -->


</div> <!-- l2trace -->

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

    def additionalCSS(self, **KWS):



        ## CHEETAH: generated from #block additionalCSS at line 163, 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",True) # '$default_table_CSS' on line 164, col 1
        if _v is not None: write(_filter(_v, rawExpr='$default_table_CSS')) # from line 164, col 1.
        write('''
<link rel="stylesheet" type="text/css" href="/style/l2trace.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: MachineTrackerTemplate.tmpl 3106 2005-01-20 10:47:13Z mortenv $
        # 
        #  Copyright 2003, 2004 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
        # 
        # 
        #  Author: Kristian Eide <kreide@gmail.com>
        # 
        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__

    path =  [("Home", "/"), ("Layer 2 Traceroute", "/l2trace/")]

    _mainCheetahMethod_for_l2traceTemplate= 'writeBody'

## END CLASS DEFINITION

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


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


