#!/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__ = 1225031604.1919911
__CHEETAH_genTimestamp__ = 'Sun Oct 26 14:33:24 2008'
__CHEETAH_src__ = 'IPInfo.tmpl'
__CHEETAH_srcLastModified__ = 'Fri Jan 19 14:16:23 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 IPInfo(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 linked(self, key, value, **KWS):



        ## CHEETAH: generated from #def linked($key, $value) at line 33, 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
        
        link = ''
        if VFFSL(SL,"key",True) == 'navreg': # generated from line 35, col 3
            link = '/browse/' + VFFSL(SL,"value",True)
        elif VFFSL(SL,"key",True) == 'arp': # generated from line 37, col 3
            link = '/machinetracker/?from_ip=' + str(VFFSL(SL,"info",True)['ip']) + '&to_ip=&aip=on&days=7&send=Search'
        elif VFFSL(SL,"key",True) == 'email': # generated from line 39, col 3
            link = 'mailto:' + VFFSL(SL,"value",True)
        if VFFSL(SL,"link",True): # generated from line 43, col 3
            write('''<a href="''')
            _v = VFFSL(SL,"link",True) # '$link' on line 44, col 10
            if _v is not None: write(_filter(_v, rawExpr='$link')) # from line 44, col 10.
            write('''">''')
            _v = VFFSL(SL,"value",True) # '$value' on line 44, col 17
            if _v is not None: write(_filter(_v, rawExpr='$value')) # from line 44, col 17.
            write('''</a> ''')
        else: # generated from line 45, col 3
            _v = VFFSL(SL,"value",True) # '$value' on line 46, col 1
            if _v is not None: write(_filter(_v, rawExpr='$value')) # from line 46, col 1.
            write(''' ''')
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

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



        ## CHEETAH: generated from #def infotable($table) at line 50, 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('''<table class="infotable">
''')
        _orig_filter_99897287 = _filter
        filterName = 'WebSafe'
        if self._CHEETAH__filters.has_key("WebSafe"):
            _filter = self._CHEETAH__currentFilter = self._CHEETAH__filters[filterName]
        else:
            _filter = self._CHEETAH__currentFilter = \
			self._CHEETAH__filters[filterName] = getattr(self._CHEETAH__filtersLib, filterName)(self).filter
        write('''  <caption>Information about <em>''')
        _v = VFFSL(SL,"target",True) # '$target' on line 53, col 34
        if _v is not None: write(_filter(_v, rawExpr='$target')) # from line 53, col 34.
        write('''</em></caption>
''')
        _filter = self._CHEETAH__currentFilter = _orig_filter_99897287
        write('''
''')
        firstkeys = [key for key in VFFSL(SL,"sortorder",True) if key in VFN(VFFSL(SL,"table",True),"keys",False)()]
        lastkeys = [key for key in VFN(VFFSL(SL,"table",True),"keys",False)() if key not in VFFSL(SL,"firstkeys",True)]
        VFN(VFFSL(SL,"lastkeys",True),"sort",False)()
        keys = VFFSL(SL,"firstkeys",True) + VFFSL(SL,"lastkeys",True)
        evenrow = True
        rowclass = ['oddrow', 'evenrow']
        write('''
''')
        for key in VFFSL(SL,"keys",True): # generated from line 63, col 3
            # if $table[$key]
            write('''      <tr class="''')
            _v = VFFSL(SL,"rowclass",True)[VFFSL(SL,"evenrow",True)] # '$rowclass[$evenrow]' on line 65, col 18
            if _v is not None: write(_filter(_v, rawExpr='$rowclass[$evenrow]')) # from line 65, col 18.
            write('''">
        <th>''')
            _v = VFN(VFFSL(SL,"info",True),"describe",False)(VFFSL(SL,"key",True)) # '$info.describe($key)' on line 66, col 13
            if _v is not None: write(_filter(_v, rawExpr='$info.describe($key)')) # from line 66, col 13.
            write('''</th>
        <td>''')
            _v = VFFSL(SL,"linked",False)(VFFSL(SL,"key",True), VFFSL(SL,"table",True)[VFFSL(SL,"key",True)]) # '$linked($key, $table[$key])' on line 67, col 13
            if _v is not None: write(_filter(_v, rawExpr='$linked($key, $table[$key])')) # from line 67, col 13.
            write('''</th>
      </tr>
''')
            evenrow = not VFFSL(SL,"evenrow",True)
            # end if
        write('''</table>
''')
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def content(self, **KWS):



        ## CHEETAH: generated from #block content at line 75, 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('''
