o ?ca@sddlZddlZddlmZmZddlmZddlmZe e Z edgdZ dZ dZdZGd dde Zdd d ZGd d d ZGdddeZGdddeZGdddZdS)N)deque namedtuple)accepts_kwargs) EVENT_ALIASESNodeList)firstmiddlelastc@seZdZddZdS)rcCs4t|j}t|j}t|j}t|||}|SN)copyrrr r)selfZ first_copyZ middle_copyZ last_copyZcopiedri/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) responsesdefaultresponserrrfirst_non_none_response&s   rc@s`eZdZddZ dddZ dddZ dd d Zd d Z   dd 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.emitNFcC|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.registercCr)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()``. rN)r"_register_firstr$rrrregister_firstls  zBaseEventHooks.register_firstcCr)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()``. 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)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 unregisterszBaseEventHooks.unregistercCst|s td|dS)Nz"Event handler %s must be callable.)callable ValueErrorrfuncrrrr,s z"BaseEventHooks._verify_is_callablecCs4zt|s td|dWdStyYdSw)zVerifies 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_kwargsNFNNF) rrrrr'r)r+r"r.r,r-rrrrrBs     rc@sreZdZddZdddZddZdd Z dd d Z dd dZ dddZ ddZ dddZ ddZ d S)HierarchicalEmittercCsi|_t|_i|_dSr ) _lookup_cache _PrefixTrie _handlers_unique_id_handlersrrrr__init__s zHierarchicalEmitter.__init__FcCsg}|j|}|dur|j|}||j|<n|sgS||d<g}|D]!}td|||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)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. rCrrrrrs 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)rrrrrrremit_until_responses z'HierarchicalEmitter.emit_until_responseNcC|j||||tddSNsection)_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|||dd|i}|rFd|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.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)NrPzInitial 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=KeyErrorr0popr9 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_staterrrrs  zHierarchicalEmitter.__copy__)Fr4r5) rrrr<rCrrFr#r(r*rKr.rrrrrr6s" (    / +r6c@sreZdZdddZddZddZ dd d Z dd d Z dd dZ   dddZ ddZ ddZ ddZ 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[rrrraliased_event_namerrrr zEventAliaser.emitcKr\r )r]r[rFr^rrrrFr`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|jD]J\}}|d}d|vr/z ||||<Wnty.Yqw||vr@|d}||||nqd|}t d|d|||j|<|S||j|<|S)N.zChanging event name from z to ) rZrYitemssplitindexr0_replace_subsectionjoinr?r@)rrZold_partnew_partZ event_parts old_partsnew_namerrrr]s,        zEventAliaser._alias_event_namecCsZtt|D]$}|||dkr*|||t||kr*|g|||t|<dSqdS)Nr)rangelen)rsectionsrkrjirrrrhsz EventAliaser._replace_subsectioncCs|t|jt|jSr )rUr r[rYr;rrrrszEventAliaser.__copy__r r4r5) rrrr<rrFr'r)r+r.r]rhrrrrrrXs"       & rXc@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]}||dvr!|did}||d|<|}q |d|}q |ddur6tggg|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. rdrrN)rqrsrrrs)rfrtrrA)rkeyvaluerJ key_partscurrentpart new_childrrrrQs     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"). 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 extendleftreversedr=rA)rZ starting_noderwr|Zstarting_indexstackZ key_parts_len current_noderg node_listZcomplete_orderrrZdirectsZwildcardZ next_indexrrrr{;s&    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. 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=rrremoverr r0ri)rrrwrvrgZ next_noderrrrrgs&     z_PrefixTrie._remove_itemcCs|}||j}||_|Sr )rU_recursive_copyrV)rZnew_copyZ copied_attrsrrrrs z_PrefixTrie.__copy__cCsVi}|D]"\}}t|trt|||<qt|tr$||||<q|||<q|Sr )re isinstancerr dictr)rnodeZ copied_noderurvrrrrs   z_PrefixTrie._recursive_copyN) rrr__doc__r<rLrQr>r{rTrrrrrrrr8s    r8r )r logging collectionsrrZbotocore.compatrZbotocore.utilsr getLoggerrr?Z _NodeListrNrLrOrrrr6rXr8rrrrs$     Qi