o .&aa@sddlZddlZddlmZmZddlmZmZddlm Z e e Z edgdZ dZdZdZGd dde Zdd d ZGd d d eZGdddeZGdddeZGdddeZdS)N)deque namedtuple)accepts_kwargssix) EVENT_ALIASESNodeList)firstmiddlelastc@seZdZddZdS)rcCs4t|j}t|j}t|j}t|||}|SN)copyrr r r)self first_copy middle_copy last_copycopiedr0/usr/lib/python3/dist-packages/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@sdeZdZddZ  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 _registerrr handler 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()``. r$N)r'_register_firstr)rrrregister_firstgs  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()``. r$N)r'_register_lastr)rrr register_lastts  zBaseEventHooks.register_lastcCs&|||||||||dSr )_verify_is_callable_verify_accept_kwargs)rr r*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|s td|dS)Nz"Event handler %s must be callable.)rcallable ValueErrorrfuncrrrr1s  z"BaseEventHooks._verify_is_callablecCs0z t|s td|WdStyYdSw)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)rr5 TypeErrorr6rrrr2s  z$BaseEventHooks._verify_accept_kwargsNFNNF) rrrr"r,r.r0r'r3r1r2rrrrrBs"       rc@steZdZddZdddZddZdd 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. Nr zEvent %s: calling handler %sr)r<getr> prefix_searchloggerdebugappend)rr r!stop_on_responserhandlers_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. rIrrrrr"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)rG)NNrJ)rr r!rrrremit_until_responses z'HierarchicalEmitter.emit_until_responseNcC|j||||tddSNsection)_register_section_MIDDLEr)rrrr(  zHierarchicalEmitter._registercCrMrN)rQ_FIRSTr)rrrr-rSz#HierarchicalEmitter._register_firstcCrMrN)rQ_LASTr)rrrr/rSz"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.rOr*)r?rBr5r> append_itemr<)rr r*r+r&rPrVunique_id_handler_itemrrrrQs8    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)NrVzInitial 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?rBKeyErrorr5popr> remove_itemr<)rr r*r+r&rVrrrr30s<   zHierarchicalEmitter.unregistercCs<|}|j}t|j|d<t|j|d<||_|S)Nr>r?) __class____dict__rr>r?)r new_instance new_staterrrrRs  zHierarchicalEmitter.__copy__)Fr9r:) rrrrArIr"rLr(r-r/rQr3rrrrrr;s$ (    ( "r;c@sveZdZdddZddZddZ  dd d Z  dd d Z  dd dZ  dddZ ddZ ddZ ddZ dS) EventAliaserNcCs$||_|dur t|_i|_||_dSr )_event_aliasesr_alias_name_cache_emitter)r event_emitter event_aliasesrrrrA\s  zEventAliaser.__init__cK||}|jj|fi|Sr )_alias_event_namercr"rr r!aliased_event_namerrrr"c zEventAliaser.emitcKrfr )rgrcrLrhrrrrLgrjz EventAliaser.emit_until_responseFcC||}|j||||Sr )rgrcr,rr r*r+r&rirrrr,k zEventAliaser.registercCrkr )rgrcr.rlrrrr.rrmzEventAliaser.register_firstcCrkr )rgrcr0rlrrrr0yrmzEventAliaser.register_lastcCrkr )rgrcr3rlrrrr3rmzEventAliaser.unregisterc Cs||jvr |j|S|jD]H\}}|d}d|vr/z ||||<Wnty.Yqw||vr@|d}||||nqd|}t d||f||j|<|S||j|<|S)N.z!Changing event name from %s to %s) rbraitemssplitindexr5_replace_subsectionjoinrDrE)rr old_partnew_part event_parts old_partsnew_namerrrrgs,        zEventAliaser._alias_event_namecCsZtt|D]$}|||dkr*|||t||kr*|g|||t|<dSqdS)Nr)rangelen)rsectionsrwruirrrrrsz EventAliaser._replace_subsectioncCs|t|jt|jSr )r\rrcrar@rrrrs  zEventAliaser.__copy__r r9r:) rrrrAr"rLr,r.r0r3rgrrrrrrrr`[s&     & r`c@sTeZdZdZddZefddZddZdd Zd d Z d d Z ddZ ddZ dS)r=ajSpecialized 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@rrrrAsz_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. rnr~N)r}rr~r)rprrrF)rkeyvaluerP key_partscurrentpart new_childrrrrWs     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"). rnr)rrpr _get_items)rr collectedrrrrrrCs  z_PrefixTrie.prefix_searchcCs||fg}t|}|r[|\}}|dr)|d} | j| j| j} |t| ||ksW|d} | ||} | d} |d}| durL|| |f| durW|| |f|s dSdS)Nrr~*r ) rzrZrr r extendleftreversedrBrF)r starting_noderrstarting_indexstack key_parts_len current_noderq node_listcomplete_orderr~directswildcard next_indexrrrrs&    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. rnr)rqN)rpr _remove_item)rrrrrrrrr[s z_PrefixTrie.remove_itemcCs|durdS|t|krp|d||}|durg|||||d|t|dkrR|d}||jvr;|j|n||jvrG|j|n ||jvrR|j||dsc|dse|d||=dSdSdStdd|dS)Nr~r rzkey is not in trie: %srn) rzrBrrremover r r5rs)rrrrrq next_noderrrrr(s*      z_PrefixTrie._remove_itemcCs|}||j}||_|Sr )r\_recursive_copyr])rnew_copy copied_attrsrrrrAs z_PrefixTrie.__copy__cCsVi}|D]"\}}t|trt|||<qt|tr$||||<q|||<q|Sr )ro isinstancerrdictr)rnode copied_noderrrrrrKs   z_PrefixTrie._recursive_copyN) rrr__doc__rArRrWrCrr[rrrrrrrr=s    r=r )rlogging collectionsrrbotocore.compatrrbotocore.utilsr getLoggerrrD _NodeListrTrRrUrrobjectrr;r`r=rrrrs"    d6b