<div id="ipinfo">
<h2>IP Information Center</h2>

<form action="" method="get">
<label for="ipinput">IP Address / DNS name</label>:<br>
<input id="ipinput" type="text" name="ip" size="30" 
''')
        if VFFSL(SL,"varExists",False)('target'): # generated from line 83, col 1
            write('''value="''')
            _v = VFFSL(SL,"target",True) # '${target, also=\'"\'}' on line 84, col 8
            if _v is not None: write(_filter(_v, also='"', rawExpr='${target, also=\'"\'}')) # from line 84, col 8.
            write('''" ''')
        write('''>
<input type="submit" value="Show">
</form>

''')
        if VFFSL(SL,"varExists",False)('target') and VFFSL(SL,"target",True): # generated from line 90, col 1
            if VFFSL(SL,"varExists",False)('invalidIP') and VFFSL(SL,"invalidIP",True): # generated from line 91, col 3
                write('''    <p>&quot;<em>''')
                _v = VFFSL(SL,"target",True) # '$target' on line 92, col 18
                if _v is not None: write(_filter(_v, rawExpr='$target')) # from line 92, col 18.
                write('''</em>&quot; is not a valid IP address!</p>
''')
            elif VFFSL(SL,"varExists",False)('info') and VFFSL(SL,"info",True): # generated from line 93, col 3
                write('''    ''')
                _v = VFFSL(SL,"infotable",False)(VFFSL(SL,"info",True)) # '$infotable($info)' on line 94, col 5
                if _v is not None: write(_filter(_v, rawExpr='$infotable($info)')) # from line 94, col 5.
                write('''

''')
                if VFFSL(SL,"varExists",False)('localsiteinfo'): # generated from line 96, col 5
                    write('''    <div id="localsiteinfo">''')
                    _v = VFFSL(SL,"localsiteinfo",True) # '$localsiteinfo' on line 97, col 29
                    if _v is not None: write(_filter(_v, rawExpr='$localsiteinfo')) # from line 97, col 29.
                    write('''<div>
''')
                write('''
''')
            else: # generated from line 100, col 3
                _orig_filter_53120361 = _filter
                filterName = 'WebSafe'
                if self._CHEETAH__filters.has_key("WebSafe"):
                    _filter = self._CHEETAH__currentFilter = self._CHEETAH__filters[filterName]
                else:
                    _filter = self._CHEETAH__currentFilter = \
			self._CHEETAH__filters[filterName] = getattr(self._CHEETAH__filtersLib, filterName)(self).filter
                write('''    <p>No information available for <em>''')
                _v = VFFSL(SL,"target",True) # '$target' on line 102, col 41
                if _v is not None: write(_filter(_v, rawExpr='$target')) # from line 102, col 41.
                write('''</em>.</p>
''')
                _filter = self._CHEETAH__currentFilter = _orig_filter_53120361
        write('''</div>

''')
        if VFFSL(SL,"varExists",False)('debug'): # generated from line 108, col 1
            write('''  <div style="font-size: small; border: 1px dashed red;">
  Debug:<br>
  <pre>''')
            _v = VFFSL(SL,"debug",True) # '$debug' on line 111, col 8
            if _v is not None: write(_filter(_v, rawExpr='$debug')) # from line 111, col 8.
            write('''</pre>
  </div>
''')
        write('''
''')
        
        ########################################
        ## END - generated method body
        
        return _dummyTrans and trans.response().getvalue() or ""
        

    def additionalCSS(self, **KWS):



        ## CHEETAH: generated from #block additionalCSS at line 117, 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/ipinfo.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: IPInfo.tmpl 3809 2007-01-19 14:16:23Z jodal $
        #  Extends the MainTemplate 
        #  Defines the IP information page
        # 
        #  Copyright 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: Morten Vold <morten.vold@ntnu.no>
        # 
        write('''
''')
        #  Don't know how to break lines in Cheetah, so this gets ugly long:
        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", "/"), ("IP Info", None)]

    sortorder = ('navreg', 'ip', 'dns', 'subnet', 'usage', 'description', 'org', 'opt1', 'opt2', 'opt3', 'arp', 'mac', 'switch', 'module', 'port')

    _mainCheetahMethod_for_IPInfo= 'writeBody'

## END CLASS DEFINITION

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


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


