diff -r 0b8e2ba5f76f -r 2dc6de43f317 scripts/search-index --- a/scripts/search-index Tue Feb 10 23:59:56 2009 +0200 +++ b/scripts/search-index Wed Feb 11 00:33:21 2009 +0200 @@ -60,6 +60,32 @@ if not options.quiet : print "OK: %d lines" % count + +def _parse_date (options, date_str, tz=None, fmt='%Y-%m-%d') : + """ + Parse the given datetime, using the given timezone(defaults to options.tz) and format + """ + + # default tz + if not tz : + tz = options.tz + + try : + # parse + return datetime.datetime.strptime(date_str, fmt).replace(tzinfo=tz) + + except Exception, e : + raise CommandError("[ERROR] Invalid date: %s: %s" % (date_str, e)) + +def _output_lines (options, lines) : + """ + Display the formatted LogLines + """ + + # display as plaintext + for line, txt_data in options.formatter.format_txt(lines, full_timestamps=True) : + print txt_data + class CommandError (Exception) : """ Error with command-line arguments @@ -87,22 +113,22 @@ index, channel = _open_index_and_channel(options, channel_name, 'c' if options.create else 'a') # handle each date - for date_name in dates : + for date_str in dates : + # prase date try : - # parse date - date = datetime.datetime.strptime(date_name, '%Y-%m-%d').replace(tzinfo=channel.source.tz) - - except Exception, e : - print "[ERROR] Invalid date: %s: %s" % (date_name, e) - + date = _parse_date(options, date_str, channel.source.tz) + + # handle errors + except CommandError, e : if options.skip_missing : - continue + print "[ERROR] %s" % (date_name, e) else : raise - # load - _load_channel_date(index, options, channel, date) + # otherwise, load + else : + _load_channel_date(index, options, channel, date) def cmd_load_month (options, channel_name, *months) : """ @@ -113,15 +139,16 @@ index, channel = _open_index_and_channel(options, channel_name, 'c' if options.create else 'a') # handle each date - for month_name in months : + for month_str in months : + # prase date try : - # parse date - month = datetime.datetime.strptime(month_name, '%Y-%m').replace(tzinfo=channel.source.tz) - - except Exception, e : - print "[ERROR] Invalid date: %s: %s" % (month_name, e) - + month = _parse_date(options, month_str, channel.source.tz, '%Y-%m') + + # handle errors + except CommandError, e : + # skip? if options.skip_missing : + print "[ERROR] %s" % (date_name, e) continue else : @@ -146,7 +173,7 @@ """ # sanity-check - if options.create_index : + if options.create : raise Exception("--create doesn't make sense for 'search'") # open index/channel @@ -155,9 +182,31 @@ # search lines = index.search_simple(channel, query) - # display as plaintext - for line in options.formatter.format_txt(lines) : - print line + # display + _output_lines(options, lines) + +def cmd_list (options, channel_name, *dates) : + """ + List the indexed events for a specific date + """ + + # sanity-check + if options.create : + raise Exception("--create doesn't make sense for 'search'") + + # open index/channel + index, channel = _open_index_and_channel(options, channel_name, 'r') + + # ...for each date + for date_str in dates : + # parse date + date = _parse_date(options, date_str) + + # list + lines = index.list(channel, date) + + # display + _output_lines(options, lines) def cmd_help (options, *args) : """