tools/search.py
changeset 82 afd3120ec71e
parent 81 745032a57803
child 83 a34e9f56ddda
equal deleted inserted replaced
81:745032a57803 82:afd3120ec71e
     1 """
       
     2     Tool for accessing the search index
       
     3 """
       
     4 
       
     5 import sys; sys.path.insert(0, '.'); sys.path.insert(0, '..')
       
     6 
       
     7 import datetime, pytz
       
     8 
       
     9 import log_search
       
    10 
       
    11 def cmd_load (options, channel_name, *dates) :
       
    12     """
       
    13         Loads the logs for a specific channel for the given dates into the index
       
    14     """
       
    15 
       
    16     import channels
       
    17     
       
    18     # open the LogSearchIndex
       
    19     index = log_search.LogSearchIndex(options.index_path, '*' if options.create_index else 'a')
       
    20 
       
    21     # open the channel
       
    22     channel = channels.channel_list.lookup(channel_name)
       
    23     
       
    24     for date_name in dates :
       
    25         print "%s..." % (date_name, ),
       
    26         lines = None
       
    27         
       
    28         try :
       
    29             # parse date
       
    30             date = datetime.datetime.strptime(date_name, '%Y-%m-%d').replace(tzinfo=channel.source.tz)
       
    31 
       
    32             # load lines for date
       
    33             lines = channel.source.get_date(date)
       
    34         
       
    35         except Exception, e :
       
    36             print "Skipped: %s" % (e, )
       
    37         
       
    38         else :
       
    39             # insert
       
    40             count = index.insert(channel, lines)
       
    41 
       
    42             print "%d" % count
       
    43 
       
    44 def cmd_search (options, channel_name, query) :
       
    45     """
       
    46         Search the index for events on a specific channel with the given query
       
    47     """
       
    48 
       
    49     import channels
       
    50 
       
    51     assert not options.create_index
       
    52     
       
    53     # open the LogSearchIndex
       
    54     index = log_search.LogSearchIndex(options.index_path, 'r')
       
    55 
       
    56     # open the channel
       
    57     channel = channels.channel_list.lookup(channel_name)
       
    58     
       
    59     # search
       
    60     lines = index.search_simple(channel, query)
       
    61     
       
    62     # display as plaintext
       
    63     for line in options.formatter.format_txt(lines) :
       
    64         print line
       
    65 
       
    66 if __name__ == '__main__' :
       
    67     from optparse import OptionParser
       
    68     import log_formatter
       
    69     
       
    70     # define parser
       
    71     parser = OptionParser(
       
    72         usage           = "%prog [options] <command> [ ... ]",
       
    73         add_help_option = True,
       
    74     )
       
    75 
       
    76     # define command-line arguments
       
    77     parser.add_option("-I", "--index", dest="index_path", help="Index database path", metavar="PATH", default="logs/index")
       
    78     parser.add_option("--create", dest="create_index", action="store_true", help="Create index database")
       
    79     parser.add_option("-f", "--formatter", dest="formatter_name", help="LogFormatter to use", default="irssi")
       
    80     parser.add_option("-z", "--timezone", dest="tz_name", help="Timezone for output", metavar="TZ", default="UTC")
       
    81     parser.add_option("--skip-missing", dest="skip_missing", action="store_true", help="Skip missing logfiles")
       
    82 
       
    83     # parse
       
    84     options, args = parser.parse_args()
       
    85 
       
    86     # postprocess stuff
       
    87     options.tz = pytz.timezone(options.tz_name)
       
    88     options.formatter = log_formatter.by_name(options.formatter_name)(options.tz)
       
    89     
       
    90     # pop command
       
    91     command = args.pop(0)
       
    92 
       
    93     # inspect
       
    94     func = globals()['cmd_%s' % command]
       
    95     
       
    96     # call
       
    97     func(options, *args)
       
    98 
       
    99