3 B\_@sddlZddlZddlmZmZmZddlmZmZddl m Z ej e Z eddddgZdZd Zd ZGd ddeZdd d ZGdddeZGdddeZGdddeZGdddeZdS)N) defaultdictdeque namedtuple)accepts_kwargssix) EVENT_ALIASESNodeListfirstmiddlelastc@seZdZddZdS)rcCs4tj|j}tj|j}tj|j}t|||}|S)N)copyr r r r)selfZ first_copyZ middle_copyZ last_copycopiedrh/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/botocore/botocore/hooks.py__copy__s     zNodeList.__copy__N)__name__ __module__ __qualname__rrrrrrscCs&x |D]}|ddk r|dSqW|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)`` r)r 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 _register)rrhandler 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()``. )rr N)r!_register_first)rrr#r$r 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()``. )rr N)r!_register_last)rrr#r$r rrr register_lastts zBaseEventHooks.register_lastcCs&|j||j||||||dS)N)_verify_is_callable_verify_accept_kwargs)rrr#r$rr 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). Nr)rrr#r$r rrr unregisters zBaseEventHooks.unregistercCstj|std|dS)Nz"Event handler %s must be callable.)rcallable ValueError)rfuncrrrr*s z"BaseEventHooks._verify_is_callablec Cs2yt|std|Wntk r,dSXdS)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)rr. TypeError)rr/rrrr+s  z$BaseEventHooks._verify_accept_kwargs)NF)NF)NF)NNF) rrrrr%r'r)r!r,r*r+rrrrrBs    rc@sfeZdZddZdddZddZdd Zdd d Zdd dZdddZ ddZ dddZ ddZ d S)HierarchicalEmittercCsi|_t|_i|_dS)N) _lookup_cache _PrefixTrie _handlers_unique_id_handlers)rrrr__init__szHierarchicalEmitter.__init__FcCsg}|jj|}|dkr0|jj|}||j|<n|s8gS||d<g}xB|D]:}tjd|||f|}|j||f|rJ|dk rJ|SqJW|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)r2getr4 prefix_searchloggerdebugappend)rrrstop_on_responserZhandlers_to_callr#rrrr_emits       zHierarchicalEmitter._emitcKs |j||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. )r=)rrrrrrrs 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)r<r N)NN)r=)rrrrrrremit_until_responses z'HierarchicalEmitter.emit_until_responseNcCs|j||||tddS)N)section)_register_section_MIDDLE)rrr#r$r rrrr"s zHierarchicalEmitter._registercCs|j||||tddS)N)r@)rA_FIRST)rrr#r$r rrrr&s z#HierarchicalEmitter._register_firstcCs|j||||tddS)N)r@)rA_LAST)rrr#r$r rrrr(s z"HierarchicalEmitter._register_lastcCs|dk r||jkrf|j|jdd}|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.)r@r#)r5r7r.r4 append_itemr2)rrr#r$r r@rEZunique_id_handler_itemrrrrAs,    z%HierarchicalEmitter._register_sectioncCs|dk ry|j|jdd}Wntk r2dSX|r|dkrNtd|q|dkrh|jj|d}q|j|dd8<dSn |rtd||jj|d}y|jj||i|_Wntk rYnXdS)NrEzInitial 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.)r5r7KeyErrorr.popr4 remove_itemr2)rrr#r$r rErrrr,0s0  zHierarchicalEmitter.unregistercCs<|j}|jj}tj|j|d<tj|j|d<||_|S)Nr4r5) __class____dict__rr4r5)rZ new_instanceZ new_staterrrrRs  zHierarchicalEmitter.__copy__)F)NF)NF)F)NNF) rrrr6r=rr?r"r&r(rAr,rrrrrr1s (    ( !r1c@sfeZdZdddZddZddZdd d Zdd d Zdd dZdddZ ddZ ddZ ddZ dS) EventAliaserNcCs||_|dkrt|_||_dS)N)_event_aliasesr_emitter)rZ event_emitterZ event_aliasesrrrr6\szEventAliaser.__init__cKs|j|}|jj|f|S)N)_alias_event_namerNr)rrraliased_event_namerrrrbs zEventAliaser.emitcKs|j|}|jj|f|S)N)rOrNr?)rrrrPrrrr?fs z EventAliaser.emit_until_responseFcCs|j|}|jj||||S)N)rOrNr%)rrr#r$r rPrrrr%js zEventAliaser.registercCs|j|}|jj||||S)N)rOrNr')rrr#r$r rPrrrr'qs zEventAliaser.register_firstcCs|j|}|jj||||S)N)rOrNr))rrr#r$r rPrrrr)xs zEventAliaser.register_lastcCs|j|}|jj||||S)N)rOrNr,)rrr#r$r rPrrrr,s zEventAliaser.unregisterc Csx|jjD]\}}|jd}d|krRy|||j|<Wqvtk rNw YqvXn$||kr |jd}|j|||nq dj|}tjd||f|SW|S)N.z!Changing event name from %s to %s) rMitemssplitindexr._replace_subsectionjoinr9r:)rrZold_partnew_partZ event_parts old_partsnew_namerrrrOs      zEventAliaser._alias_event_namecCs\xVtt|D]F}|||dkr|||t||kr|g|||t|<dSqWdS)Nr)rangelen)rsectionsrXrWirrrrUs z EventAliaser._replace_subsectioncCs|jtj|jtj|jS)N)rJrrNrM)rrrrrs zEventAliaser.__copy__)N)NF)NF)NF)NNF) rrrr6rr?r%r'r)r,rOrUrrrrrrL[s      rLc@sTeZdZdZddZefddZddZdd Zd d Z d d Z ddZ ddZ dS)r3ajSpecialized 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)_root)rrrrr6sz_PrefixTrie.__init__cCs|jd}|j}xB|D]:}||dkrD|did}||d|<|}q|d|}qW|ddkrptggg|d<|d|j|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. rQr_N)r^r`r_r`)rSrarr;)rkeyvaluer@ key_partscurrentpart new_childrrrrFs      z_PrefixTrie.append_itemcCs*t}|jd}|j}|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"). rQr)rrSra _get_items)rrb collectedrdrerrrr8s  z_PrefixTrie.prefix_searchcCs||fg}t|}x|r|j\}}|drT|d} | j| j| j} |jt| ||ks|d} | j||} | jd} |d}| dk r|j| |f| dk r|j| |fqWdS)Nr`r_*r ) r[rHr r r extendleftreversedr7r;)rZ starting_noderdriZstarting_indexstackZ key_parts_len current_noderT node_listZcomplete_orderr_ZdirectsZwildcardZ next_indexrrrrhs$    z_PrefixTrie._get_itemscCs&|jd}|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. rQr)rTN)rSra _remove_item)rrbrcrdrerrrrIs z_PrefixTrie.remove_itemcCs|dkr dS|t|kr|dj||}|dk r|j||||d|t|dkr|d}||jkrv|jj|n.||jkr|jj|n||jkr|jj||d r|d r|d||=ntddj|dS)Nr_r r`zkey is not in trie: %srQ) r[r7rpr remover r r.rV)rrnrdrcrTZ next_noderorrrrps$     z_PrefixTrie._remove_itemcCs|j}|j|j}||_|S)N)rJ_recursive_copyrK)rZnew_copyZ copied_attrsrrrr7s z_PrefixTrie.__copy__cCsZi}xP|jD]D\}}t|tr0tj|||<qt|trJ|j|||<q|||<qW|S)N)rR isinstancerrdictrr)rnodeZ copied_noderbrcrrrrrAs   z_PrefixTrie._recursive_copyN) rrr__doc__r6rBrFr8rhrIrprrrrrrrr3s   r3)N)rlogging collectionsrrrZbotocore.compatrrZbotocore.utilsr getLoggerrr9Z _NodeListrCrBrDrrobjectrr1rLr3rrrr s    d6Y