<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , unicode_literals <EOL> import numbers <EOL> from . abstract import MaybeChannelBound , Object <EOL> from . exceptions import ContentDisallowed <EOL> from . five import python_2_unicode_compatible , string_t <EOL> from . serialization import prepare_accept_content <EOL> TRANSIENT_DELIVERY_MODE = <NUM_LIT:1> <EOL> PERSISTENT_DELIVERY_MODE = <NUM_LIT:2> <EOL> DELIVERY_MODES = { '<STR_LIT>' : TRANSIENT_DELIVERY_MODE , <EOL> '<STR_LIT>' : PERSISTENT_DELIVERY_MODE } <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> INTERNAL_EXCHANGE_PREFIX = ( '<STR_LIT>' , ) <EOL> def _reprstr ( s ) : <EOL> s = repr ( s ) <EOL> if isinstance ( s , string_t ) and s . startswith ( "<STR_LIT>" ) : <EOL> return s [ <NUM_LIT:2> : - <NUM_LIT:1> ] <EOL> return s [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> def pretty_bindings ( bindings ) : <EOL> return '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( map ( str , bindings ) ) ) <EOL> def maybe_delivery_mode ( <EOL> v , modes = DELIVERY_MODES , default = PERSISTENT_DELIVERY_MODE ) : <EOL> if v : <EOL> return v if isinstance ( v , numbers . Integral ) else modes [ v ] <EOL> return default <EOL> @ python_2_unicode_compatible <EOL> class Exchange ( MaybeChannelBound ) : <EOL> """<STR_LIT>""" <EOL> TRANSIENT_DELIVERY_MODE = TRANSIENT_DELIVERY_MODE <EOL> PERSISTENT_DELIVERY_MODE = PERSISTENT_DELIVERY_MODE <EOL> name = '<STR_LIT>' <EOL> type = '<STR_LIT>' <EOL> durable = True <EOL> auto_delete = False <EOL> passive = False <EOL> delivery_mode = None <EOL> no_declare = False <EOL> attrs = ( <EOL> ( '<STR_LIT:name>' , None ) , <EOL> ( '<STR_LIT:type>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , bool ) , <EOL> ( '<STR_LIT>' , bool ) , <EOL> ( '<STR_LIT>' , bool ) , <EOL> ( '<STR_LIT>' , lambda m : DELIVERY_MODES . get ( m ) or m ) , <EOL> ( '<STR_LIT>' , bool ) , <EOL> ) <EOL> def __init__ ( self , name = '<STR_LIT>' , type = '<STR_LIT>' , channel = None , ** kwargs ) : <EOL> super ( Exchange , self ) . __init__ ( ** kwargs ) <EOL> self . name = name or self . name <EOL> self . type = type or self . type <EOL> self . maybe_bind ( channel ) <EOL> def __hash__ ( self ) : <EOL> return hash ( '<STR_LIT>' % ( self . name , ) ) <EOL> def _can_declare ( self ) : <EOL> return not self . no_declare and ( <EOL> self . name and not self . name . startswith ( <EOL> INTERNAL_EXCHANGE_PREFIX ) ) <EOL> def declare ( self , nowait = False , passive = None ) : <EOL> """<STR_LIT>""" <EOL> if self . _can_declare ( ) : <EOL> passive = self . passive if passive is None else passive <EOL> return self . channel . exchange_declare ( <EOL> exchange = self . name , type = self . type , durable = self . durable , <EOL> auto_delete = self . auto_delete , arguments = self . arguments , <EOL> nowait = nowait , passive = passive , <EOL> ) <EOL> def bind_to ( self , exchange = '<STR_LIT>' , routing_key = '<STR_LIT>' , <EOL> arguments = None , nowait = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( exchange , Exchange ) : <EOL> exchange = exchange . name <EOL> return self . channel . exchange_bind ( destination = self . name , <EOL> source = exchange , <EOL> routing_key = routing_key , <EOL> nowait = nowait , <EOL> arguments = arguments ) <EOL> def unbind_from ( self , source = '<STR_LIT>' , routing_key = '<STR_LIT>' , <EOL> nowait = False , arguments = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( source , Exchange ) : <EOL> source = source . name <EOL> return self . channel . exchange_unbind ( destination = self . name , <EOL> source = source , <EOL> routing_key = routing_key , <EOL> nowait = nowait , <EOL> arguments = arguments ) <EOL> def Message ( self , body , delivery_mode = None , priority = None , <EOL> content_type = None , content_encoding = None , <EOL> properties = None , headers = None ) : <EOL> """<STR_LIT>""" <EOL> properties = { } if properties is None else properties <EOL> properties [ '<STR_LIT>' ] = maybe_delivery_mode ( self . delivery_mode ) <EOL> return self . channel . prepare_message ( body , <EOL> properties = properties , <EOL> priority = priority , <EOL> content_type = content_type , <EOL> content_encoding = content_encoding , <EOL> headers = headers ) <EOL> def publish ( self , message , routing_key = None , mandatory = False , <EOL> immediate = False , exchange = None ) : <EOL> """<STR_LIT>""" <EOL> exchange = exchange or self . name <EOL> return self . channel . basic_publish ( message , <EOL> exchange = exchange , <EOL> routing_key = routing_key , <EOL> mandatory = mandatory , <EOL> immediate = immediate ) <EOL> def delete ( self , if_unused = False , nowait = False ) : <EOL> """<STR_LIT>""" <EOL> return self . channel . exchange_delete ( exchange = self . name , <EOL> if_unused = if_unused , <EOL> nowait = nowait ) <EOL> def binding ( self , routing_key = '<STR_LIT>' , arguments = None , unbind_arguments = None ) : <EOL> return binding ( self , routing_key , arguments , unbind_arguments ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , Exchange ) : <EOL> return ( self . name == other . name and <EOL> self . type == other . type and <EOL> self . arguments == other . arguments and <EOL> self . durable == other . durable and <EOL> self . auto_delete == other . auto_delete and <EOL> self . delivery_mode == other . delivery_mode ) <EOL> return NotImplemented <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __repr__ ( self ) : <EOL> return self . _repr_entity ( self ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' . format ( <EOL> _reprstr ( self . name ) or repr ( '<STR_LIT>' ) , self . type , <EOL> ) <EOL> @ property <EOL> def can_cache_declaration ( self ) : <EOL> return not self . auto_delete <EOL> @ python_2_unicode_compatible <EOL> class binding ( Object ) : <EOL> """<STR_LIT>""" <EOL> attrs = ( <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) <EOL> ) <EOL> def __init__ ( self , exchange = None , routing_key = '<STR_LIT>' , <EOL> arguments = None , unbind_arguments = None ) : <EOL> self . exchange = exchange <EOL> self . routing_key = routing_key <EOL> self . arguments = arguments <EOL> self . unbind_arguments = unbind_arguments <EOL> def declare ( self , channel , nowait = False ) : <EOL> """<STR_LIT>""" <EOL> if self . exchange and self . exchange . name : <EOL> ex = self . exchange ( channel ) <EOL> ex . declare ( nowait = nowait ) <EOL> def bind ( self , entity , nowait = False ) : <EOL> """<STR_LIT>""" <EOL> entity . bind_to ( exchange = self . exchange , <EOL> routing_key = self . routing_key , <EOL> arguments = self . arguments , <EOL> nowait = nowait ) <EOL> def unbind ( self , entity , nowait = False ) : <EOL> """<STR_LIT>""" <EOL> entity . unbind_from ( self . exchange , <EOL> routing_key = self . routing_key , <EOL> arguments = self . unbind_arguments , <EOL> nowait = nowait ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' . format ( self ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' . format ( <EOL> _reprstr ( self . exchange . name ) , _reprstr ( self . routing_key ) , <EOL> ) <EOL> @ python_2_unicode_compatible <EOL> class Queue ( MaybeChannelBound ) : <EOL> """<STR_LIT>""" <EOL> ContentDisallowed = ContentDisallowed <EOL> name = '<STR_LIT>' <EOL> exchange = Exchange ( '<STR_LIT>' ) <EOL> routing_key = '<STR_LIT>' <EOL> durable = True <EOL> exclusive = False <EOL> auto_delete = False <EOL> no_ack = False <EOL> attrs = ( <EOL> ( '<STR_LIT:name>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , bool ) , <EOL> ( '<STR_LIT>' , bool ) , <EOL> ( '<STR_LIT>' , bool ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , list ) , <EOL> ( '<STR_LIT>' , bool ) , <EOL> ) <EOL> def __init__ ( self , name = '<STR_LIT>' , exchange = None , routing_key = '<STR_LIT>' , <EOL> channel = None , bindings = None , on_declared = None , <EOL> ** kwargs ) : <EOL> super ( Queue , self ) . __init__ ( ** kwargs ) <EOL> self . name = name or self . name <EOL> self . exchange = exchange or self . exchange <EOL> self . routing_key = routing_key or self . routing_key <EOL> self . bindings = set ( bindings or [ ] ) <EOL> self . on_declared = on_declared <EOL> if isinstance ( exchange , ( list , tuple , set ) ) : <EOL> self . bindings |= set ( exchange ) <EOL> if self . bindings : <EOL> self . exchange = None <EOL> if self . exclusive : <EOL> self . auto_delete = True <EOL> self . maybe_bind ( channel ) <EOL> def bind ( self , channel ) : <EOL> on_declared = self . on_declared <EOL> bound = super ( Queue , self ) . bind ( channel ) <EOL> bound . on_declared = on_declared <EOL> return bound <EOL> def __hash__ ( self ) : <EOL> return hash ( '<STR_LIT>' % ( self . name , ) ) <EOL> def when_bound ( self ) : <EOL> if self . exchange : <EOL> self . exchange = self . exchange ( self . channel ) <EOL> def declare ( self , nowait = False ) : <EOL> """<STR_LIT>""" <EOL> if not self . no_declare : <EOL> self . _create_exchange ( nowait = nowait ) <EOL> self . _create_queue ( nowait = nowait ) <EOL> self . _create_bindings ( nowait = nowait ) <EOL> return self . name <EOL> def _create_exchange ( self , nowait = False ) : <EOL> if self . exchange : <EOL> self . exchange . declare ( nowait ) <EOL> def _create_queue ( self , nowait = False ) : <EOL> self . queue_declare ( nowait , passive = False ) <EOL> if self . exchange and self . exchange . name : <EOL> self . queue_bind ( nowait ) <EOL> def _create_bindings ( self , nowait = False ) : <EOL> for B in self . bindings : <EOL> B . declare ( self . channel ) <EOL> B . bind ( self , nowait = nowait ) <EOL> def queue_declare ( self , nowait = False , passive = False ) : <EOL> """<STR_LIT>""" <EOL> ret = self . channel . queue_declare ( queue = self . name , <EOL> passive = passive , <EOL> durable = self . durable , <EOL> exclusive = self . exclusive , <EOL> auto_delete = self . auto_delete , <EOL> arguments = self . queue_arguments , <EOL> nowait = nowait ) <EOL> if not self . name : <EOL> self . name = ret [ <NUM_LIT:0> ] <EOL> if self . on_declared : <EOL> self . on_declared ( * ret ) <EOL> return ret <EOL> def queue_bind ( self , nowait = False ) : <EOL> """<STR_LIT>""" <EOL> return self . bind_to ( self . exchange , self . routing_key , <EOL> self . binding_arguments , nowait = nowait ) <EOL> def bind_to ( self , exchange = '<STR_LIT>' , routing_key = '<STR_LIT>' , <EOL> arguments = None , nowait = False ) : <EOL> if isinstance ( exchange , Exchange ) : <EOL> exchange = exchange . name <EOL> return self . channel . queue_bind ( queue = self . name , <EOL> exchange = exchange , <EOL> routing_key = routing_key , <EOL> arguments = arguments , <EOL> nowait = nowait ) <EOL> def get ( self , no_ack = None , accept = None ) : <EOL> """<STR_LIT>""" <EOL> no_ack = self . no_ack if no_ack is None else no_ack <EOL> message = self . channel . basic_get ( queue = self . name , no_ack = no_ack ) <EOL> if message is not None : <EOL> m2p = getattr ( self . channel , '<STR_LIT>' , None ) <EOL> if m2p : <EOL> message = m2p ( message ) <EOL> if message . errors : <EOL> message . _reraise_error ( ) <EOL> message . accept = prepare_accept_content ( accept ) <EOL> return message <EOL> def purge ( self , nowait = False ) : <EOL> """<STR_LIT>""" <EOL> return self . channel . queue_purge ( queue = self . name , <EOL> nowait = nowait ) or <NUM_LIT:0> <EOL> def consume ( self , consumer_tag = '<STR_LIT>' , callback = None , <EOL> no_ack = None , nowait = False ) : <EOL> """<STR_LIT>""" <EOL> if no_ack is None : <EOL> no_ack = self . no_ack <EOL> return self . channel . basic_consume ( queue = self . name , <EOL> no_ack = no_ack , <EOL> consumer_tag = consumer_tag or '<STR_LIT>' , <EOL> callback = callback , <EOL> nowait = nowait , <EOL> arguments = self . consumer_arguments ) <EOL> def cancel ( self , consumer_tag ) : <EOL> """<STR_LIT>""" <EOL> return self . channel . basic_cancel ( consumer_tag ) <EOL> def delete ( self , if_unused = False , if_empty = False , nowait = False ) : <EOL> """<STR_LIT>""" <EOL> return self . channel . queue_delete ( queue = self . name , <EOL> if_unused = if_unused , <EOL> if_empty = if_empty , <EOL> nowait = nowait ) <EOL> def queue_unbind ( self , arguments = None , nowait = False ) : <EOL> return self . unbind_from ( self . exchange , self . routing_key , <EOL> arguments , nowait ) <EOL> def unbind_from ( self , exchange = '<STR_LIT>' , routing_key = '<STR_LIT>' , <EOL> arguments = None , nowait = False ) : <EOL> """<STR_LIT>""" <EOL> return self . channel . queue_unbind ( queue = self . name , <EOL> exchange = exchange . name , <EOL> routing_key = routing_key , <EOL> arguments = arguments , <EOL> nowait = nowait ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , Queue ) : <EOL> return ( self . name == other . name and <EOL> self . exchange == other . exchange and <EOL> self . routing_key == other . routing_key and <EOL> self . queue_arguments == other . queue_arguments and <EOL> self . binding_arguments == other . binding_arguments and <EOL> self . consumer_arguments == other . consumer_arguments and <EOL> self . durable == other . durable and <EOL> self . exclusive == other . exclusive and <EOL> self . auto_delete == other . auto_delete ) <EOL> return NotImplemented <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __repr__ ( self ) : <EOL> if self . bindings : <EOL> return self . _repr_entity ( '<STR_LIT>' . format ( <EOL> name = _reprstr ( self . name ) , <EOL> bindings = pretty_bindings ( self . bindings ) , <EOL> ) ) <EOL> return self . _repr_entity ( <EOL> '<STR_LIT>' . format ( <EOL> self , name = _reprstr ( self . name ) , <EOL> routing_key = _reprstr ( self . routing_key ) , <EOL> ) , <EOL> ) <EOL> @ property <EOL> def can_cache_declaration ( self ) : <EOL> return not self . auto_delete <EOL> @ classmethod <EOL> def from_dict ( self , queue , ** options ) : <EOL> binding_key = options . get ( '<STR_LIT>' ) or options . get ( '<STR_LIT>' ) <EOL> e_durable = options . get ( '<STR_LIT>' ) <EOL> if e_durable is None : <EOL> e_durable = options . get ( '<STR_LIT>' ) <EOL> e_auto_delete = options . get ( '<STR_LIT>' ) <EOL> if e_auto_delete is None : <EOL> e_auto_delete = options . get ( '<STR_LIT>' ) <EOL> q_durable = options . get ( '<STR_LIT>' ) <EOL> if q_durable is None : <EOL> q_durable = options . get ( '<STR_LIT>' ) <EOL> q_auto_delete = options . get ( '<STR_LIT>' ) <EOL> if q_auto_delete is None : <EOL> q_auto_delete = options . get ( '<STR_LIT>' ) <EOL> e_arguments = options . get ( '<STR_LIT>' ) <EOL> q_arguments = options . get ( '<STR_LIT>' ) <EOL> b_arguments = options . get ( '<STR_LIT>' ) <EOL> c_arguments = options . get ( '<STR_LIT>' ) <EOL> bindings = options . get ( '<STR_LIT>' ) <EOL> exchange = Exchange ( options . get ( '<STR_LIT>' ) , <EOL> type = options . get ( '<STR_LIT>' ) , <EOL> delivery_mode = options . get ( '<STR_LIT>' ) , <EOL> routing_key = options . get ( '<STR_LIT>' ) , <EOL> durable = e_durable , <EOL> auto_delete = e_auto_delete , <EOL> arguments = e_arguments ) <EOL> return Queue ( queue , <EOL> exchange = exchange , <EOL> routing_key = binding_key , <EOL> durable = q_durable , <EOL> exclusive = options . get ( '<STR_LIT>' ) , <EOL> auto_delete = q_auto_delete , <EOL> no_ack = options . get ( '<STR_LIT>' ) , <EOL> queue_arguments = q_arguments , <EOL> binding_arguments = b_arguments , <EOL> consumer_arguments = c_arguments , <EOL> bindings = bindings ) <EOL> def as_dict ( self , recurse = False ) : <EOL> res = super ( Queue , self ) . as_dict ( recurse ) <EOL> if not recurse : <EOL> return res <EOL> bindings = res . get ( '<STR_LIT>' ) <EOL> if bindings : <EOL> res [ '<STR_LIT>' ] = [ b . as_dict ( recurse = True ) for b in bindings ] <EOL> return res </s>
