<s> import traceback <EOL> import bugsnag <EOL> class SimpleMiddleware ( object ) : <EOL> def __init__ ( self , before = None , after = None ) : <EOL> self . before = before <EOL> self . after = after <EOL> def __call__ ( self , bugsnag ) : <EOL> def middleware ( notification ) : <EOL> if self . before : <EOL> ret = self . before ( notification ) <EOL> if ret is False : <EOL> return <EOL> bugsnag ( notification ) <EOL> if self . after : <EOL> self . after ( notification ) <EOL> return middleware <EOL> class DefaultMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , bugsnag ) : <EOL> self . bugsnag = bugsnag <EOL> def __call__ ( self , notification ) : <EOL> config = notification . request_config <EOL> notification . set_user ( id = config . user_id ) <EOL> notification . set_user ( ** config . user ) <EOL> notification . grouping_hash = config . get ( "<STR_LIT>" ) <EOL> if not notification . context : <EOL> notification . context = config . get ( "<STR_LIT>" ) <EOL> for name , dictionary in config . meta_data . items ( ) : <EOL> notification . add_tab ( name , dictionary ) <EOL> notification . add_tab ( "<STR_LIT>" , config . get ( "<STR_LIT>" ) ) <EOL> notification . add_tab ( "<STR_LIT>" , config . get ( "<STR_LIT>" ) ) <EOL> notification . add_tab ( "<STR_LIT>" , config . get ( "<STR_LIT>" ) ) <EOL> notification . add_tab ( "<STR_LIT>" , config . get ( "<STR_LIT>" ) ) <EOL> self . bugsnag ( notification ) <EOL> class MiddlewareStack ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . stack = [ ] <EOL> def before_notify ( self , func ) : <EOL> """<STR_LIT>""" <EOL> self . append ( SimpleMiddleware ( before = func ) ) <EOL> def after_notify ( self , func ) : <EOL> """<STR_LIT>""" <EOL> self . append ( SimpleMiddleware ( after = func ) ) <EOL> def append ( self , middleware ) : <EOL> """<STR_LIT>""" <EOL> self . stack . append ( middleware ) <EOL> def run ( self , notification , callback ) : <EOL> """<STR_LIT>""" <EOL> def finish ( notification ) : <EOL> if not hasattr ( finish , '<STR_LIT>' ) : <EOL> finish . called = True <EOL> callback ( ) <EOL> to_call = finish <EOL> for middleware in reversed ( self . stack ) : <EOL> to_call = middleware ( to_call ) <EOL> try : <EOL> to_call ( notification ) <EOL> except Exception as exc : <EOL> template = "<STR_LIT>" <EOL> bugsnag . log ( template % ( exc , traceback . format_exc ( ) ) ) <EOL> finish ( notification ) </s>
