B ㊇ca@sddlZddlZddlmZmZddlmZddlmZe e Z eddddgZ dZ d Zd ZGd dde Zdd d ZGdddZGdddeZGdddeZGdddZdS)N)deque namedtuple)accepts_kwargs) EVENT_ALIASESNodeListfirstmiddlelastc@seZdZddZdS)rcCs4t|j}t|j}t|j}t|||}|S)N)copyrrr r)selfZ first_copyZ middle_copyZ last_copycopiedri/private/var/folders/8c/hx9_v10d5x38qmnzt13b7b8j1k3n5b/T/pip-target-x6xd5gna/lib/python/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)r rhandler unique_idrrrrregisterTs 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()``. )rrN)r_register_first)r rr!r"rrrrregister_firstls 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()``. )rrN)r_register_last)r rr!r"rrrr register_last~s zBaseEventHooks.register_lastcCs&|||||||||dS)N)_verify_is_callable_verify_accept_kwargs)r rr!r"rrrrrrs  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)r rr!r"rrrr unregisterszBaseEventHooks.unregistercCst|std|dS)Nz"Event handler %s must be callable.)callable ValueError)r funcrrrr(sz"BaseEventHooks._verify_is_callablecCs6yt|std|dWntk r0dSXdS)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)rr, TypeError)r r-rrrr)s z$BaseEventHooks._verify_accept_kwargs)NF)NF)NF)NNF) rrrrr#r%r'rr*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)r rrr__init__szHierarchicalEmitter.__init__FcCsg}|j|}|dkr0|j|}||j|<n|s8gS||d<g}xB|D]:}td|||f|}|||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)r0getr2 prefix_searchloggerdebugappend)r rrstop_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. )r;)r rrrrrrs 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:)NNN)r;)r rrrrrremit_until_responses z'HierarchicalEmitter.emit_until_responseNcCs|j||||tddS)N)section)_register_section_MIDDLE)r rr!r"rrrrr s zHierarchicalEmitter._registercCs|j||||tddS)N)r>)r?_FIRST)r rr!r"rrrrr$ s z#HierarchicalEmitter._register_firstcCs|j||||tddS)N)r>)r?_LAST)r rr!r"rrrrr&+sz"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.)r>r!)r3r5r,r2 append_itemr0)r rr!r"rr>rCZunique_id_handler_itemrrrr?2s,    z%HierarchicalEmitter._register_sectioncCs|dk ry|j|dd}Wntk r2dSX|r|dkrNtd|q|dkrh|j|d}q|j|dd8<dSn |rtd||j|d}y|j||i|_Wntk rYnXdS)NrCzInitial 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.)r3r5KeyErrorr,popr2 remove_itemr0)r rr!r"rrCrrrr*^s2   zHierarchicalEmitter.unregistercCs<|}|j}t|j|d<t|j|d<||_|S)Nr2r3) __class____dict__r r2r3)r Z new_instance new_staterrrrs  zHierarchicalEmitter.__copy__)F)NF)NF)F)NNF) rrrr4r;rr=r r$r&r?r*rrrrrr/s (  / &r/c@sfeZdZdddZddZddZdd d Zdd d Zdd dZdddZ ddZ ddZ ddZ dS) EventAliaserNcCs$||_|dkrt|_i|_||_dS)N)_event_aliasesr_alias_name_cache_emitter)r Z event_emitterZ event_aliasesrrrr4s zEventAliaser.__init__cKs||}|jj|f|S)N)_alias_event_namerNr)r rraliased_event_namerrrrs zEventAliaser.emitcKs||}|jj|f|S)N)rOrNr=)r rrrPrrrr=s z EventAliaser.emit_until_responseFcCs||}|j||||S)N)rOrNr#)r rr!r"rrPrrrr#s zEventAliaser.registercCs||}|j||||S)N)rOrNr%)r rr!r"rrPrrrr%s zEventAliaser.register_firstcCs||}|j||||S)N)rOrNr')r rr!r"rrPrrrr's zEventAliaser.register_lastcCs||}|j||||S)N)rOrNr*)r rr!r"rrPrrrr*s zEventAliaser.unregisterc Cs||jkr|j|Sx|jD]\}}|d}d|krfy||||<Wqtk rbw YqXn$||kr |d}||||nq d|}t d|d|||j|<|SW||j|<|S)N.zChanging event name from z to ) rMrLitemssplitindexr,_replace_subsectionjoinr7r8)r rZold_partnew_partZ event_parts old_partsnew_namerrrrOs(        zEventAliaser._alias_event_namecCs\xVtt|D]F}|||dkr|||t||kr|g|||t|<dSqWdS)Nr)rangelen)r sectionsrXrWirrrrUs z EventAliaser._replace_subsectioncCs|t|jt|jS)N)rHr rNrL)r rrrrszEventAliaser.__copy__)N)NF)NF)NF)NNF) rrrr4rr=r#r%r'r*rOrUrrrrrrKs     & rKc@sTeZdZdZddZefddZddZdd Zd d Z d d Z ddZ ddZ dS)r1ajSpecialized 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)r rrrr4sz_PrefixTrie.__init__cCs|d}|j}xB|D]:}||dkrD|did}||d|<|}q|d|}qW|ddkrptggg|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. rQr_N)r^r`r_r`)rSrarr9)r keyvaluer> key_partscurrentpart new_childrrrrDs      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"). rQr)rrSra _get_items)r rb collectedrdrerrrr6-s  z_PrefixTrie.prefix_searchcCs||fg}t|}x|r|\}}|drT|d} | j| j| j} |t| ||ks|d} | ||} | d} |d}| dk r|| |f| dk r|| |fqWdS)Nr`r_*r ) r[rFrrr extendleftreversedr5r9)r Z starting_noderdriZstarting_indexstackZ key_parts_len current_noderT node_listZcomplete_orderr_ZdirectsZwildcardZ next_indexrrrrh;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. rQr)rTN)rSra _remove_item)r rbrcrdrerrrrG[s 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)Nr_r r`zkey is not in trie: rQ) r[r5rprremoverr r,rV)r rnrdrcrTZ next_noderorrrrpgs"     z_PrefixTrie._remove_itemcCs|}||j}||_|S)N)rH_recursive_copyrI)r Znew_copyZ copied_attrsrrrrs z_PrefixTrie.__copy__cCsZi}xP|D]D\}}t|tr0t|||<qt|trJ||||<q|||<qW|S)N)rR isinstancerr dictrr)r nodeZ copied_noderbrcrrrrrs   z_PrefixTrie._recursive_copyN) rrr__doc__r4r@rDr6rhrGrprrrrrrrr1s    r1)N)r logging collectionsrrZbotocore.compatrZbotocore.utilsr getLoggerrr7Z _NodeListrAr@rBrrrr/rKr1rrrr s"    Qi