<s> import logging <EOL> import traceback <EOL> import requests <EOL> import os <EOL> import sys <EOL> from six import text_type <EOL> from xml . etree . ElementTree import Element , tostring , SubElement <EOL> from . import __app_name__ , __version__ , __app_url__ <EOL> _API_URL = '<STR_LIT>' <EOL> _DEFAULT_ENV_VARIABLES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _DEFAULT_META_VARIABLES = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> class AirbrakeErrorHandler ( logging . Handler ) : <EOL> def __init__ ( self , api_key , env_name , request , api_url = _API_URL , <EOL> timeout = <NUM_LIT:30> , env_variables = _DEFAULT_ENV_VARIABLES , <EOL> meta_variables = _DEFAULT_META_VARIABLES , session = None , <EOL> root_path = None ) : <EOL> logging . Handler . __init__ ( self ) <EOL> self . api_key = api_key <EOL> self . api_url = api_url <EOL> self . env_name = env_name <EOL> self . env_variables = env_variables <EOL> self . meta_variables = meta_variables <EOL> self . timeout = timeout <EOL> self . session = session <EOL> self . root_path = root_path <EOL> if self . root_path is None : <EOL> self . root_path = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> self . request = { <EOL> '<STR_LIT:url>' : request . url , <EOL> '<STR_LIT:path>' : request . path , <EOL> '<STR_LIT>' : request . method , <EOL> '<STR_LIT>' : request . values , <EOL> '<STR_LIT:body>' : request . data , <EOL> '<STR_LIT>' : request . headers , <EOL> '<STR_LIT>' : request . remote_addr <EOL> } <EOL> try : <EOL> self . request [ '<STR_LIT>' ] = request . json <EOL> except AttributeError : <EOL> pass <EOL> def emit ( self , exception , exc_info = None ) : <EOL> headers = { "<STR_LIT:Content-Type>" : "<STR_LIT>" } <EOL> data = self . _generate_xml ( exception = exception , exc_info = exc_info ) <EOL> status = requests . post ( url = self . api_url , headers = headers , data = data ) <EOL> return ( status . status_code , status . text ) <EOL> def _generate_xml ( self , exception , exc_info = None ) : <EOL> _ , _ , trace = exc_info or sys . exc_info ( ) <EOL> xml = Element ( '<STR_LIT>' , dict ( version = '<STR_LIT>' ) ) <EOL> SubElement ( xml , '<STR_LIT>' ) . text = self . api_key <EOL> notifier = SubElement ( xml , '<STR_LIT>' ) <EOL> SubElement ( notifier , '<STR_LIT:name>' ) . text = __app_name__ <EOL> SubElement ( notifier , '<STR_LIT:version>' ) . text = __version__ <EOL> SubElement ( notifier , '<STR_LIT:url>' ) . text = __app_url__ <EOL> server_env = SubElement ( xml , '<STR_LIT>' ) <EOL> SubElement ( server_env , '<STR_LIT>' ) . text = self . env_name <EOL> SubElement ( server_env , '<STR_LIT>' ) . text = self . root_path <EOL> request_xml = SubElement ( xml , '<STR_LIT>' ) <EOL> SubElement ( request_xml , '<STR_LIT:url>' ) . text = self . request [ '<STR_LIT:url>' ] <EOL> if self . request [ '<STR_LIT:path>' ] : <EOL> SubElement ( request_xml , '<STR_LIT>' ) . text = self . request [ '<STR_LIT:path>' ] <EOL> SubElement ( request_xml , '<STR_LIT:action>' ) . text = self . request [ '<STR_LIT>' ] <EOL> if self . session : <EOL> request_session = SubElement ( request_xml , '<STR_LIT>' ) <EOL> for key in self . session : <EOL> SubElement ( request_session , '<STR_LIT>' , dict ( key = key ) ) . text = ( text_type ( <EOL> self . session [ key ] ) ) <EOL> params = SubElement ( request_xml , '<STR_LIT>' ) <EOL> if self . request [ '<STR_LIT>' ] : <EOL> for key in self . request [ '<STR_LIT>' ] : <EOL> SubElement ( params , '<STR_LIT>' , dict ( key = key ) ) . text = ( text_type ( <EOL> self . request [ '<STR_LIT>' ] . get ( key ) ) ) <EOL> if self . request [ '<STR_LIT:body>' ] : <EOL> SubElement ( params , '<STR_LIT>' , dict ( key = '<STR_LIT:body>' ) ) . text = ( text_type ( <EOL> self . request [ '<STR_LIT:body>' ] ) ) <EOL> if self . request . get ( '<STR_LIT>' ) : <EOL> for key in self . request [ '<STR_LIT>' ] : <EOL> SubElement ( params , '<STR_LIT>' , dict ( key = key ) ) . text = ( text_type ( <EOL> self . request [ '<STR_LIT>' ] . get ( key ) ) ) <EOL> cgi_data = SubElement ( request_xml , '<STR_LIT>' ) <EOL> for key , value in os . environ . items ( ) : <EOL> if key in self . env_variables : <EOL> SubElement ( cgi_data , '<STR_LIT>' , dict ( key = key ) ) . text = text_type ( value ) <EOL> if self . request [ '<STR_LIT>' ] : <EOL> SubElement ( cgi_data , '<STR_LIT>' , dict ( key = '<STR_LIT>' ) ) . text = text_type ( <EOL> self . request [ '<STR_LIT>' ] ) <EOL> if self . request [ '<STR_LIT>' ] : <EOL> for key , value in self . request [ '<STR_LIT>' ] : <EOL> SubElement ( cgi_data , '<STR_LIT>' , dict ( key = key ) ) . text = text_type ( value ) <EOL> error = SubElement ( xml , '<STR_LIT:error>' ) <EOL> SubElement ( error , '<STR_LIT:class>' ) . text = exception . __class__ . __name__ <EOL> SubElement ( error , '<STR_LIT:message>' ) . text = text_type ( exception ) <EOL> backtrace = SubElement ( error , '<STR_LIT>' ) <EOL> if trace is not None : <EOL> for ( pathname , lineno , func_name , <EOL> text ) in traceback . extract_tb ( trace ) [ : : - <NUM_LIT:1> ] : <EOL> SubElement ( backtrace , '<STR_LIT>' , dict ( file = pathname , <EOL> number = text_type ( lineno ) , <EOL> method = '<STR_LIT>' % ( func_name , <EOL> text ) ) ) <EOL> return tostring ( xml ) </s>
