<s> """<STR_LIT>""" <EOL> import sys <EOL> try : <EOL> try : <EOL> from cProfile import Profile <EOL> except ImportError : <EOL> from profile import Profile <EOL> from pstats import Stats <EOL> available = True <EOL> except ImportError : <EOL> available = False <EOL> class MergeStream ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * streams ) : <EOL> if not streams : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . streams = streams <EOL> def write ( self , data ) : <EOL> for stream in self . streams : <EOL> stream . write ( data ) <EOL> class ProfilerMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app , stream = None , <EOL> sort_by = ( '<STR_LIT:time>' , '<STR_LIT>' ) , restrictions = ( ) ) : <EOL> if not available : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . _app = app <EOL> self . _stream = stream or sys . stdout <EOL> self . _sort_by = sort_by <EOL> self . _restrictions = restrictions <EOL> def __call__ ( self , environ , start_response ) : <EOL> response_body = [ ] <EOL> def catching_start_response ( status , headers , exc_info = None ) : <EOL> start_response ( status , headers , exc_info ) <EOL> return response_body . append <EOL> def runapp ( ) : <EOL> appiter = self . _app ( environ , catching_start_response ) <EOL> response_body . extend ( appiter ) <EOL> if hasattr ( appiter , '<STR_LIT>' ) : <EOL> appiter . close ( ) <EOL> p = Profile ( ) <EOL> p . runcall ( runapp ) <EOL> body = '<STR_LIT>' . join ( response_body ) <EOL> stats = Stats ( p , stream = self . _stream ) <EOL> stats . sort_stats ( * self . _sort_by ) <EOL> self . _stream . write ( '<STR_LIT:->' * <NUM_LIT> ) <EOL> self . _stream . write ( '<STR_LIT>' % environ . get ( '<STR_LIT>' ) ) <EOL> stats . print_stats ( * self . _restrictions ) <EOL> self . _stream . write ( '<STR_LIT:->' * <NUM_LIT> + '<STR_LIT>' ) <EOL> return [ body ] <EOL> def make_action ( app_factory , hostname = '<STR_LIT:localhost>' , port = <NUM_LIT> , <EOL> threaded = False , processes = <NUM_LIT:1> , stream = None , <EOL> sort_by = ( '<STR_LIT:time>' , '<STR_LIT>' ) , restrictions = ( ) ) : <EOL> """<STR_LIT>""" <EOL> def action ( hostname = ( '<STR_LIT:h>' , hostname ) , port = ( '<STR_LIT:p>' , port ) , <EOL> threaded = threaded , processes = processes ) : <EOL> """<STR_LIT>""" <EOL> from werkzeug . serving import run_simple <EOL> app = ProfilerMiddleware ( app_factory ( ) , stream , sort_by , restrictions ) <EOL> run_simple ( hostname , port , app , False , None , threaded , processes ) <EOL> return action </s>
