o ˆü?cäaã@s¶ddlZddlZddlmZmZddlmZddlmZe  e ¡Z edgd¢ƒZ dZ dZdZGd d„de ƒZdd d „ZGd d „d ƒZGdd„deƒZGdd„deƒZGdd„dƒZdS)éN)ÚdequeÚ namedtuple)Úaccepts_kwargs)Ú EVENT_ALIASESÚNodeList)ÚfirstÚmiddleÚlastééc@seZdZdd„ZdS)rcCs4t |j¡}t |j¡}t |j¡}t|||ƒ}|S©N)Úcopyrrr r)ÚselfZ first_copyZ middle_copyZ last_copyZcopied©rúi/private/var/folders/cw/wlscbxl13mj6wd668h7l9g9sllkg5j/T/pip-target-b31awkwq/lib/python/botocore/hooks.pyÚ__copy__s    zNodeList.__copy__N)Ú__name__Ú __module__Ú __qualname__rrrrrrs cCs&|D]}|ddur|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)Ú responsesÚdefaultÚresponserrrÚfirst_non_none_response&s   ÿrc@s`eZdZdd„Z ddd„Z ddd„Z dd d „Zd d „Z   dd d„Zdd„Z dd„Z dS)ÚBaseEventHookscKsgS)aäCall 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_nameÚkwargsrrrÚemitCszBaseEventHooks.emitNFcCó|j||||j|d�dS)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_methodÚunique_id_uses_countN)Ú_verify_and_registerÚ _register©rrÚhandlerÚ unique_idr!rrrÚregisterTs ûzBaseEventHooks.registercCr)zìRegister 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()``. rN)r"Ú_register_firstr$rrrÚregister_firstls  ûzBaseEventHooks.register_firstcCr)zâRegister 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()``. rN)r"Ú_register_lastr$rrrÚ register_last~s  ûzBaseEventHooks.register_lastcCs&| |¡| |¡|||||ƒdSr )Ú_verify_is_callableÚ_verify_accept_kwargs)rrr%r&r r!rrrr"�s  z#BaseEventHooks._verify_and_registercCsdS)z÷Unregister 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Ú unregister›szBaseEventHooks.unregistercCst|ƒs td|ƒ‚dS)Nz"Event handler %s must be callable.)ÚcallableÚ ValueError©rÚfuncrrrr,«s ÿz"BaseEventHooks._verify_is_callablecCs4zt|ƒs td|›d�ƒ‚WdStyYdSw)z¸Verifies a callable accepts kwargs :type func: callable :param func: A callable object. :returns: True, if ``func`` accepts kwargs, otherwise False. zEvent handler z) must accept keyword arguments (**kwargs)FN)rr0Ú TypeErrorr1rrrr-¯s  ÿÿ ÿz$BaseEventHooks._verify_accept_kwargs©NF©NNF) rrrrr'r)r+r"r.r,r-rrrrrBs ÿ ÿ ÿ û rc@sreZdZdd„Zddd„Zdd„Zdd „Z dd d „Z dd d„Z ddd„Z dd„Z ddd„Z dd„Z d S)ÚHierarchicalEmittercCsi|_tƒ|_i|_dSr )Ú _lookup_cacheÚ _PrefixTrieÚ _handlersÚ_unique_id_handlers©rrrrÚ__init__Ãs zHierarchicalEmitter.__init__FcCs�g}|j |¡}|dur|j |¡}||j|<n|sgS||d<g}|D]!}t d||¡|di|¤Ž}| ||f¡|rE|durE|Sq$|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 %sr)r7Úgetr9Ú prefix_searchÚloggerÚdebugÚappend)rrrÚstop_on_responserZhandlers_to_callr%rrrrÚ_emitÍs"    €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|dSdS)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éÿÿÿÿ)NNrD)rrrrrrrÚemit_until_responses z'HierarchicalEmitter.emit_until_responseNcCó|j||||td�dS©N©Úsection)Ú_register_sectionÚ_MIDDLEr$rrrr#ó ûzHierarchicalEmitter._registercCrGrH)rKÚ_FIRSTr$rrrr( rMz#HierarchicalEmitter._register_firstcCrGrH)rKÚ_LASTr$rrrr*+s  ÿz"HierarchicalEmitter._register_lastcCs´|durL||jvr3|j| dd¡}|r)|std|ƒ‚|j|dd7<dS|r1td|ƒ‚dS|jj|||d�d|i}|rFd|d<||j|<n |jj|||d�i|_dS)NÚcountz“Initial 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 zšInitial 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.rIr%)r:r=r0r9Ú append_itemr7)rrr%r&r!rJrPZunique_id_handler_itemrrrrK2s8 ýÿ ùýÿÿ  z%HierarchicalEmitter._register_sectioncCsÐ|durPz |j| dd¡}Wn tyYdSw|r@|dur&td|ƒ‚|dkr3|j |¡d}n|j|dd8<dS|rHtd|ƒ‚|j |¡d}z |j ||¡i|_WdStygYdSw)NrPz”Initial 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%zœInitial 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=ÚKeyErrorr0Úpopr9Ú remove_itemr7)rrr%r&r!rPrrrr.^s@ ýþÿ ÿýÿ  ÿzHierarchicalEmitter.unregistercCs<| ¡}|j ¡}t |j¡|d<t |j¡|d<||_|S)Nr9r:)Ú __class__Ú__dict__r r9r:)rZ new_instanceÚ new_staterrrr‰s  zHierarchicalEmitter.__copy__)Fr4r5) rrrr<rCrrFr#r(r*rKr.rrrrrr6Âs" (  ÿ ÿ ÿ/ û +r6c@sreZdZddd„Zdd„Zdd„Z dd d „Z dd d „Z dd d„Z   ddd„Z dd„Z dd„Z dd„Z dS)Ú EventAliaserNcCs$||_|dur t|_i|_||_dSr )Ú_event_aliasesrÚ_alias_name_cacheÚ_emitter)rZ event_emitterZ event_aliasesrrrr<“s  zEventAliaser.__init__cKó| |¡}|jj|fi|¤ŽSr )Ú_alias_event_namer[r©rrrÚaliased_event_namerrrršó zEventAliaser.emitcKr\r )r]r[rFr^rrrrFžr`z EventAliaser.emit_until_responseFcCó| |¡}|j ||||¡Sr )r]r[r'©rrr%r&r!r_rrrr'¢ó ÿzEventAliaser.registercCrar )r]r[r)rbrrrr)ªrczEventAliaser.register_firstcCrar )r]r[r+rbrrrr+²rczEventAliaser.register_lastcCrar )r]r[r.rbrrrr.ºs ÿzEventAliaser.unregisterc CsÂ||jvr |j|S|j ¡D]J\}}| d¡}d|vr/z ||| |¡<Wnty.Yqw||vr@| d¡}| |||¡nqd |¡}t  d|›d|›�¡||j|<|S||j|<|S)NÚ.zChanging event name from z to ) rZrYÚitemsÚsplitÚindexr0Ú_replace_subsectionÚjoinr?r@)rrZold_partÚnew_partZ event_partsÚ old_partsÚnew_namerrrr]Æs,    ÿ  ÿ  zEventAliaser._alias_event_namecCsZtt|ƒƒD]$}|||dkr*|||t|ƒ…|kr*|g|||t|ƒ…<dSqdS)Nr)ÚrangeÚlen)rÚsectionsrkrjÚirrrrhìsÿþ€úz EventAliaser._replace_subsectioncCs| t |j¡t |j¡¡Sr )rUr r[rYr;rrrrõsÿzEventAliaser.__copy__r r4r5) rrrr<rrFr'r)r+r.r]rhrrrrrrX’s"  ÿ ÿ ÿ  û & rXc@sTeZdZdZdd„Zefdd„Zdd„Zdd „Zd d „Z d d „Z dd„Z dd„Z 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)ÚchunkÚchildrenÚvalues)Ú_rootr;rrrr<sz_PrefixTrie.__init__cCs‚| d¡}|j}|D]}||dvr!|didœ}||d|<|}q |d|}q |ddur6tgggƒ|d<|d| |¡dS)z’Add 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. rdrrN)rqrsrrrs)rfrtrrA)rÚkeyÚvaluerJÚ key_partsÚcurrentÚpartÚ new_childrrrrQs     z_PrefixTrie.append_itemcCs*tƒ}| d¡}|j}| |||d¡|S)zÖCollect 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"). rdr)rrfrtÚ _get_items)rruÚ collectedrwrxrrrr>-s  z_PrefixTrie.prefix_searchcCsº||fg}t|ƒ}|r[| ¡\}}|dr)|d} | j| j| j} | t| ƒ¡||ksW|d} |  ||¡} |  d¡} |d}| durL| | |f¡| durW| | |f¡|s dSdS)NrsrrÚ*r ) rnrSrrr Ú extendleftÚreversedr=rA)rZ starting_noderwr|Zstarting_indexÚstackZ key_parts_lenÚ current_nodergÚ node_listZcomplete_orderrrZdirectsZwildcardZ next_indexrrrr{;s&  ÿ  éz_PrefixTrie._get_itemscCs&| d¡}|j}|j|||dd�dS)zÞRemove 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. rdr)rgN)rfrtÚ _remove_item)rrurvrwrxrrrrT[s z_PrefixTrie.remove_itemcCsæ|durdS|t|ƒkrq|d ||¡}|durg| ||||d¡|t|ƒdkrR|d}||jvr;|j |¡n||jvrG|j |¡n ||jvrR|j |¡|dsc|dse|d||=dSdSdStdd |¡›�ƒ‚dS)Nrrr rszkey is not in trie: rd) rnr=rƒrÚremoverr r0ri)rr�rwrvrgZ next_noder‚rrrrƒgs&     ûíz_PrefixTrie._remove_itemcCs| ¡}| |j¡}||_|Sr )rUÚ_recursive_copyrV)rZnew_copyZ copied_attrsrrrrs z_PrefixTrie.__copy__cCsVi}| ¡D]"\}}t|tƒrt |¡||<qt|tƒr$| |¡||<q|||<q|Sr )reÚ isinstancerr Údictr…)rÚnodeZ copied_noderurvrrrr…‰s   z_PrefixTrie._recursive_copyN) rrrÚ__doc__r<rLrQr>r{rTrƒrr…rrrrr8ûs    r8r )r ÚloggingÚ collectionsrrZbotocore.compatrZbotocore.utilsrÚ getLoggerrr?Z _NodeListrNrLrOrrrr6rXr8rrrrÚs$     Qi