<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , unicode_literals <EOL> from copy import copy <EOL> from . connection import maybe_channel <EOL> from . exceptions import NotBoundError <EOL> from . five import python_2_unicode_compatible <EOL> from . utils import ChannelPromise <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def unpickle_dict ( cls , kwargs ) : <EOL> return cls ( ** kwargs ) <EOL> def _any ( v ) : <EOL> return v <EOL> class Object ( object ) : <EOL> """<STR_LIT>""" <EOL> attrs = ( ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> for name , type_ in self . attrs : <EOL> value = kwargs . get ( name ) <EOL> if value is not None : <EOL> setattr ( self , name , ( type_ or _any ) ( value ) ) <EOL> else : <EOL> try : <EOL> getattr ( self , name ) <EOL> except AttributeError : <EOL> setattr ( self , name , None ) <EOL> def as_dict ( self , recurse = False ) : <EOL> def f ( obj , type ) : <EOL> if recurse and isinstance ( obj , Object ) : <EOL> return obj . as_dict ( recurse = True ) <EOL> return type ( obj ) if type else obj <EOL> return { <EOL> attr : f ( getattr ( self , attr ) , type ) for attr , type in self . attrs <EOL> } <EOL> def __reduce__ ( self ) : <EOL> return unpickle_dict , ( self . __class__ , self . as_dict ( ) ) <EOL> def __copy__ ( self ) : <EOL> return self . __class__ ( ** self . as_dict ( ) ) <EOL> @ python_2_unicode_compatible <EOL> class MaybeChannelBound ( Object ) : <EOL> """<STR_LIT>""" <EOL> _channel = None <EOL> _is_bound = False <EOL> can_cache_declaration = False <EOL> def __call__ ( self , channel ) : <EOL> """<STR_LIT>""" <EOL> return self . bind ( channel ) <EOL> def bind ( self , channel ) : <EOL> """<STR_LIT>""" <EOL> return copy ( self ) . maybe_bind ( channel ) <EOL> def maybe_bind ( self , channel ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_bound and channel : <EOL> self . _channel = maybe_channel ( channel ) <EOL> self . when_bound ( ) <EOL> self . _is_bound = True <EOL> return self <EOL> def revive ( self , channel ) : <EOL> """<STR_LIT>""" <EOL> if self . is_bound : <EOL> self . _channel = channel <EOL> self . when_bound ( ) <EOL> def when_bound ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def __repr__ ( self ) : <EOL> return self . _repr_entity ( type ( self ) . __name__ ) <EOL> def _repr_entity ( self , item = '<STR_LIT>' ) : <EOL> item = item or type ( self ) . __name__ <EOL> if self . is_bound : <EOL> return '<STR_LIT>' . format ( <EOL> item or type ( self ) . __name__ , self . channel . channel_id ) <EOL> return '<STR_LIT>' . format ( item ) <EOL> @ property <EOL> def is_bound ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _is_bound and self . _channel is not None <EOL> @ property <EOL> def channel ( self ) : <EOL> """<STR_LIT>""" <EOL> channel = self . _channel <EOL> if channel is None : <EOL> raise NotBoundError ( <EOL> "<STR_LIT>" . format ( <EOL> type ( self ) . __name__ ) ) <EOL> if isinstance ( channel , ChannelPromise ) : <EOL> channel = self . _channel = channel ( ) <EOL> return channel </s>
