<s> """<STR_LIT>""" <EOL> import logging <EOL> import os <EOL> import re <EOL> import webapp2 <EOL> from furious import context <EOL> from furious . async import Async <EOL> from furious . async import defaults <EOL> class GrepHandler ( webapp2 . RequestHandler ) : <EOL> """<STR_LIT>""" <EOL> def get ( self ) : <EOL> query = self . request . get ( '<STR_LIT>' ) <EOL> curdir = os . getcwd ( ) <EOL> with context . new ( ) : <EOL> build_and_start ( query , curdir ) <EOL> self . response . out . write ( '<STR_LIT>' % query ) <EOL> def log_results ( ) : <EOL> """<STR_LIT>""" <EOL> from furious . context import get_current_async <EOL> async = get_current_async ( ) <EOL> for result in async . result : <EOL> logging . info ( result ) <EOL> def build_and_start ( query , directory ) : <EOL> """<STR_LIT>""" <EOL> Async ( target = grep , args = [ query , directory ] ) . start ( ) <EOL> def grep_file ( query , item ) : <EOL> """<STR_LIT>""" <EOL> return [ '<STR_LIT>' % ( item , line ) for line in open ( item ) <EOL> if re . search ( query , line ) ] <EOL> @ defaults ( callbacks = { '<STR_LIT:success>' : log_results } ) <EOL> def grep ( query , directory ) : <EOL> """<STR_LIT>""" <EOL> dir_contents = os . listdir ( directory ) <EOL> results = [ ] <EOL> for item in dir_contents : <EOL> path = os . path . join ( directory , item ) <EOL> if os . path . isdir ( path ) : <EOL> build_and_start ( query , path ) <EOL> else : <EOL> if item . endswith ( '<STR_LIT>' ) : <EOL> results . extend ( grep_file ( query , path ) ) <EOL> return results </s>
