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