fix pagination to just use page numbers... less bugs
"""
Tool for accessing the search index
"""
import sys; sys.path.insert(0, '.'); sys.path.insert(0, '..')
import datetime, pytz
import log_search
def cmd_load (options, channel_name, *dates) :
"""
Loads the logs for a specific channel for the given dates into the index
"""
import channels
# open the LogSearchIndex
index = log_search.LogSearchIndex(options.index_path, '*' if options.create_index else 'a')
# open the channel
channel = channels.channel_list.lookup(channel_name)
for date_name in dates :
print "%s..." % (date_name, ),
lines = None
try :
# parse date
date = datetime.datetime.strptime(date_name, '%Y-%m-%d').replace(tzinfo=channel.source.tz)
# load lines for date
lines = channel.source.get_date(date)
except Exception, e :
print "Skipped: %s" % (e, )
else :
# insert
count = index.insert(channel, lines)
print "%d" % count
def cmd_search (options, channel_name, query) :
"""
Search the index for events on a specific channel with the given query
"""
import channels
assert not options.create_index
# open the LogSearchIndex
index = log_search.LogSearchIndex(options.index_path, 'r')
# open the channel
channel = channels.channel_list.lookup(channel_name)
# search
lines = index.search_simple(channel, query)
# display as plaintext
for line in options.formatter.format_txt(lines) :
print line
if __name__ == '__main__' :
from optparse import OptionParser
import log_formatter
# define parser
parser = OptionParser(
usage = "%prog [options] <command> [ ... ]",
add_help_option = True,
)
# define command-line arguments
parser.add_option("-I", "--index", dest="index_path", help="Index database path", metavar="PATH", default="logs/index")
parser.add_option("--create", dest="create_index", action="store_true", help="Create index database")
parser.add_option("-f", "--formatter", dest="formatter_name", help="LogFormatter to use", default="irssi")
parser.add_option("-z", "--timezone", dest="tz_name", help="Timezone for output", metavar="TZ", default="UTC")
parser.add_option("--skip-missing", dest="skip_missing", action="store_true", help="Skip missing logfiles")
# parse
options, args = parser.parse_args()
# postprocess stuff
options.tz = pytz.timezone(options.tz_name)
options.formatter = log_formatter.by_name(options.formatter_name)(options.tz)
# pop command
command = args.pop(0)
# inspect
func = globals()['cmd_%s' % command]
# call
func(options, *args)