<s> """<STR_LIT>""" <EOL> import cgi <EOL> import os <EOL> import pstats <EOL> import string <EOL> import sys <EOL> import threading <EOL> from repoze . profile . compat import bytes_ <EOL> from repoze . profile . compat import parse_qs <EOL> from repoze . profile . compat import profile <EOL> from repoze . profile . compat import StringIO <EOL> from repoze . profile . compat import url_quote <EOL> PY3 = sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> <EOL> HAS_PP2CT = True <EOL> try : <EOL> import pyprof2calltree <EOL> except ImportError : <EOL> HAS_PP2CT = False <EOL> _HERE = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> DEFAULT_PROFILE_LOG = '<STR_LIT>' <EOL> PROFILE_EXEC_EAGER = """<STR_LIT>""" <EOL> PROFILE_EXEC_LAZY = """<STR_LIT>""" <EOL> class ProfileMiddleware ( object ) : <EOL> Stats = pstats . Stats <EOL> def __init__ ( self , app , <EOL> global_conf = None , <EOL> log_filename = DEFAULT_PROFILE_LOG , <EOL> cachegrind_filename = None , <EOL> discard_first_request = True , <EOL> flush_at_shutdown = True , <EOL> path = '<STR_LIT>' , <EOL> unwind = False , <EOL> ) : <EOL> self . exists = os . path . exists <EOL> self . remove = os . remove <EOL> self . app = app <EOL> self . profiler = profile . Profile ( ) <EOL> self . log_filename = log_filename <EOL> self . cachegrind_filename = cachegrind_filename <EOL> self . first_request = discard_first_request <EOL> self . lock = threading . Lock ( ) <EOL> self . flush_at_shutdown = flush_at_shutdown <EOL> self . path = path <EOL> self . unwind = unwind <EOL> def index ( self , request , output = None ) : <EOL> querydata = request . get_params ( ) <EOL> fulldirs = int ( querydata . get ( '<STR_LIT>' , <NUM_LIT:0> ) ) <EOL> sort = querydata . get ( '<STR_LIT>' , '<STR_LIT:time>' ) <EOL> clear = querydata . get ( '<STR_LIT>' , None ) <EOL> filename = querydata . get ( '<STR_LIT:filename>' , '<STR_LIT>' ) . strip ( ) <EOL> limit = int ( querydata . get ( '<STR_LIT>' , <NUM_LIT:100> ) ) <EOL> mode = querydata . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if output is None : <EOL> output = StringIO ( ) <EOL> url = request . get_url ( ) <EOL> log_exists = os . path . exists ( self . log_filename ) <EOL> if clear and log_exists : <EOL> os . remove ( self . log_filename ) <EOL> self . profiler = profile . Profile ( ) <EOL> log_exists = False <EOL> if log_exists : <EOL> stats = self . Stats ( self . log_filename ) <EOL> if not fulldirs : <EOL> stats . strip_dirs ( ) <EOL> stats . sort_stats ( sort ) <EOL> if hasattr ( stats , '<STR_LIT>' ) : <EOL> stats . stream = output <EOL> try : <EOL> orig_stdout = sys . stdout <EOL> sys . stdout = output <EOL> print_fn = getattr ( stats , '<STR_LIT>' % mode ) <EOL> if filename : <EOL> print_fn ( filename , limit ) <EOL> else : <EOL> print_fn ( limit ) <EOL> finally : <EOL> sys . stdout = orig_stdout <EOL> profiledata = output . getvalue ( ) <EOL> description = empty_description <EOL> action = url <EOL> formelements = '<STR_LIT>' <EOL> filename = filename or '<STR_LIT>' <EOL> if profiledata : <EOL> description = """<STR_LIT>""" <EOL> sort_repl = '<STR_LIT>' % sort <EOL> sort_selected = '<STR_LIT>' % sort <EOL> sort = sort_tmpl . replace ( sort_repl , sort_selected ) <EOL> limit_repl = '<STR_LIT>' % limit <EOL> limit_selected = '<STR_LIT>' % limit <EOL> limit = limit_tmpl . replace ( limit_repl , limit_selected ) <EOL> mode_repl = '<STR_LIT>' % mode <EOL> mode_selected = '<STR_LIT>' % mode <EOL> mode = mode_tmpl . replace ( mode_repl , mode_selected ) <EOL> fulldirs_checked = '<STR_LIT>' <EOL> fulldirs_repl = '<STR_LIT>' <EOL> if fulldirs : <EOL> fulldirs_checked = '<STR_LIT>' <EOL> fulldirs = fulldirs_tmpl . replace ( fulldirs_repl , fulldirs_checked ) <EOL> filename_repl = '<STR_LIT>' <EOL> filename_selected = '<STR_LIT>' % filename <EOL> filename = filename_tmpl . replace ( filename_repl , filename_selected ) <EOL> fulldirs_repl <EOL> formelements = string . Template ( formelements_tmpl ) <EOL> formelements = formelements . substitute ( <EOL> { '<STR_LIT:description>' : description , <EOL> '<STR_LIT:action>' : action , <EOL> '<STR_LIT>' : sort , <EOL> '<STR_LIT>' : limit , <EOL> '<STR_LIT>' : fulldirs , <EOL> '<STR_LIT>' : mode , <EOL> '<STR_LIT:filename>' : filename , <EOL> } <EOL> ) <EOL> index = string . Template ( index_tmpl ) <EOL> index = index . substitute ( <EOL> { '<STR_LIT>' : formelements , <EOL> '<STR_LIT:action>' : action , <EOL> '<STR_LIT:description>' : description , <EOL> '<STR_LIT>' : profiledata , <EOL> } <EOL> ) <EOL> return index <EOL> def __del__ ( self ) : <EOL> if self . flush_at_shutdown and self . exists ( self . log_filename ) : <EOL> self . remove ( self . log_filename ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> request = MiniRequest ( environ ) <EOL> if request . path_info == self . path : <EOL> self . lock . acquire ( ) <EOL> try : <EOL> text = self . index ( request ) <EOL> finally : <EOL> self . lock . release ( ) <EOL> start_response ( '<STR_LIT>' , [ <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , str ( len ( text ) ) ) ] ) <EOL> return [ bytes_ ( text ) ] <EOL> self . lock . acquire ( ) <EOL> try : <EOL> _locals = locals ( ) <EOL> code = self . unwind and PROFILE_EXEC_EAGER or PROFILE_EXEC_LAZY <EOL> self . profiler . runctx ( code , globals ( ) , _locals ) <EOL> if self . first_request : <EOL> self . profiler = profile . Profile ( ) <EOL> self . first_request = False <EOL> else : <EOL> self . profiler . dump_stats ( self . log_filename ) <EOL> if HAS_PP2CT and self . cachegrind_filename is not None : <EOL> stats = pstats . Stats ( self . profiler ) <EOL> conv = pyprof2calltree . CalltreeConverter ( stats ) <EOL> with open ( self . cachegrind_filename , '<STR_LIT:w>' ) as grind : <EOL> conv . output ( grind ) <EOL> app_iter = _locals [ '<STR_LIT>' ] <EOL> return app_iter <EOL> finally : <EOL> self . lock . release ( ) <EOL> def boolean ( s ) : <EOL> if s == True : <EOL> return True <EOL> s = s . lower ( ) <EOL> if ( s . startswith ( '<STR_LIT:t>' ) or s . startswith ( '<STR_LIT:y>' ) or <EOL> s . startswith ( '<STR_LIT:1>' ) or s . startswith ( '<STR_LIT>' ) ) : <EOL> return True <EOL> return False <EOL> empty_description = """<STR_LIT>""" <EOL> sort_tmpl = """<STR_LIT>""" <EOL> limit_tmpl = """<STR_LIT>""" <EOL> fulldirs_tmpl = """<STR_LIT>""" <EOL> mode_tmpl = """<STR_LIT>""" <EOL> filename_tmpl = """<STR_LIT>""" <EOL> formelements_tmpl = """<STR_LIT>""" <EOL> index_tmpl = """<STR_LIT>""" <EOL> PATH_SAFE = '<STR_LIT>' <EOL> class MiniRequest ( object ) : <EOL> def __init__ ( self , environ ) : <EOL> self . environ = environ <EOL> self . path_info = environ [ '<STR_LIT>' ] <EOL> def get_url ( self ) : <EOL> e = self . environ <EOL> url = e [ '<STR_LIT>' ] + '<STR_LIT>' <EOL> if e . get ( '<STR_LIT>' ) : <EOL> host = e [ '<STR_LIT>' ] <EOL> if '<STR_LIT::>' in host : <EOL> host , port = host . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> else : <EOL> port = None <EOL> else : <EOL> host = e [ '<STR_LIT>' ] <EOL> port = e [ '<STR_LIT>' ] <EOL> if self . environ [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> if port == '<STR_LIT>' : <EOL> port = None <EOL> elif self . environ [ '<STR_LIT>' ] == '<STR_LIT:http>' : <EOL> if port == '<STR_LIT>' : <EOL> port = None <EOL> url += host <EOL> if port : <EOL> url += '<STR_LIT>' % port <EOL> url += url_quote ( <EOL> self . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) , PATH_SAFE ) <EOL> url += url_quote ( <EOL> self . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) , PATH_SAFE ) <EOL> if self . environ . get ( '<STR_LIT>' ) : <EOL> url += '<STR_LIT:?>' + self . environ [ '<STR_LIT>' ] <EOL> return url <EOL> def get_params ( self ) : <EOL> params = { } <EOL> fs = cgi . FieldStorage ( <EOL> fp = self . environ [ '<STR_LIT>' ] , <EOL> environ = self . environ , <EOL> keep_blank_values = True ) <EOL> for field in fs . list or ( ) : <EOL> name = field . name <EOL> value = field . value <EOL> params [ name ] = value <EOL> get_params = parse_qs ( self . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> keep_blank_values = True , <EOL> strict_parsing = False ) <EOL> params . update ( get_params ) <EOL> return params <EOL> AccumulatingProfileMiddleware = ProfileMiddleware <EOL> def make_profile_middleware ( app , <EOL> global_conf , <EOL> log_filename = DEFAULT_PROFILE_LOG , <EOL> cachegrind_filename = None , <EOL> discard_first_request = '<STR_LIT:true>' , <EOL> path = '<STR_LIT>' , <EOL> flush_at_shutdown = '<STR_LIT:true>' , <EOL> unwind = '<STR_LIT:false>' , <EOL> ) : <EOL> """<STR_LIT>""" <EOL> flush_at_shutdown = boolean ( flush_at_shutdown ) <EOL> discard_first_request = boolean ( discard_first_request ) <EOL> unwind = boolean ( unwind ) <EOL> return ProfileMiddleware ( <EOL> app , <EOL> log_filename = log_filename , <EOL> cachegrind_filename = cachegrind_filename , <EOL> discard_first_request = discard_first_request , <EOL> flush_at_shutdown = flush_at_shutdown , <EOL> path = path , <EOL> unwind = unwind , <EOL> ) </s>
