<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> import datetime <EOL> import logging <EOL> import socket <EOL> import uuid <EOL> import os <EOL> from functools import wraps <EOL> from appenlight_client import __version__ , __protocol_version__ <EOL> from appenlight_client . exceptions import get_current_traceback <EOL> from appenlight_client . ext . logging import register_logging , unregister_logger <EOL> from appenlight_client . timing import get_local_storage <EOL> from appenlight_client . utils import asbool , aslist , import_from_module <EOL> from appenlight_client . utils import parse_tag , PY3 <EOL> from webob import Request <EOL> if PY3 : <EOL> import configparser <EOL> else : <EOL> import ConfigParser <EOL> LEVELS = { '<STR_LIT>' : logging . DEBUG , <EOL> '<STR_LIT:info>' : logging . INFO , <EOL> '<STR_LIT>' : logging . WARNING , <EOL> '<STR_LIT:error>' : logging . ERROR , <EOL> '<STR_LIT>' : logging . CRITICAL } <EOL> log = logging . getLogger ( __name__ ) <EOL> def singleton ( cls ) : <EOL> def getinstance ( * args , ** kwargs ) : <EOL> if cls . self_instance is None : <EOL> cls . self_instance = cls ( * args , ** kwargs ) <EOL> return cls . self_instance <EOL> return getinstance <EOL> class BaseClient ( object ) : <EOL> self_instance = None <EOL> __version__ = __version__ <EOL> __protocol_version__ = __protocol_version__ <EOL> def __init__ ( self , config = None , register_timing = True ) : <EOL> """<STR_LIT>""" <EOL> self . uuid = uuid . uuid4 ( ) <EOL> self . log_handlers = [ ] <EOL> self . update_config ( config ) <EOL> self . reinitialize ( ) <EOL> def update_config ( self , config ) : <EOL> self . config = { } <EOL> self . config [ '<STR_LIT>' ] = asbool ( config . get ( '<STR_LIT>' , True ) ) <EOL> self . config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' ) or socket . getfqdn ( ) <EOL> if PY3 : <EOL> default_client = '<STR_LIT>' <EOL> else : <EOL> default_client = '<STR_LIT>' <EOL> self . config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , default_client ) <EOL> self . config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' ) <EOL> if not self . config [ '<STR_LIT>' ] : <EOL> self . config [ '<STR_LIT>' ] = False <EOL> logging . warning ( "<STR_LIT>" ) <EOL> self . config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> self . config [ '<STR_LIT>' ] = asbool ( <EOL> config . get ( '<STR_LIT>' , True ) ) <EOL> self . config [ '<STR_LIT>' ] = asbool ( <EOL> config . get ( '<STR_LIT>' , True ) ) <EOL> self . config [ '<STR_LIT>' ] = float ( <EOL> config . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> if self . config [ '<STR_LIT>' ] < <NUM_LIT> : <EOL> self . config [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . config [ '<STR_LIT>' ] = datetime . timedelta ( <EOL> seconds = self . config [ '<STR_LIT>' ] ) <EOL> self . config [ '<STR_LIT>' ] = asbool ( config . get ( '<STR_LIT>' , True ) ) <EOL> self . config [ '<STR_LIT>' ] = asbool ( config . get ( '<STR_LIT>' , True ) ) <EOL> self . config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) <EOL> self . config [ '<STR_LIT>' ] = asbool ( <EOL> config . get ( '<STR_LIT>' , False ) ) <EOL> self . config [ '<STR_LIT>' ] = asbool ( config . get ( '<STR_LIT>' , <EOL> False ) ) <EOL> self . config [ '<STR_LIT>' ] = asbool ( <EOL> config . get ( '<STR_LIT>' , True ) ) <EOL> self . config [ '<STR_LIT>' ] = asbool ( <EOL> config . get ( '<STR_LIT>' , True ) ) <EOL> self . config [ '<STR_LIT>' ] = asbool ( <EOL> config . get ( '<STR_LIT>' , True ) ) <EOL> self . config [ '<STR_LIT>' ] = int ( <EOL> config . get ( '<STR_LIT>' , <NUM_LIT:5> ) ) <EOL> self . config [ '<STR_LIT>' ] = asbool ( <EOL> config . get ( '<STR_LIT>' , False ) ) <EOL> self . config [ '<STR_LIT>' ] = asbool ( config . get ( '<STR_LIT>' , <EOL> False ) ) <EOL> self . config [ '<STR_LIT>' ] = [ '<STR_LIT:password>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> req_blacklist = aslist ( config . get ( '<STR_LIT>' , <EOL> config . get ( <EOL> '<STR_LIT>' ) ) , <EOL> '<STR_LIT:U+002C>' ) <EOL> self . config [ '<STR_LIT>' ] . extend ( <EOL> filter ( lambda x : x , req_blacklist ) <EOL> ) <EOL> self . config [ '<STR_LIT>' ] = [ ] <EOL> cookie_whitelist = aslist ( config . get ( '<STR_LIT>' , <EOL> config . get ( <EOL> '<STR_LIT>' ) ) , <EOL> '<STR_LIT:U+002C>' ) <EOL> self . config [ '<STR_LIT>' ] . extend ( <EOL> filter ( lambda x : x , cookie_whitelist ) <EOL> ) <EOL> if config . get ( '<STR_LIT>' ) : <EOL> log . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . config [ '<STR_LIT>' ] = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> environ_whitelist = aslist ( <EOL> config . get ( '<STR_LIT>' ) , '<STR_LIT:U+002C>' ) <EOL> self . config [ '<STR_LIT>' ] . extend ( <EOL> filter ( lambda x : x , environ_whitelist ) ) <EOL> self . config [ '<STR_LIT>' ] = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> log_blacklist = aslist ( <EOL> config . get ( '<STR_LIT>' ) , '<STR_LIT:U+002C>' ) <EOL> self . config [ '<STR_LIT>' ] . extend ( filter ( <EOL> lambda x : x , log_blacklist ) ) <EOL> self . config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' ) <EOL> if self . config [ '<STR_LIT>' ] < <NUM_LIT:1> : <EOL> self . config [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> self . config [ '<STR_LIT>' ] = datetime . timedelta ( seconds = self . config [ '<STR_LIT>' ] ) <EOL> if self . config [ '<STR_LIT>' ] and self . config [ '<STR_LIT>' ] : <EOL> self . config [ '<STR_LIT>' ] = config . get ( '<STR_LIT>' , { } ) <EOL> for k , v in config . items ( ) : <EOL> if k . startswith ( '<STR_LIT>' ) : <EOL> try : <EOL> self . config [ '<STR_LIT>' ] [ k [ <NUM_LIT> : ] ] = float ( v ) <EOL> except ( TypeError , ValueError ) as e : <EOL> self . config [ '<STR_LIT>' ] [ k [ <NUM_LIT> : ] ] = False <EOL> self . hooks_blacklist = aslist ( config . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:U+002C>' ) <EOL> def reinitialize ( self ) : <EOL> self . filter_callable = lambda x : x <EOL> if self . config [ '<STR_LIT>' ] : <EOL> try : <EOL> parts = self . config [ '<STR_LIT>' ] . split ( '<STR_LIT::>' ) <EOL> _tmp = __import__ ( parts [ <NUM_LIT:0> ] , globals ( ) , locals ( ) , <EOL> [ parts [ <NUM_LIT:1> ] , ] , <NUM_LIT:0> ) <EOL> self . filter_callable = getattr ( _tmp , parts [ <NUM_LIT:1> ] ) <EOL> except ImportError as e : <EOL> self . filter_callable = self . data_filter <EOL> msg = '<STR_LIT>' % e <EOL> log . error ( msg ) <EOL> else : <EOL> self . filter_callable = self . data_filter <EOL> self . unregister_logger ( ) <EOL> if self . config [ '<STR_LIT>' ] and self . config [ '<STR_LIT>' ] : <EOL> self . register_logger ( ) <EOL> if self . config [ '<STR_LIT>' ] and self . config [ '<STR_LIT>' ] : <EOL> import appenlight_client . timing <EOL> appenlight_client . timing . register_timing ( self . config ) <EOL> self . hooks = [ '<STR_LIT>' ] <EOL> if self . config [ '<STR_LIT>' ] : <EOL> self . register_hooks ( ) <EOL> selected_transport = import_from_module ( self . config [ '<STR_LIT>' ] ) <EOL> if not selected_transport : <EOL> from appenlight_client . transports . requests import HTTPTransport as selected_transport <EOL> msg = '<STR_LIT>' % ( self . config [ '<STR_LIT>' ] , e ) <EOL> log . error ( msg ) <EOL> self . transport = selected_transport ( self . config [ '<STR_LIT>' ] , <EOL> self . config ) <EOL> def register_logger ( self , logger = logging . root ) : <EOL> handler_cls = import_from_module ( '<STR_LIT>' ) <EOL> log_handler = register_logging ( logger , <EOL> client_config = self . config , <EOL> cls = handler_cls ) <EOL> level = LEVELS . get ( self . config [ '<STR_LIT>' ] , logging . WARNING ) <EOL> log_handler . setLevel ( level ) <EOL> self . log_handlers . append ( log_handler ) <EOL> def unregister_logger ( self , logger = logging . root ) : <EOL> for handler in list ( self . log_handlers ) : <EOL> unregister_logger ( logger , handler ) <EOL> self . log_handlers . remove ( handler ) <EOL> def register_hooks ( self ) : <EOL> for hook in self . hooks : <EOL> if hook in self . hooks_blacklist : <EOL> log . debug ( '<STR_LIT>' % hook ) <EOL> continue <EOL> try : <EOL> e_callable = import_from_module ( <EOL> '<STR_LIT>' % hook ) <EOL> if e_callable : <EOL> e_callable ( ) <EOL> except Exception : <EOL> raise <EOL> log . warning ( "<STR_LIT>" % hook ) <EOL> def log_handlers_get_records ( self ) : <EOL> appenlight_storage = get_local_storage ( ) <EOL> return appenlight_storage . logs <EOL> def log_handlers_clear_records ( self ) : <EOL> appenlight_storage = get_local_storage ( ) <EOL> appenlight_storage . logs = [ ] <EOL> def check_if_deliver ( self , force_send = False ) : <EOL> return self . transport . check_if_deliver ( force_send = force_send ) <EOL> def purge_data ( self ) : <EOL> storage = get_local_storage ( ) <EOL> storage . clear ( ) <EOL> self . log_handlers_clear_records ( ) <EOL> self . transport . purge ( ) <EOL> def data_filter ( self , structure , section = None ) : <EOL> def filter_dict ( f_input , dict_method ) : <EOL> for k in dict_method ( ) : <EOL> for bad_key in self . config [ '<STR_LIT>' ] : <EOL> if bad_key in k . lower ( ) : <EOL> f_input [ k ] = u'<STR_LIT>' <EOL> keys_to_check = ( ) <EOL> if section in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> keys_to_check = ( <EOL> structure [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) , <EOL> structure [ '<STR_LIT>' ] . get ( '<STR_LIT:POST>' ) , <EOL> ) <EOL> for source in filter ( None , keys_to_check ) : <EOL> if hasattr ( source , '<STR_LIT>' ) : <EOL> filter_dict ( source , source . iterkeys ) <EOL> elif hasattr ( source , '<STR_LIT>' ) : <EOL> filter_dict ( source , source . keys ) <EOL> frameinfo = structure . get ( '<STR_LIT>' ) <EOL> if frameinfo : <EOL> for f in frameinfo : <EOL> for source in f . get ( '<STR_LIT>' , [ ] ) : <EOL> if hasattr ( source [ <NUM_LIT:1> ] , '<STR_LIT>' ) : <EOL> filter_dict ( source [ <NUM_LIT:1> ] , source [ <NUM_LIT:1> ] . iterkeys ) <EOL> elif hasattr ( source , '<STR_LIT>' ) : <EOL> filter_dict ( source [ <NUM_LIT:1> ] , source [ <NUM_LIT:1> ] . keys ) <EOL> else : <EOL> for bad_key in self . config [ '<STR_LIT>' ] : <EOL> if bad_key in source [ <NUM_LIT:0> ] . lower ( ) : <EOL> source [ <NUM_LIT:1> ] = u'<STR_LIT>' <EOL> return structure <EOL> def py_report ( self , environ , traceback = None , message = None , http_status = <NUM_LIT:200> , <EOL> start_time = None , end_time = None , request_stats = None , <EOL> slow_calls = None ) : <EOL> if not request_stats : <EOL> request_stats = { } <EOL> report_data , appenlight_info = self . create_report_structure ( environ , <EOL> traceback , <EOL> server = self . config [ '<STR_LIT>' ] , <EOL> http_status = http_status , <EOL> include_params = True ) <EOL> report_data = self . filter_callable ( report_data , '<STR_LIT>' ) <EOL> url = report_data [ '<STR_LIT:url>' ] <EOL> if not PY3 : <EOL> url = url . decode ( '<STR_LIT:utf8>' , '<STR_LIT:ignore>' ) <EOL> report_data [ '<STR_LIT>' ] = request_stats <EOL> if traceback : <EOL> try : <EOL> log . warning ( '<STR_LIT>' % ( http_status , <EOL> report_data . get ( '<STR_LIT:error>' ) . encode ( '<STR_LIT:utf8>' , '<STR_LIT:ignore>' ) , <EOL> url . encode ( '<STR_LIT:utf8>' , '<STR_LIT:ignore>' ) , ) ) <EOL> log . error ( <EOL> report_data . get ( '<STR_LIT:error>' , '<STR_LIT>' ) . encode ( '<STR_LIT:utf8>' , '<STR_LIT:ignore>' ) ) <EOL> log . error ( traceback . plaintext . encode ( '<STR_LIT:utf8>' , '<STR_LIT:ignore>' ) ) <EOL> except Exception : <EOL> pass <EOL> del traceback <EOL> report_data [ '<STR_LIT>' ] = start_time <EOL> report_data [ '<STR_LIT>' ] = end_time <EOL> report_data [ '<STR_LIT>' ] = request_stats <EOL> report_data [ '<STR_LIT>' ] = [ ] <EOL> if slow_calls : <EOL> for record in slow_calls : <EOL> r = dict ( getattr ( record , '<STR_LIT>' , record ) ) <EOL> r . pop ( '<STR_LIT>' , None ) <EOL> r . pop ( '<STR_LIT>' , None ) <EOL> r . pop ( '<STR_LIT:count>' , None ) <EOL> r . pop ( '<STR_LIT>' , None ) <EOL> r [ '<STR_LIT:start>' ] = datetime . datetime . utcfromtimestamp ( r [ '<STR_LIT:start>' ] ) <EOL> r [ '<STR_LIT:end>' ] = datetime . datetime . utcfromtimestamp ( r [ '<STR_LIT:end>' ] ) <EOL> report_data [ '<STR_LIT>' ] . append ( r ) <EOL> try : <EOL> log . info ( '<STR_LIT>' % url . encode ( '<STR_LIT:utf8>' , '<STR_LIT:ignore>' ) ) <EOL> except Exception : <EOL> pass <EOL> self . transport . feed_report ( report_data ) <EOL> return True <EOL> def py_log ( self , environ , records = None , r_uuid = None , created_report = None ) : <EOL> if not records : <EOL> records = self . log_handlers_get_records ( ) <EOL> self . log_handlers_clear_records ( ) <EOL> if not environ . get ( '<STR_LIT>' ) and ( self . config [ '<STR_LIT>' ] and created_report is None ) : <EOL> return False <EOL> count = <NUM_LIT:0> <EOL> for record in records : <EOL> count += <NUM_LIT:1> <EOL> record [ '<STR_LIT>' ] = r_uuid <EOL> self . transport . feed_log ( record ) <EOL> log . debug ( '<STR_LIT>' % count ) <EOL> return True <EOL> def save_request_stats ( self , stats , view_name = None ) : <EOL> if not view_name : <EOL> view_name = '<STR_LIT>' <EOL> self . transport . save_request_stats ( stats , view_name ) <EOL> def process_environ ( self , environ , traceback = None , include_params = False , <EOL> http_status = <NUM_LIT:200> ) : <EOL> parsed_environ = { } <EOL> appenlight_info = { } <EOL> if '<STR_LIT>' in environ or '<STR_LIT>' in environ or '<STR_LIT>' in environ : <EOL> if '<STR_LIT>' not in environ : <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT:http>' <EOL> environ [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> try : <EOL> req = Request ( environ ) <EOL> except Exception : <EOL> req = None <EOL> else : <EOL> req = None <EOL> for key , value in environ . items ( ) : <EOL> if key . startswith ( '<STR_LIT>' ) and key not in ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) : <EOL> appenlight_info [ key [ <NUM_LIT:11> : ] ] = unicode ( value ) <EOL> elif key == '<STR_LIT>' : <EOL> appenlight_info [ '<STR_LIT>' ] = [ ] <EOL> for k , v in value . iteritems ( ) : <EOL> try : <EOL> appenlight_info [ '<STR_LIT>' ] . append ( parse_tag ( k , v ) ) <EOL> except Exception as e : <EOL> log . info ( u'<STR_LIT>' % e ) <EOL> elif key == '<STR_LIT>' : <EOL> appenlight_info [ '<STR_LIT>' ] = [ ] <EOL> for k , v in value . iteritems ( ) : <EOL> try : <EOL> appenlight_info [ '<STR_LIT>' ] . append ( parse_tag ( k , v ) ) <EOL> except Exception as e : <EOL> log . info ( u'<STR_LIT>' % e ) <EOL> else : <EOL> whitelisted = key . startswith ( '<STR_LIT>' ) or key in self . config [ <EOL> '<STR_LIT>' ] <EOL> if http_status not in [ <NUM_LIT> , '<STR_LIT>' ] and whitelisted : <EOL> try : <EOL> if isinstance ( value , str ) : <EOL> if PY3 : <EOL> parsed_environ [ key ] = value <EOL> else : <EOL> parsed_environ [ key ] = value . decode ( '<STR_LIT:utf8>' ) <EOL> else : <EOL> parsed_environ [ key ] = unicode ( value ) <EOL> except Exception : <EOL> pass <EOL> try : <EOL> parsed_environ [ '<STR_LIT>' ] = req . method <EOL> except Exception : <EOL> pass <EOL> if include_params and req : <EOL> try : <EOL> parsed_environ [ '<STR_LIT>' ] = dict ( [ ( k , v , ) for k , v in req . cookies . items ( ) <EOL> if k in self . config [ '<STR_LIT>' ] ] ) <EOL> except Exception : <EOL> parsed_environ [ '<STR_LIT>' ] = { } <EOL> try : <EOL> parsed_environ [ '<STR_LIT:GET>' ] = dict ( [ ( k , req . GET . getall ( k ) , ) for k in req . GET ] ) <EOL> except Exception : <EOL> parsed_environ [ '<STR_LIT:GET>' ] = { } <EOL> try : <EOL> parsed_environ [ '<STR_LIT:POST>' ] = { } <EOL> post_vars = req . environ . get ( '<STR_LIT>' , None ) <EOL> if post_vars is None : <EOL> post_vars = req . POST . mixed ( ) <EOL> if hasattr ( post_vars , '<STR_LIT>' ) : <EOL> post_vars = post_vars . to_dict ( flat = False ) . items ( ) <EOL> elif hasattr ( post_vars , '<STR_LIT>' ) : <EOL> post_vars = post_vars . lists ( ) <EOL> else : <EOL> post_vars = post_vars . items ( ) <EOL> for k , v in post_vars : <EOL> try : <EOL> if isinstance ( v , basestring ) : <EOL> parsed_environ [ '<STR_LIT:POST>' ] [ k ] = v <EOL> else : <EOL> try : <EOL> parsed_environ [ '<STR_LIT:POST>' ] [ k ] = [ unicode ( val ) for val in v ] <EOL> except Exception : <EOL> parsed_environ [ '<STR_LIT:POST>' ] [ k ] = unicode ( v ) <EOL> except Exception as e : <EOL> pass <EOL> except Exception as e : <EOL> parsed_environ [ '<STR_LIT:POST>' ] = { } <EOL> if environ . get ( "<STR_LIT>" ) : <EOL> remote_addr = environ . get ( "<STR_LIT>" ) . split ( '<STR_LIT:U+002C>' ) [ <EOL> <NUM_LIT:0> ] . strip ( ) <EOL> else : <EOL> remote_addr = ( <EOL> environ . get ( "<STR_LIT>" ) or environ . get ( '<STR_LIT>' ) ) <EOL> parsed_environ [ '<STR_LIT>' ] = environ . get ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> parsed_environ [ '<STR_LIT>' ] = remote_addr <EOL> if '<STR_LIT:message>' in appenlight_info : <EOL> appenlight_info [ '<STR_LIT>' ] . append ( ( '<STR_LIT:message>' , appenlight_info [ '<STR_LIT:message>' ] ) , ) <EOL> try : <EOL> username = appenlight_info . get ( '<STR_LIT:username>' , environ . get ( '<STR_LIT>' , u'<STR_LIT>' ) ) <EOL> appenlight_info [ '<STR_LIT:username>' ] = u'<STR_LIT:%s>' % username <EOL> except ( UnicodeEncodeError , UnicodeDecodeError ) : <EOL> appenlight_info [ '<STR_LIT:username>' ] = "<STR_LIT>" <EOL> try : <EOL> appenlight_info [ '<STR_LIT>' ] = req . url <EOL> except ( UnicodeEncodeError , UnicodeDecodeError ) : <EOL> appenlight_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> except Exception : <EOL> appenlight_info [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return parsed_environ , appenlight_info <EOL> def create_report_structure ( self , environ , traceback = None , message = None , <EOL> http_status = <NUM_LIT:200> , server = '<STR_LIT>' , <EOL> include_params = False ) : <EOL> if environ is None : <EOL> environ = { } <EOL> ( parsed_environ , appenlight_info ) = self . process_environ ( <EOL> environ , <EOL> traceback , <EOL> include_params , <EOL> http_status ) <EOL> report_data = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> report_data [ '<STR_LIT:error>' ] = '<STR_LIT>' <EOL> if traceback : <EOL> exception_text = traceback . exception <EOL> report_data [ '<STR_LIT:error>' ] = exception_text <EOL> local_vars = ( self . config [ '<STR_LIT>' ] or <EOL> environ . get ( '<STR_LIT>' ) ) <EOL> skip_existing = self . config [ '<STR_LIT>' ] <EOL> report_data [ '<STR_LIT>' ] = traceback . frameinfo ( <EOL> include_vars = local_vars , skip_existing = skip_existing ) <EOL> report_data [ '<STR_LIT>' ] = http_status <EOL> if http_status == <NUM_LIT> : <EOL> report_data [ '<STR_LIT:error>' ] = '<STR_LIT>' <EOL> report_data [ '<STR_LIT>' ] = <NUM_LIT:5> <EOL> report_data [ '<STR_LIT>' ] = ( server or <EOL> environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> report_data [ '<STR_LIT>' ] = parsed_environ <EOL> report_data [ '<STR_LIT>' ] = parsed_environ . get ( '<STR_LIT>' , u'<STR_LIT>' ) <EOL> report_data [ '<STR_LIT>' ] = parsed_environ [ '<STR_LIT>' ] <EOL> report_data [ '<STR_LIT:username>' ] = appenlight_info . pop ( '<STR_LIT:username>' ) <EOL> report_data [ '<STR_LIT:url>' ] = appenlight_info . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if '<STR_LIT>' in appenlight_info : <EOL> report_data [ '<STR_LIT>' ] = appenlight_info . pop ( '<STR_LIT>' , <EOL> None ) <EOL> report_data [ '<STR_LIT:message>' ] = message or appenlight_info . get ( '<STR_LIT:message>' , <EOL> u'<STR_LIT>' ) <EOL> exclude_keys = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> for k in exclude_keys : <EOL> report_data [ '<STR_LIT>' ] . pop ( k , None ) <EOL> report_data . update ( appenlight_info ) <EOL> del traceback <EOL> return report_data , appenlight_info <EOL> def get_current_traceback ( self ) : <EOL> tb = get_current_traceback ( skip = <NUM_LIT:1> , show_hidden_frames = True , <EOL> ignore_system_exceptions = True ) <EOL> return tb <EOL> class Client ( BaseClient ) : <EOL> pass <EOL> def get_config ( config = None , path_to_config = None , section_name = '<STR_LIT>' ) : <EOL> if not config and not path_to_config : <EOL> path_to_config = os . environ . get ( '<STR_LIT>' ) <EOL> if not path_to_config : <EOL> path_to_config = os . environ . get ( '<STR_LIT>' ) <EOL> if config is None : <EOL> config = { } <EOL> api_key = os . environ . get ( '<STR_LIT>' ) <EOL> if not api_key : <EOL> api_key = os . environ . get ( '<STR_LIT>' ) <EOL> if path_to_config : <EOL> file_config = { } <EOL> if not os . path . exists ( path_to_config ) : <EOL> logging . warning ( "<STR_LIT>" % path_to_config ) <EOL> return config <EOL> with open ( path_to_config ) as f : <EOL> parser = ConfigParser . SafeConfigParser ( ) <EOL> parser . readfp ( f ) <EOL> try : <EOL> file_config = dict ( parser . items ( section_name ) ) <EOL> except ConfigParser . NoSectionError : <EOL> logging . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' % section_name ) <EOL> config . update ( file_config ) <EOL> if not config . get ( '<STR_LIT>' ) and api_key : <EOL> config [ '<STR_LIT>' ] = api_key <EOL> if not config . get ( '<STR_LIT>' ) and api_key : <EOL> config [ '<STR_LIT>' ] = api_key <EOL> if not config . get ( '<STR_LIT>' ) : <EOL> logging . warning ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return config or { } <EOL> def decorate ( appenlight_config = None ) : <EOL> def app_decorator ( app ) : <EOL> @ wraps ( app ) <EOL> def app_wrapper ( * args , ** kwargs ) : <EOL> return make_appenlight_middleware ( app , appenlight_config ) <EOL> return app_wrapper ( ) <EOL> return app_decorator <EOL> def make_appenlight_middleware ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> app , client = make_appenlight_middleware_with_client ( * args , ** kwargs ) <EOL> return app <EOL> def make_appenlight_middleware_with_client ( app , global_config = None , ** kw ) : <EOL> client = kw . pop ( '<STR_LIT>' , None ) <EOL> if global_config : <EOL> config = global_config . copy ( ) <EOL> else : <EOL> config = { } <EOL> config . update ( kw ) <EOL> ini_path = os . environ . get ( '<STR_LIT>' , <EOL> config . get ( '<STR_LIT>' ) ) <EOL> if not ini_path : <EOL> ini_path = os . environ . get ( '<STR_LIT>' , <EOL> config . get ( '<STR_LIT>' ) ) <EOL> config = get_config ( config = config , path_to_config = ini_path ) <EOL> if not client : <EOL> client = Client ( config ) <EOL> from appenlight_client . wsgi import AppenlightWSGIWrapper <EOL> if client . config [ '<STR_LIT>' ] : <EOL> app = AppenlightWSGIWrapper ( app , client ) <EOL> return app , client </s>
