## ------------------------------------------------------------------------
## iSiLK - A python-based graphical user interface for SiLK
## ------------------------------------------------------------------------
## Copyright (c) 2007-2008 Carnegie Mellon University. All Rights Reserved.
## ------------------------------------------------------------------------
## Authors: Andrew Kompanek (ajk@cert.org)
## ------------------------------------------------------------------------
## GNU General Public License (GPL) Rights pursuant to Version 2, June 1991
## Government Purpose License Rights (GPLR) pursuant to DFARS 252.225-7013
## ------------------------------------------------------------------------
## See the LICENSE file included in this distribution
## ------------------------------------------------------------------------

# Simple wrappers to demonstrate interacting with GUI from ipython command
# line.  Also beginning of proof of concept for how scripting might work.
#
# Stuff to consider:
#   * Managing logging and handling errors
#   * Providing a way to log progress
#   * Headless mode for scripting

scriptMode = True

import isilk
import tools
#import tools.shell
import analysisresult
import resultset
import analysistool
import config.silk

def test():
    global _runRemote
    _runRemote = True
    print "Running regression tests -- running commands remotely"
    test_wrappers()
    _runRemote = False    
    print "Running regression tests -- running commands locally"
    test_wrappers()
    
def test_wrappers():
    if _runRemote: name_suffix = "--REMOTE--"
    else: name_suffix = "--LOCAL--"
    ps = _problemset()
    q = query(ps, start_date="2004/12/14:00", end_date="2004/12/15:23",
                name="QUERY TEST" + name_suffix)
    sh = shell(q, command="rwfilter $input --saddress=208.102.234.30 --pass=$output",
                name="SHELL TEST" + name_suffix)
    if not _runRemote:
        s = split(q, name="SPLIT TEST" + name_suffix)
    c1 = count(q, fields="proto", name="--count test" + name_suffix)
    t = trend(q, name="TREND TEST" + name_suffix)
    f = refine(q, saddress="208.102.234.30", name="REFINE TEST" + name_suffix)
    c2 = count(f, fields="sip", name="COUNT REFINE RESULT TEST" + name_suffix)
    
    # Copy remote results
    if _runRemote:
        c1.DownloadFile()
    
    save()
        
def test_tool_api():
    runRemote = False
    tm = analysistool.ToolManager()
    q = tm.RunTool(True, tools.query.QueryTool, _problemset(), start_date="2004/12/14:00", end_date="2004/12/15:23")
    sh = tm.RunTool(runRemote, tools.shell.ShellTool, q, command="rwfilter $input --saddress=208.102.234.30 --pass=$output")
    c = tm.RunTool(runRemote, tools.uniq.UniqTool, q, fields="proto")
    t = tm.RunTool(runRemote, tools.count.CountTool, q)
    f = tm.RunTool(runRemote, tools.filter.FilterTool, q, saddress="208.102.234.30")
    c = tm.RunTool(runRemote, tools.uniq.UniqTool, f, fields="sip") 



# Initialize
# FIXME: Make sure these do the right thing in headless mode
_headless = False
_current_problemset = None
_app = isilk.main(ipython=True, headless=_headless)
_runRemote = config.silk.opts["WORK_REMOTE"]

def _temparea():
    pass

def save():
    _problemset().Save()
    
def _problemset():
    global _headless
    global _current_problemset
    if _headless:
        if not _current_problemset:
            _current_problemset = analysisresult.ProblemSet()
        return _current_problemset
    else:
        return _app.GetActiveProblemSet()

def _result():
    if _headless:
        return _current_problemset
    else:
        return _app.GetSelectedData()
    
def show():
    _app.GetExplorer(_result).Show(True)
    
def hide():
    _app.GetExplorer(_result).Show(False)
    
def select(result):
    _app.GetExplorer(result).SelectDataSet(result)
    
def query(parent = None, **kargs):
    if not parent: parent = _problemset()
    return analysistool.RunTool(_runRemote, tools.query.QueryTool, parent, **kargs)
    
def refine(parent=None, **kargs):
    if not parent: parent = _result()
    return analysistool.RunTool(_runRemote, tools.filter.FilterTool, parent, **kargs)
    
def count(parent=None, **kargs):
    if not parent: parent = _result()
    return analysistool.RunTool(_runRemote, tools.uniq.UniqTool, parent, **kargs)
    
def trend(parent=None, **kargs):
    if not parent: parent = _result()
    return analysistool.RunTool(_runRemote, tools.count.CountTool, parent, **kargs)
    
def shell(parent=None, command=None, resultClass=resultset.FilterResult, **kargs):
    if not parent: parent = _result()
    if command==None:
        raise RuntimeError, "Must specify a command string"
    return analysistool.RunTool(_runRemote, tools.shell.ShellTool, parent, 
        command=command, resultClass=resultClass, **kargs)
