<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import logging <EOL> from contextlib import closing <EOL> from raven . _compat import text_type <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class SerializationManager ( object ) : <EOL> logger = logger <EOL> def __init__ ( self ) : <EOL> self . __registry = [ ] <EOL> self . __serializers = { } <EOL> @ property <EOL> def serializers ( self ) : <EOL> for serializer in self . __registry : <EOL> yield serializer <EOL> def register ( self , serializer ) : <EOL> if serializer not in self . __registry : <EOL> self . __registry . append ( serializer ) <EOL> return serializer <EOL> class Serializer ( object ) : <EOL> logger = logger <EOL> def __init__ ( self , manager ) : <EOL> self . manager = manager <EOL> self . context = set ( ) <EOL> self . serializers = [ ] <EOL> for serializer in manager . serializers : <EOL> self . serializers . append ( serializer ( self ) ) <EOL> def close ( self ) : <EOL> del self . serializers <EOL> del self . context <EOL> def transform ( self , value , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return None <EOL> objid = id ( value ) <EOL> if objid in self . context : <EOL> return '<STR_LIT>' <EOL> self . context . add ( objid ) <EOL> try : <EOL> for serializer in self . serializers : <EOL> if serializer . can ( value ) : <EOL> try : <EOL> return serializer . serialize ( value , ** kwargs ) <EOL> except Exception as e : <EOL> logger . exception ( e ) <EOL> return text_type ( type ( value ) ) <EOL> try : <EOL> return repr ( value ) <EOL> except Exception as e : <EOL> logger . exception ( e ) <EOL> return text_type ( type ( value ) ) <EOL> finally : <EOL> self . context . remove ( objid ) <EOL> manager = SerializationManager ( ) <EOL> register = manager . register <EOL> def transform ( value , manager = manager , ** kwargs ) : <EOL> with closing ( Serializer ( manager ) ) as serializer : <EOL> return serializer . transform ( value , ** kwargs ) </s>
