<s> import os <EOL> from flask import current_app , request , g <EOL> from flask . globals import _request_ctx_stack <EOL> from flask import send_from_directory <EOL> from jinja2 import Environment , PackageLoader <EOL> from werkzeug . exceptions import HTTPException <EOL> from werkzeug . urls import url_quote_plus <EOL> from flask_debugtoolbar . toolbar import DebugToolbar <EOL> from flask import Blueprint <EOL> module = Blueprint ( '<STR_LIT>' , __name__ ) <EOL> def replace_insensitive ( string , target , replacement ) : <EOL> """<STR_LIT>""" <EOL> no_case = string . lower ( ) <EOL> index = no_case . rfind ( target . lower ( ) ) <EOL> if index >= <NUM_LIT:0> : <EOL> return string [ : index ] + replacement + string [ index + len ( target ) : ] <EOL> else : <EOL> return string <EOL> def _printable ( value ) : <EOL> if isinstance ( value , unicode ) : <EOL> return value . encode ( '<STR_LIT>' ) <EOL> elif isinstance ( value , str ) : <EOL> return value . encode ( '<STR_LIT>' ) <EOL> else : <EOL> return repr ( value ) <EOL> class DebugToolbarExtension ( object ) : <EOL> _static_dir = os . path . realpath ( <EOL> os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> _redirect_codes = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> def __init__ ( self , app ) : <EOL> self . app = app <EOL> self . debug_toolbars = { } <EOL> self . hosts = ( ) <EOL> if not app . config . get ( '<STR_LIT>' , app . debug ) : <EOL> return <EOL> if not app . config . get ( '<STR_LIT>' ) : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> DebugToolbar . load_panels ( app ) <EOL> self . hosts = app . config . get ( '<STR_LIT>' , ( ) ) <EOL> self . app . before_request ( self . process_request ) <EOL> self . app . after_request ( self . process_response ) <EOL> self . app . teardown_request ( self . teardown_request ) <EOL> app . dispatch_request = self . dispatch_request <EOL> self . jinja_env = Environment ( <EOL> autoescape = True , <EOL> extensions = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> loader = PackageLoader ( __name__ , '<STR_LIT>' ) ) <EOL> self . jinja_env . filters [ '<STR_LIT>' ] = url_quote_plus <EOL> self . jinja_env . filters [ '<STR_LIT>' ] = _printable <EOL> app . add_url_rule ( '<STR_LIT>' , <EOL> '<STR_LIT>' , self . send_static_file ) <EOL> app . register_blueprint ( module , url_prefix = '<STR_LIT>' ) <EOL> def dispatch_request ( self ) : <EOL> """<STR_LIT>""" <EOL> req = _request_ctx_stack . top . request <EOL> app = current_app <EOL> if req . routing_exception is not None : <EOL> app . raise_routing_exception ( req ) <EOL> rule = req . url_rule <EOL> if getattr ( rule , '<STR_LIT>' , False ) and req . method == '<STR_LIT>' : <EOL> return app . make_default_options_response ( ) <EOL> view_func = app . view_functions [ rule . endpoint ] <EOL> view_func = self . process_view ( app , view_func , req . view_args ) <EOL> return view_func ( ** req . view_args ) <EOL> def _show_toolbar ( self ) : <EOL> """<STR_LIT>""" <EOL> if request . path . startswith ( '<STR_LIT>' ) : <EOL> return False <EOL> if self . hosts and request . remote_addr not in self . hosts : <EOL> return False <EOL> return True <EOL> def send_static_file ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> return send_from_directory ( self . _static_dir , filename ) <EOL> def process_request ( self ) : <EOL> g . debug_toolbar = self <EOL> if not self . _show_toolbar ( ) : <EOL> return <EOL> real_request = request . _get_current_object ( ) <EOL> self . debug_toolbars [ real_request ] = DebugToolbar ( real_request , self . jinja_env ) <EOL> for panel in self . debug_toolbars [ real_request ] . panels : <EOL> panel . process_request ( real_request ) <EOL> def process_view ( self , app , view_func , view_kwargs ) : <EOL> """<STR_LIT>""" <EOL> real_request = request . _get_current_object ( ) <EOL> if real_request in self . debug_toolbars : <EOL> for panel in self . debug_toolbars [ real_request ] . panels : <EOL> new_view = panel . process_view ( real_request , view_func , view_kwargs ) <EOL> if new_view : <EOL> view_func = new_view <EOL> return view_func <EOL> def process_response ( self , response ) : <EOL> real_request = request . _get_current_object ( ) <EOL> if real_request not in self . debug_toolbars : <EOL> return response <EOL> if self . debug_toolbars [ real_request ] . config [ '<STR_LIT>' ] : <EOL> if response . status_code in self . _redirect_codes : <EOL> redirect_to = response . location <EOL> redirect_code = response . status_code <EOL> if redirect_to : <EOL> content = self . render ( '<STR_LIT>' , { <EOL> '<STR_LIT>' : redirect_to , <EOL> '<STR_LIT>' : redirect_code <EOL> } ) <EOL> response . content_length = len ( content ) <EOL> response . location = None <EOL> response . response = [ content ] <EOL> response . status_code = <NUM_LIT:200> <EOL> if ( response . status_code == <NUM_LIT:200> <EOL> and response . headers [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) ) : <EOL> for panel in self . debug_toolbars [ real_request ] . panels : <EOL> panel . process_response ( real_request , response ) <EOL> if response . is_sequence : <EOL> response_html = response . data . decode ( response . charset ) <EOL> toolbar_html = self . debug_toolbars [ real_request ] . render_toolbar ( ) <EOL> content = replace_insensitive ( <EOL> response_html , '<STR_LIT>' , toolbar_html + '<STR_LIT>' ) <EOL> content = content . encode ( response . charset ) <EOL> response . response = [ content ] <EOL> response . content_length = len ( content ) <EOL> return response <EOL> def teardown_request ( self , exc ) : <EOL> self . debug_toolbars . pop ( request . _get_current_object ( ) , None ) <EOL> def render ( self , template_name , context ) : <EOL> template = self . jinja_env . get_template ( template_name ) <EOL> return template . render ( ** context ) </s>
