<s> from __future__ import absolute_import , unicode_literals <EOL> import os <EOL> try : <EOL> from qpid . selector import Selector <EOL> except ImportError : <EOL> Selector = None <EOL> import atexit <EOL> def default_monkey ( ) : <EOL> Selector . lock . acquire ( ) <EOL> try : <EOL> if Selector . DEFAULT is None : <EOL> sel = Selector ( ) <EOL> atexit . register ( sel . stop ) <EOL> sel . start ( ) <EOL> Selector . DEFAULT = sel <EOL> Selector . _current_pid = os . getpid ( ) <EOL> elif Selector . _current_pid != os . getpid ( ) : <EOL> sel = Selector ( ) <EOL> atexit . register ( sel . stop ) <EOL> sel . start ( ) <EOL> Selector . DEFAULT = sel <EOL> Selector . _current_pid = os . getpid ( ) <EOL> return Selector . DEFAULT <EOL> finally : <EOL> Selector . lock . release ( ) <EOL> try : <EOL> import qpid . selector <EOL> qpid . selector . Selector . default = staticmethod ( default_monkey ) <EOL> except ImportError : <EOL> pass <EOL> try : <EOL> from qpid . ops import ExchangeQuery , QueueQuery <EOL> except ImportError : <EOL> ExchangeQuery = None <EOL> QueueQuery = None <EOL> try : <EOL> from qpid . messaging . exceptions import ( <EOL> NotFound , AssertionFailed , ConnectionError , <EOL> ) <EOL> except ImportError : <EOL> NotFound = None <EOL> AssertionFailed = None <EOL> ConnectionError = None <EOL> def resolve_declare_monkey ( self , sst , lnk , dir , action ) : <EOL> declare = lnk . options . get ( '<STR_LIT>' ) in ( '<STR_LIT>' , dir ) <EOL> assrt = lnk . options . get ( '<STR_LIT>' ) in ( '<STR_LIT>' , dir ) <EOL> requested_type = lnk . options . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:type>' ) <EOL> def do_resolved ( type , subtype ) : <EOL> err = None <EOL> if type is None : <EOL> if declare : <EOL> err = self . declare ( sst , lnk , action ) <EOL> else : <EOL> err = NotFound ( text = '<STR_LIT>' % lnk . name ) <EOL> else : <EOL> if assrt : <EOL> expected = lnk . options . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT:type>' ) <EOL> if expected and type != expected : <EOL> err = AssertionFailed ( <EOL> text = '<STR_LIT>' % ( expected , type ) ) <EOL> if err is None : <EOL> action ( type , subtype ) <EOL> if err : <EOL> tgt = lnk . target <EOL> tgt . error = err <EOL> del self . _attachments [ tgt ] <EOL> tgt . closed = True <EOL> return <EOL> self . resolve ( sst , lnk . name , do_resolved , node_type = requested_type , <EOL> force = declare ) <EOL> def resolve_monkey ( self , sst , name , action , force = False , <EOL> node_type = None ) : <EOL> if not force and not node_type : <EOL> try : <EOL> type , subtype = self . address_cache [ name ] <EOL> action ( type , subtype ) <EOL> return <EOL> except KeyError : <EOL> pass <EOL> args = [ ] <EOL> def do_result ( r ) : <EOL> args . append ( r ) <EOL> def do_action ( r ) : <EOL> do_result ( r ) <EOL> er , qr = args <EOL> if node_type == '<STR_LIT>' and not er . not_found : <EOL> type , subtype = '<STR_LIT>' , er . type <EOL> elif node_type == '<STR_LIT>' and qr . queue : <EOL> type , subtype = '<STR_LIT>' , None <EOL> elif er . not_found and not qr . queue : <EOL> type , subtype = None , None <EOL> elif qr . queue : <EOL> type , subtype = '<STR_LIT>' , None <EOL> else : <EOL> type , subtype = '<STR_LIT>' , er . type <EOL> if type is not None : <EOL> self . address_cache [ name ] = ( type , subtype ) <EOL> action ( type , subtype ) <EOL> sst . write_query ( ExchangeQuery ( name ) , do_result ) <EOL> sst . write_query ( QueueQuery ( name ) , do_action ) <EOL> try : <EOL> import qpid . messaging . driver <EOL> qpid . messaging . driver . Engine . resolve_declare = resolve_declare_monkey <EOL> qpid . messaging . driver . Engine . resolve = resolve_monkey <EOL> except ImportError : <EOL> pass </s>
