<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> try : <EOL> from flask_login import current_user <EOL> except ImportError : <EOL> has_flask_login = False <EOL> else : <EOL> has_flask_login = True <EOL> import sys <EOL> import os <EOL> import logging <EOL> from flask import request , current_app , g <EOL> from flask . signals import got_request_exception , request_finished <EOL> from werkzeug . exceptions import ClientDisconnected <EOL> from raven . _compat import string_types <EOL> from raven . conf import setup_logging <EOL> from raven . base import Client <EOL> from raven . middleware import Sentry as SentryMiddleware <EOL> from raven . handlers . logging import SentryHandler <EOL> from raven . utils . compat import _urlparse <EOL> from raven . utils . encoding import to_unicode <EOL> from raven . utils . imports import import_string <EOL> from raven . utils . wsgi import get_headers , get_environ <EOL> def make_client ( client_cls , app , dsn = None ) : <EOL> transport = app . config . get ( '<STR_LIT>' ) <EOL> if isinstance ( transport , string_types ) : <EOL> transport = import_string ( transport ) <EOL> return client_cls ( <EOL> dsn = dsn or app . config . get ( '<STR_LIT>' ) or os . environ . get ( '<STR_LIT>' ) , <EOL> transport = transport , <EOL> include_paths = set ( app . config . get ( <EOL> '<STR_LIT>' , [ ] ) ) | set ( [ app . import_name ] ) , <EOL> exclude_paths = app . config . get ( '<STR_LIT>' ) , <EOL> name = app . config . get ( '<STR_LIT>' ) , <EOL> site = app . config . get ( '<STR_LIT>' ) , <EOL> processors = app . config . get ( '<STR_LIT>' ) , <EOL> string_max_length = app . config . get ( '<STR_LIT>' ) , <EOL> list_max_length = app . config . get ( '<STR_LIT>' ) , <EOL> auto_log_stacks = app . config . get ( '<STR_LIT>' ) , <EOL> tags = app . config . get ( '<STR_LIT>' ) , <EOL> release = app . config . get ( '<STR_LIT>' ) , <EOL> extra = { <EOL> '<STR_LIT>' : app , <EOL> } , <EOL> ) <EOL> class Sentry ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app = None , client = None , client_cls = Client , dsn = None , <EOL> logging = False , logging_exclusions = None , level = logging . NOTSET , <EOL> wrap_wsgi = None , register_signal = True ) : <EOL> self . dsn = dsn <EOL> self . logging = logging <EOL> self . logging_exclusions = logging_exclusions <EOL> self . client_cls = client_cls <EOL> self . client = client <EOL> self . level = level <EOL> self . wrap_wsgi = wrap_wsgi <EOL> self . register_signal = register_signal <EOL> if app : <EOL> self . init_app ( app ) <EOL> @ property <EOL> def last_event_id ( self ) : <EOL> return getattr ( self , '<STR_LIT>' , None ) <EOL> @ last_event_id . setter <EOL> def last_event_id ( self , value ) : <EOL> self . _last_event_id = value <EOL> try : <EOL> g . sentry_event_id = value <EOL> except Exception : <EOL> pass <EOL> def handle_exception ( self , * args , ** kwargs ) : <EOL> if not self . client : <EOL> return <EOL> ignored_exc_type_list = current_app . config . get ( <EOL> '<STR_LIT>' , [ ] ) <EOL> exc = sys . exc_info ( ) [ <NUM_LIT:1> ] <EOL> if any ( ( isinstance ( exc , ignored_exc_type ) <EOL> for ignored_exc_type in ignored_exc_type_list ) ) : <EOL> return <EOL> self . captureException ( exc_info = kwargs . get ( '<STR_LIT>' ) ) <EOL> def get_user_info ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if not has_flask_login : <EOL> return <EOL> if not hasattr ( current_app , '<STR_LIT>' ) : <EOL> return <EOL> try : <EOL> is_authenticated = current_user . is_authenticated <EOL> except AttributeError : <EOL> return { } <EOL> if callable ( is_authenticated ) : <EOL> is_authenticated = is_authenticated ( ) <EOL> if not is_authenticated : <EOL> return { } <EOL> user_info = { <EOL> '<STR_LIT:id>' : current_user . get_id ( ) , <EOL> } <EOL> if '<STR_LIT>' in current_app . config : <EOL> for attr in current_app . config [ '<STR_LIT>' ] : <EOL> if hasattr ( current_user , attr ) : <EOL> user_info [ attr ] = getattr ( current_user , attr ) <EOL> return user_info <EOL> def get_http_info ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if self . is_json_type ( request . mimetype ) : <EOL> retriever = self . get_json_data <EOL> else : <EOL> retriever = self . get_form_data <EOL> return self . get_http_info_with_retriever ( request , retriever ) <EOL> def is_json_type ( self , content_type ) : <EOL> return content_type == '<STR_LIT:application/json>' <EOL> def get_form_data ( self , request ) : <EOL> return request . form <EOL> def get_json_data ( self , request ) : <EOL> return request . data <EOL> def get_http_info_with_retriever ( self , request , retriever = None ) : <EOL> """<STR_LIT>""" <EOL> if retriever is None : <EOL> retriever = self . get_form_data <EOL> urlparts = _urlparse . urlsplit ( request . url ) <EOL> try : <EOL> data = retriever ( request ) <EOL> except ClientDisconnected : <EOL> data = { } <EOL> return { <EOL> '<STR_LIT:url>' : '<STR_LIT>' % ( urlparts . scheme , urlparts . netloc , urlparts . path ) , <EOL> '<STR_LIT>' : urlparts . query , <EOL> '<STR_LIT>' : request . method , <EOL> '<STR_LIT:data>' : data , <EOL> '<STR_LIT>' : dict ( get_headers ( request . environ ) ) , <EOL> '<STR_LIT>' : dict ( get_environ ( request . environ ) ) , <EOL> } <EOL> def before_request ( self , * args , ** kwargs ) : <EOL> self . last_event_id = None <EOL> try : <EOL> self . client . http_context ( self . get_http_info ( request ) ) <EOL> except Exception as e : <EOL> self . client . logger . exception ( to_unicode ( e ) ) <EOL> try : <EOL> self . client . user_context ( self . get_user_info ( request ) ) <EOL> except Exception as e : <EOL> self . client . logger . exception ( to_unicode ( e ) ) <EOL> def after_request ( self , sender , response , * args , ** kwargs ) : <EOL> if self . last_event_id : <EOL> response . headers [ '<STR_LIT>' ] = self . last_event_id <EOL> self . client . context . clear ( ) <EOL> return response <EOL> def init_app ( self , app , dsn = None , logging = None , level = None , <EOL> logging_exclusions = None , wrap_wsgi = None , <EOL> register_signal = None ) : <EOL> if dsn is not None : <EOL> self . dsn = dsn <EOL> if level is not None : <EOL> self . level = level <EOL> if wrap_wsgi is not None : <EOL> self . wrap_wsgi = wrap_wsgi <EOL> elif self . wrap_wsgi is None : <EOL> if app and app . debug : <EOL> self . wrap_wsgi = False <EOL> else : <EOL> self . wrap_wsgi = True <EOL> if register_signal is not None : <EOL> self . register_signal = register_signal <EOL> if logging is not None : <EOL> self . logging = logging <EOL> if logging_exclusions is not None : <EOL> self . logging_exclusions = logging_exclusions <EOL> if not self . client : <EOL> self . client = make_client ( self . client_cls , app , self . dsn ) <EOL> if self . logging : <EOL> kwargs = { } <EOL> if self . logging_exclusions is not None : <EOL> kwargs [ '<STR_LIT>' ] = self . logging_exclusions <EOL> setup_logging ( SentryHandler ( self . client , level = self . level ) , ** kwargs ) <EOL> if self . wrap_wsgi : <EOL> app . wsgi_app = SentryMiddleware ( app . wsgi_app , self . client ) <EOL> app . before_request ( self . before_request ) <EOL> if self . register_signal : <EOL> got_request_exception . connect ( self . handle_exception , sender = app ) <EOL> request_finished . connect ( self . after_request , sender = app ) <EOL> if not hasattr ( app , '<STR_LIT>' ) : <EOL> app . extensions = { } <EOL> app . extensions [ '<STR_LIT>' ] = self <EOL> def captureException ( self , * args , ** kwargs ) : <EOL> assert self . client , '<STR_LIT>' <EOL> result = self . client . captureException ( * args , ** kwargs ) <EOL> if result : <EOL> self . last_event_id = self . client . get_ident ( result ) <EOL> else : <EOL> self . last_event_id = None <EOL> return result <EOL> def captureMessage ( self , * args , ** kwargs ) : <EOL> assert self . client , '<STR_LIT>' <EOL> result = self . client . captureMessage ( * args , ** kwargs ) <EOL> if result : <EOL> self . last_event_id = self . client . get_ident ( result ) <EOL> else : <EOL> self . last_event_id = None <EOL> return result <EOL> def user_context ( self , * args , ** kwargs ) : <EOL> assert self . client , '<STR_LIT>' <EOL> return self . client . user_context ( * args , ** kwargs ) <EOL> def tags_context ( self , * args , ** kwargs ) : <EOL> assert self . client , '<STR_LIT>' <EOL> return self . client . tags_context ( * args , ** kwargs ) <EOL> def extra_context ( self , * args , ** kwargs ) : <EOL> assert self . client , '<STR_LIT>' <EOL> return self . client . extra_context ( * args , ** kwargs ) </s>
