<s> import logging <EOL> import time <EOL> import itsdangerous <EOL> from flask import current_app , request , abort , g <EOL> from flask_debugtoolbar import module <EOL> from flask_debugtoolbar . panels import DebugPanel <EOL> from flask_debugtoolbar . utils import format_sql <EOL> from peewee import SqliteDatabase <EOL> peewee = logging . getLogger ( '<STR_LIT>' ) <EOL> def query_signer ( ) : <EOL> return itsdangerous . URLSafeSerializer ( current_app . config [ '<STR_LIT>' ] , salt = '<STR_LIT>' ) <EOL> def load_query ( data ) : <EOL> try : <EOL> return query_signer ( ) . loads ( request . args [ '<STR_LIT>' ] ) <EOL> except ( itsdangerous . BadSignature , TypeError ) : <EOL> abort ( <NUM_LIT> ) <EOL> def dump_query ( query , params ) : <EOL> try : <EOL> return query_signer ( ) . dumps ( ( query , params ) ) <EOL> except TypeError : <EOL> return None <EOL> class AmountHandler ( logging . Handler ) : <EOL> def __init__ ( self , * args ) : <EOL> super ( AmountHandler , self ) . __init__ ( * args ) <EOL> self . records = [ ] <EOL> def emit ( self , record ) : <EOL> self . records . append ( ( time . time ( ) - self . time , record ) ) <EOL> self . time = time . time ( ) <EOL> @ property <EOL> def amount ( self ) : <EOL> return len ( self . records ) <EOL> class PeeweeDebugPanel ( DebugPanel ) : <EOL> name = '<STR_LIT>' <EOL> has_content = True <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . handler = AmountHandler ( ) <EOL> peewee . setLevel ( '<STR_LIT>' ) <EOL> peewee . handlers = [ ] <EOL> peewee . addHandler ( self . handler ) <EOL> super ( PeeweeDebugPanel , self ) . __init__ ( * args , ** kwargs ) <EOL> @ property <EOL> def has_content ( self ) : <EOL> return bool ( self . handler . amount ) <EOL> def url ( self ) : <EOL> return '<STR_LIT>' <EOL> def title ( self ) : <EOL> return '<STR_LIT>' <EOL> def nav_title ( self ) : <EOL> return '<STR_LIT>' % self . handler . amount <EOL> def process_request ( self , request ) : <EOL> self . handler . time = time . time ( ) <EOL> def content ( self ) : <EOL> data = [ ] <EOL> for duration , record in self . handler . records : <EOL> sql , params = record . msg <EOL> data . append ( { <EOL> '<STR_LIT>' : dump_query ( sql , params ) , <EOL> '<STR_LIT>' : sql , <EOL> '<STR_LIT>' : duration , <EOL> } ) <EOL> return self . render ( '<STR_LIT>' , { '<STR_LIT>' : data } ) <EOL> @ module . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ module . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] , defaults = dict ( explain = True ) ) <EOL> def sql_select ( explain = False ) : <EOL> statement , params = load_query ( request . args [ '<STR_LIT>' ] ) <EOL> database = current_app . extensions . get ( '<STR_LIT>' ) . database . obj <EOL> if explain : <EOL> if isinstance ( database , SqliteDatabase ) : <EOL> statement = '<STR_LIT>' % statement <EOL> else : <EOL> statement = '<STR_LIT>' % statement <EOL> result = database . execute_sql ( statement , params ) <EOL> headers = [ ] <EOL> data = list ( result . fetchall ( ) ) <EOL> if data : <EOL> headers = [ '<STR_LIT>' for _ in range ( len ( data [ <NUM_LIT:0> ] ) ) ] <EOL> return g . debug_toolbar . render ( '<STR_LIT>' , { <EOL> '<STR_LIT:result>' : data , <EOL> '<STR_LIT>' : headers , <EOL> '<STR_LIT>' : format_sql ( statement , params ) , <EOL> '<STR_LIT>' : float ( request . args [ '<STR_LIT>' ] ) , <EOL> } ) </s>
