scripts/search-index.py
author Tero Marttila <terom@fixme.fi>
Tue, 10 Feb 2009 04:27:22 +0200
changeset 82 afd3120ec71e
parent 68 tools/search.py@8157c41b3236
child 83 a34e9f56ddda
permissions -rw-r--r--
add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
"""
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
    Tool for accessing the search index
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
     5
# XXX: fix path
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
import sys; sys.path.insert(0, '.'); sys.path.insert(0, '..')
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     7
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
import datetime, pytz
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    10
# configuration and the LogSearchIndex module
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    11
import config, log_search, channels
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    12
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    13
def _open_index_and_channel (options, channel_name, open_mode) :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    14
    """
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    15
        Opens+returns a LogSearchIndex and a LogChannel
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    16
    """
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    17
    # open the LogSearchIndex
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    18
    index = log_search.LogSearchIndex(options.index_path, open_mode)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    19
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    20
    # open the channel
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    21
    channel = config.LOG_CHANNELS.lookup(channel_name)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    22
    
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    23
    # return
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    24
    return index, channel
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    25
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    26
def _load_channel_date (index, options, channel, date) :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    27
    """
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    28
        Loads the logs for the given date from the channel's LogSource into the given LogSearchIndex
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    29
    """
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    30
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    31
    if not options.quiet :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    32
        print "%s %s..." % (channel.id, date.strftime(channel.source.filename_fmt)),
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    33
        
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    34
    try :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    35
        # load lines for date
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    36
        lines = channel.source.get_date(date)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    37
    
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    38
    except Exception, e :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    39
        if not options.skip_missing :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    40
            raise
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    41
            
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    42
        if not options.quiet :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    43
            print "Skipped: %s" % (e, )
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    44
    
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    45
    else :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    46
        # insert -> count
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    47
        count = index.insert(channel, lines)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    48
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    49
        if not options.quiet :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    50
            print "OK: %d lines" % count
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
def cmd_load (options, channel_name, *dates) :
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    """
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    54
        Loads the logs for a specific channel for the given dates (in terms of the channe logs' timezone) into the index
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    """
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    57
    # open index/channel
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    58
    index, channel = _open_index_and_channel(options, channel_name, '*' if options.create_index else 'a')
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
    
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    60
    # handle each date
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
    for date_name in dates :
68
8157c41b3236 improve search form & script
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
    62
        try :
8157c41b3236 improve search form & script
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
    63
            # parse date
8157c41b3236 improve search form & script
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
    64
            date = datetime.datetime.strptime(date_name, '%Y-%m-%d').replace(tzinfo=channel.source.tz)
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
68
8157c41b3236 improve search form & script
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
    66
        except Exception, e :
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    67
            print "[ERROR] Invalid date: %s: %s" % (date_name, e)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    68
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    69
            if options.skip_missing :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    70
                continue
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    71
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    72
            else :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    73
                raise
68
8157c41b3236 improve search form & script
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
    74
        
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    75
        # load
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    76
        _load_channel_date(index, options, channel, date)
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    78
def cmd_load_month (options, channel_name, *months) :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    79
    """
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    80
        Loads the logs for a specific channel for the given months (in terms of the channel's timezone) into the index
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    81
    """
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    83
    # open index/channel
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    84
    index, channel = _open_index_and_channel(options, channel_name, '*' if options.create_index else 'a')
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    85
    
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    86
    # handle each date
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    87
    for month_name in months :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    88
        try :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    89
            # parse date
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    90
            month = datetime.datetime.strptime(month_name, '%Y-%m').replace(tzinfo=channel.source.tz)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    91
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    92
        except Exception, e :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    93
            print "[ERROR] Invalid date: %s: %s" % (month_name, e)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    94
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    95
            if options.skip_missing :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    96
                continue
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    97
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    98
            else :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
    99
                raise
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   100
        
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   101
        # get the set of days
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   102
        days = channel.source.get_month_days(month)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   103
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   104
        print "Loading %d days of logs:" % (len(days))
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   105
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   106
        # load each day
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   107
        for date in days :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   108
            # convert to datetime
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   109
            dt = datetime.datetime.combine(date, datetime.time(0)).replace(tzinfo=channel.source.tz)
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   110
            
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   111
            # load
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   112
            _load_channel_date(index, options, channel, dt)
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   113
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   114
def cmd_search (options, channel_name, query) :
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   115
    """
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   116
        Search the index for events on a specific channel with the given query
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   117
    """
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   118
    
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   119
    # sanity-check
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   120
    if options.create_index :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   121
        raise Exception("--create doesn't make sense for 'search'")
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   122
    
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   123
    # open index/channel
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   124
    index, channel = _open_index_and_channel(options, channel_name, 'r')
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   125
    
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   126
    # search
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   127
    lines = index.search_simple(channel, query)
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   128
    
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   129
    # display as plaintext
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   130
    for line in options.formatter.format_txt(lines) :
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   131
        print line
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   132
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   133
if __name__ == '__main__' :
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   134
    from optparse import OptionParser
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   135
    
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   136
    # define parser
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   137
    parser = OptionParser(
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   138
        usage           = "%prog [options] <command> [ ... ]",
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   139
        add_help_option = True,
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   140
    )
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   141
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   142
    # define command-line arguments
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   143
    parser.add_option("-I", "--index", dest="index_path", help="Index database path", metavar="PATH", default="logs/index")
68
8157c41b3236 improve search form & script
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   144
    parser.add_option("--create", dest="create_index", action="store_true", help="Create index database")
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   145
    parser.add_option("-f", "--formatter", dest="formatter_name", help="LogFormatter to use", default="irssi")
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   146
    parser.add_option("-z", "--timezone", dest="tz_name", help="Timezone for output", metavar="TZ", default="UTC")
68
8157c41b3236 improve search form & script
Tero Marttila <terom@fixme.fi>
parents: 65
diff changeset
   147
    parser.add_option("--skip-missing", dest="skip_missing", action="store_true", help="Skip missing logfiles")
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   148
    parser.add_option("--quiet", dest="quiet", action="store_true", help="Supress status messages")
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   149
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   150
    # parse
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   151
    options, args = parser.parse_args()
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   152
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   153
    # postprocess stuff
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   154
    options.tz = pytz.timezone(options.tz_name)
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   155
    options.formatter = config.LOG_FORMATTERS[options.formatter_name](options.tz, "%H:%M:%S", None, None)
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   156
    
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   157
    # pop command
82
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   158
    if not args :
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   159
        raise Exception("Missing command")
afd3120ec71e add a LogSourceDecoder to fallback from utf-8 to latin-1, and improve scripts/search-index.py
Tero Marttila <terom@fixme.fi>
parents: 68
diff changeset
   160
65
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   161
    command = args.pop(0)
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   162
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   163
    # inspect
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   164
    func = globals()['cmd_%s' % command]
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   165
    
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   166
    # call
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   167
    func(options, *args)
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   168
8b50694f841e improve search further
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
   169