|
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 # parse date |
|
26 date = datetime.datetime.strptime(date_name, '%Y-%m-%d').replace(tzinfo=channel.source.tz) |
|
27 |
|
28 print "%s..." % (date, ) |
|
29 |
|
30 # load lines for date |
|
31 lines = channel.source.get_date(date) |
|
32 |
|
33 # insert |
|
34 index.insert(channel, lines) |
|
35 |
|
36 def cmd_search (options, channel_name, query) : |
|
37 """ |
|
38 Search the index for events on a specific channel with the given query |
|
39 """ |
|
40 |
|
41 import channels |
|
42 |
|
43 assert not options.create_index |
|
44 |
|
45 # open the LogSearchIndex |
|
46 index = log_search.LogSearchIndex(options.index_path, 'r') |
|
47 |
|
48 # open the channel |
|
49 channel = channels.channel_list.lookup(channel_name) |
|
50 |
|
51 # search |
|
52 lines = index.search_simple(channel, query) |
|
53 |
|
54 # display as plaintext |
|
55 for line in options.formatter.format_txt(lines) : |
|
56 print line |
|
57 |
|
58 if __name__ == '__main__' : |
|
59 from optparse import OptionParser |
|
60 import log_formatter |
|
61 |
|
62 # define parser |
|
63 parser = OptionParser( |
|
64 usage = "%prog [options] <command> [ ... ]", |
|
65 add_help_option = True, |
|
66 ) |
|
67 |
|
68 # define command-line arguments |
|
69 parser.add_option("-I", "--index", dest="index_path", help="Index database path", metavar="PATH", default="logs/index") |
|
70 parser.add_option("--create", dest="create_index", help="Create index database", default=False) |
|
71 parser.add_option("-f", "--formatter", dest="formatter_name", help="LogFormatter to use", default="irssi") |
|
72 parser.add_option("-z", "--timezone", dest="tz_name", help="Timezone for output", metavar="TZ", default="UTC") |
|
73 |
|
74 # parse |
|
75 options, args = parser.parse_args() |
|
76 |
|
77 # postprocess stuff |
|
78 options.tz = pytz.timezone(options.tz_name) |
|
79 options.formatter = log_formatter.by_name(options.formatter_name)(options.tz) |
|
80 |
|
81 # pop command |
|
82 command = args.pop(0) |
|
83 |
|
84 # inspect |
|
85 func = globals()['cmd_%s' % command] |
|
86 |
|
87 # call |
|
88 func(options, *args) |
|
89 |
|
90 |