U C^_@sddlZddlZddlmZmZmZddlmZmZddl m Z e e Z eddddgZdZd Zd ZGd ddeZdd d ZGdddeZGdddeZGdddeZGdddeZdS)N) defaultdictdeque namedtuple)accepts_kwargssix) EVENT_ALIASESNodeListfirstmiddlelastc@seZdZddZdS)rcCs4t|j}t|j}t|j}t|||}|SN)copyr r r r)selfZ first_copyZ middle_copyZ last_copycopiedr4/tmp/pip-install-6_kvzl1k/botocore/botocore/hooks.py__copy__s     zNodeList.__copy__N)__name__ __module__ __qualname__rrrrrrscCs&|D]}|ddk r|dSq|S)aFind first non None response in a list of tuples. This function can be used to find the first non None response from handlers connected to an event. This is useful if you are interested in the returned responses from event handlers. Example usage:: print(first_non_none_response([(func1, None), (func2, 'foo'), (func3, 'bar')])) # This will print 'foo' :type responses: list of tuples :param responses: The responses from the ``EventHooks.emit`` method. This is a list of tuples, and each tuple is (handler, handler_response). :param default: If no non-None responses are found, then this default value will be returned. :return: The first non-None response in the list of tuples. r Nr) responsesdefaultresponserrrfirst_non_none_response&s rc@sTeZdZddZdddZdddZdd d Zd d Zdd dZddZ ddZ dS)BaseEventHookscKsgS)aCall all handlers subscribed to an event. :type event_name: str :param event_name: The name of the event to emit. :type **kwargs: dict :param **kwargs: Arbitrary kwargs to pass through to the subscribed handlers. The ``event_name`` will be injected into the kwargs so it's not necesary to add this to **kwargs. :rtype: list of tuples :return: A list of ``(handler_func, handler_func_return_value)`` rr event_namekwargsrrremitCszBaseEventHooks.emitNFcCs|j||||j|ddS)a@Register an event handler for a given event. If a ``unique_id`` is given, the handler will not be registered if a handler with the ``unique_id`` has already been registered. Handlers are called in the order they have been registered. Note handlers can also be registered with ``register_first()`` and ``register_last()``. All handlers registered with ``register_first()`` are called before handlers registered with ``register()`` which are called before handlers registered with ``register_last()``. register_methodunique_id_uses_countN)_verify_and_register _registerrrhandler unique_idr#rrrregisterTs zBaseEventHooks.registercCs|j||||j|ddS)zRegister an event handler to be called first for an event. All event handlers registered with ``register_first()`` will be called before handlers registered with ``register()`` and ``register_last()``. r!N)r$_register_firstr&rrrregister_firstgs zBaseEventHooks.register_firstcCs|j||||j|ddS)zRegister an event handler to be called last for an event. All event handlers registered with ``register_last()`` will be called after handlers registered with ``register_first()`` and ``register()``. r!N)r$_register_lastr&rrr register_lastts zBaseEventHooks.register_lastcCs&|||||||||dSr)_verify_is_callable_verify_accept_kwargs)rrr'r(r"r#rrrr$s  z#BaseEventHooks._verify_and_registercCsdS)zUnregister an event handler for a given event. If no ``unique_id`` was given during registration, then the first instance of the event handler is removed (if the event handler has been registered multiple times). Nrr&rrr unregisters zBaseEventHooks.unregistercCst|std|dS)Nz"Event handler %s must be callable.)rcallable ValueErrorrfuncrrrr.s z"BaseEventHooks._verify_is_callablecCs4zt|std|Wntk r.YdSXdS)zVerifies a callable accepts kwargs :type func: callable :param func: A callable object. :returns: True, if ``func`` accepts kwargs, otherwise False. z9Event handler %s must accept keyword arguments (**kwargs)FN)rr2 TypeErrorr3rrrr/s  z$BaseEventHooks._verify_accept_kwargs)NF)NF)NF)NNF) rrrr r)r+r-r$r0r.r/rrrrrBs     rc@sfeZdZddZdddZddZdd Zdd d Zdd dZdddZ ddZ dddZ ddZ d S)HierarchicalEmittercCsi|_t|_i|_dSr) _lookup_cache _PrefixTrie _handlers_unique_id_handlersrrrr__init__szHierarchicalEmitter.__init__FcCsg}|j|}|dkr0|j|}||j|<n|s8gS||d<g}|D]>}td|||f|}|||f|rH|dk rH|SqH|S)a Emit an event with optional keyword arguments. :type event_name: string :param event_name: Name of the event :type kwargs: dict :param kwargs: Arguments to be passed to the handler functions. :type stop_on_response: boolean :param stop_on_response: Whether to stop on the first non-None response. If False, then all handlers will be called. This is especially useful to handlers which mutate data and then want to stop propagation of the event. :rtype: list :return: List of (handler, response) tuples from all processed handlers. NrzEvent %s: calling handler %s)r7getr9 prefix_searchloggerdebugappend)rrrstop_on_responserZhandlers_to_callr'rrrr_emits       zHierarchicalEmitter._emitcKs |||S)a; Emit an event by name with arguments passed as keyword args. >>> responses = emitter.emit( ... 'my-event.service.operation', arg1='one', arg2='two') :rtype: list :return: List of (handler, response) tuples from all processed handlers. rCrrrrr s zHierarchicalEmitter.emitcKs$|j||dd}|r|dSdSdS)a Emit an event by name with arguments passed as keyword args, until the first non-``None`` response is received. This method prevents subsequent handlers from being invoked. >>> handler, response = emitter.emit_until_response( 'my-event.service.operation', arg1='one', arg2='two') :rtype: tuple :return: The first (handler, response) tuple where the response is not ``None``, otherwise (``None``, ``None``). T)rB)NNNrD)rrrrrrremit_until_responses z'HierarchicalEmitter.emit_until_responseNcCs|j||||tddSNsection)_register_section_MIDDLEr&rrrr%s zHierarchicalEmitter._registercCs|j||||tddSrG)rJ_FIRSTr&rrrr*s z#HierarchicalEmitter._register_firstcCs|j||||tddSrG)rJ_LASTr&rrrr,s z"HierarchicalEmitter._register_lastcCs|dk r||jkrf|j|dd}|rR|s:td|qb|j|dd7<n|rbtd|dS|jj|||dd|i}|rd|d<||j|<n|jj|||di|_dS)NcountzInitial registration of unique id %s was specified to use a counter. Subsequent register calls to unique id must specify use of a counter as well.r zInitial registration of unique id %s was specified to not use a counter. Subsequent register calls to unique id must specify not to use a counter as well.rHr')r:r=r2r9 append_itemr7)rrr'r(r#rIrNZunique_id_handler_itemrrrrJs6   z%HierarchicalEmitter._register_sectioncCs|dk rz|j|dd}Wntk r4YdSX|r|dkrPtd|q|dkrj|j|d}q|j|dd8<dSn |rtd||j|d}z|j||i|_Wntk rYnXdS)NrNzInitial registration of unique id %s was specified to use a counter. Subsequent unregister calls to unique id must specify use of a counter as well.r r'zInitial registration of unique id %s was specified to not use a counter. Subsequent unregister calls to unique id must specify not to use a counter as well.)r:r=KeyErrorr2popr9 remove_itemr7)rrr'r(r#rNrrrr00s8 zHierarchicalEmitter.unregistercCs<|}|j}t|j|d<t|j|d<||_|S)Nr9r:) __class____dict__rr9r:)rZ new_instanceZ new_staterrrrRs  zHierarchicalEmitter.__copy__)F)NF)NF)F)NNF) rrrr<rCr rFr%r*r,rJr0rrrrrr6s" (    ( "r6c@sfeZdZdddZddZddZdd d Zdd d Zdd dZdddZ ddZ ddZ ddZ dS) EventAliaserNcCs||_|dkrt|_||_dSr)_event_aliasesr_emitter)rZ event_emitterZ event_aliasesrrrr<\szEventAliaser.__init__cKs||}|jj|f|Sr)_alias_event_namerWr rrraliased_event_namerrrr bs zEventAliaser.emitcKs||}|jj|f|Sr)rXrWrFrYrrrrFfs z EventAliaser.emit_until_responseFcCs||}|j||||Sr)rXrWr)rrr'r(r#rZrrrr)js zEventAliaser.registercCs||}|j||||Sr)rXrWr+r[rrrr+qs zEventAliaser.register_firstcCs||}|j||||Sr)rXrWr-r[rrrr-xs zEventAliaser.register_lastcCs||}|j||||Sr)rXrWr0r[rrrr0s zEventAliaser.unregisterc Cs|jD]\}}|d}d|krRz||||<Wqvtk rNYq YqvXn$||kr |d}||||nq d|}td||f|S|S)N.z!Changing event name from %s to %s) rVitemssplitindexr2_replace_subsectionjoinr?r@)rrZold_partnew_partZ event_parts old_partsnew_namerrrrXs$    zEventAliaser._alias_event_namecCsZtt|D]H}|||dkr |||t||kr |g|||t|<dSq dS)Nr)rangelen)rsectionsrcrbirrrr`s z EventAliaser._replace_subsectioncCs|t|jt|jSr)rSrrWrVr;rrrrs  zEventAliaser.__copy__)N)NF)NF)NF)NNF) rrrr<r rFr)r+r-r0rXr`rrrrrrU[s$      rUc@sTeZdZdZddZefddZddZdd Zd d Z d d Z ddZ ddZ dS)r8ajSpecialized prefix trie that handles wildcards. The prefixes in this case are based on dot separated names so 'foo.bar.baz' is:: foo -> bar -> baz Wildcard support just means that having a key such as 'foo.bar.*.baz' will be matched with a call to ``get_items(key='foo.bar.ANYTHING.baz')``. You can think of this prefix trie as the equivalent as defaultdict(list), except that it can do prefix searches: foo.bar.baz -> A foo.bar -> B foo -> C Calling ``get_items('foo.bar.baz')`` will return [A + B + C], from most specific to least specific. cCsdidd|_dS)N)chunkchildrenvalues)_rootr;rrrr<sz_PrefixTrie.__init__cCs|d}|j}|D]:}||dkrB|did}||d|<|}q|d|}q|ddkrltggg|d<|d||dS)zAdd an item to a key. If a value is already associated with that key, the new value is appended to the list for the key. r\rjN)rirkrjrk)r^rlrrA)rkeyvaluerI key_partscurrentpart new_childrrrrOs     z_PrefixTrie.append_itemcCs*t}|d}|j}||||d|S)zCollect all items that are prefixes of key. Prefix in this case are delineated by '.' characters so 'foo.bar.baz' is a 3 chunk sequence of 3 "prefixes" ( "foo", "bar", and "baz"). r\r)rr^rl _get_items)rrm collectedrorprrrr>s  z_PrefixTrie.prefix_searchcCs||fg}t|}|r|\}}|drR|d} | j| j| j} |t| ||ks|d} | ||} | d} |d}| dk r|| |f| dk r|| |fqdS)Nrkrj*r ) rfrQr r r extendleftreversedr=rA)rZ starting_noderortZstarting_indexstackZ key_parts_len current_noder_ node_listZcomplete_orderrjZdirectsZwildcardZ next_indexrrrrss&    z_PrefixTrie._get_itemscCs&|d}|j}|j|||dddS)zRemove an item associated with a key. If the value is not associated with the key a ``ValueError`` will be raised. If the key does not exist in the trie, a ``ValueError`` will be raised. r\r)r_N)r^rl _remove_item)rrmrnrorprrrrRs z_PrefixTrie.remove_itemcCs|dkr dS|t|kr|d||}|dk r|||||d|t|dkr|d}||jkrv|j|n.||jkr|j|n||jkr|j||ds|ds|d||=ntdd|dS)Nrjr rkzkey is not in trie: %sr\) rfr=r{r remover r r2ra)rryrornr_Z next_noderzrrrr{s&      z_PrefixTrie._remove_itemcCs|}||j}||_|Sr)rS_recursive_copyrT)rZnew_copyZ copied_attrsrrrr7s z_PrefixTrie.__copy__cCsVi}|D]D\}}t|tr.t|||<q t|trH||||<q |||<q |Sr)r] isinstancerrdictr})rnodeZ copied_nodermrnrrrr}As   z_PrefixTrie._recursive_copyN) rrr__doc__r<rKrOr>rsrRr{rr}rrrrr8s   r8)N)rlogging collectionsrrrZbotocore.compatrrZbotocore.utilsr getLoggerrr?Z _NodeListrLrKrMrrobjectrr6rUr8rrrr s    d6Y