paulb@4 | 1 | #!/usr/bin/env python |
paulb@4 | 2 | |
paulb@4 | 3 | from parallel import start, Exchange |
paulb@4 | 4 | from Dict import Indexer, Searcher, update |
paulb@4 | 5 | |
paulb@4 | 6 | def apply_indexer(channel, filename): |
paulb@4 | 7 | print "Indexing", filename |
paulb@4 | 8 | indexer = Indexer(filename) |
paulb@4 | 9 | channel.send(indexer.get_index()) |
paulb@4 | 10 | |
paulb@4 | 11 | def get_searcher(filenames): |
paulb@4 | 12 | master_index = {} |
paulb@4 | 13 | |
paulb@4 | 14 | # Start indexing. |
paulb@4 | 15 | |
paulb@4 | 16 | channels = [] |
paulb@4 | 17 | for filename in filenames: |
paulb@4 | 18 | channels.append(start(apply_indexer, filename)) |
paulb@4 | 19 | |
paulb@4 | 20 | # Start listening for responses. |
paulb@4 | 21 | |
paulb@4 | 22 | exchange = Exchange(channels) |
paulb@4 | 23 | while len(channels) > 0: |
paulb@4 | 24 | print "Waiting for %d channels..." % len(channels) |
paulb@4 | 25 | for channel in exchange.ready(): |
paulb@4 | 26 | index = channel.receive() |
paulb@4 | 27 | update(master_index, index) |
paulb@4 | 28 | channels.remove(channel) |
paulb@4 | 29 | |
paulb@4 | 30 | # Provide a search interface. |
paulb@4 | 31 | |
paulb@4 | 32 | return Searcher(master_index) |
paulb@4 | 33 | |
paulb@4 | 34 | if __name__ == "__main__": |
paulb@4 | 35 | import sys |
paulb@4 | 36 | filenames = sys.argv[1:] |
paulb@4 | 37 | searcher = get_searcher(filenames) |
paulb@4 | 38 | while 1: |
paulb@4 | 39 | print "Pattern:", |
paulb@4 | 40 | pattern = raw_input() |
paulb@4 | 41 | print searcher.find(pattern) |
paulb@4 | 42 | |
paulb@4 | 43 | # vim: tabstop=4 expandtab shiftwidth=4 |