>ac@s ddlZddlZddlmZmZddlmZmZddlm Z ej e Z eddddgZ d Zd Zd Zde fd YZdd ZdefdYZdefdYZdefdYZdefdYZdS(iN(tdequet namedtuple(taccepts_kwargstsix(t EVENT_ALIASEStNodeListtfirsttmiddletlastiiicBseZdZRS(cCsLtj|j}tj|j}tj|j}t|||}|S(N(tcopyRRRR(tselft first_copyt middle_copyt last_copytcopied((s//usr/lib/fence-agents/bundled/botocore/hooks.pyt__copy__s (t__name__t __module__R(((s//usr/lib/fence-agents/bundled/botocore/hooks.pyRscCs-x&|D]}|ddk r|dSqW|S(sFind 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. iN(tNone(t responsestdefaulttresponse((s//usr/lib/fence-agents/bundled/botocore/hooks.pytfirst_non_none_response&s  tBaseEventHookscBskeZdZdedZdedZdedZdZddedZ dZ dZ RS( cKsgS(sCall 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 t event_nametkwargs((s//usr/lib/fence-agents/bundled/botocore/hooks.pytemitCscCs&|j|||d|jd|dS(s@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()``. tregister_methodtunique_id_uses_countN(t_verify_and_registert _register(R Rthandlert unique_idR((s//usr/lib/fence-agents/bundled/botocore/hooks.pytregisterTs cCs&|j|||d|jd|dS(sRegister 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(Rt_register_first(R RRR R((s//usr/lib/fence-agents/bundled/botocore/hooks.pytregister_firstgs  cCs&|j|||d|jd|dS(sRegister 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(Rt_register_last(R RRR R((s//usr/lib/fence-agents/bundled/botocore/hooks.pyt register_lastts cCs1|j||j||||||dS(N(t_verify_is_callablet_verify_accept_kwargs(R RRR RR((s//usr/lib/fence-agents/bundled/botocore/hooks.pyRs  cCsdS(sUnregister 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). N((R RRR R((s//usr/lib/fence-agents/bundled/botocore/hooks.pyt unregisters cCs&tj|s"td|ndS(Ns"Event handler %s must be callable.(Rtcallablet ValueError(R tfunc((s//usr/lib/fence-agents/bundled/botocore/hooks.pyR&scCs<y#t|s"td|nWntk r7tSXdS(sVerifies a callable accepts kwargs :type func: callable :param func: A callable object. :returns: True, if ``func`` accepts kwargs, otherwise False. s9Event handler %s must accept keyword arguments (**kwargs)N(RR*t TypeErrortFalse(R R+((s//usr/lib/fence-agents/bundled/botocore/hooks.pyR's   N( RRRRR-R!R#R%RR(R&R'(((s//usr/lib/fence-agents/bundled/botocore/hooks.pyRBs     tHierarchicalEmittercBs}eZdZedZdZdZd edZd edZ edZ dZ d d edZ d Z RS( cCs"i|_t|_i|_dS(N(t _lookup_cachet _PrefixTriet _handlerst_unique_id_handlers(R ((s//usr/lib/fence-agents/bundled/botocore/hooks.pyt__init__s  cCsg}|jj|}|dkrF|jj|}||j|>> 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 RR((s//usr/lib/fence-agents/bundled/botocore/hooks.pyRs cKs.|j||dt}|r&|dSdSdS(s 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``). R9iN(NN(R;tTrueR(R RRR((s//usr/lib/fence-agents/bundled/botocore/hooks.pytemit_until_responses cCs |j||||dtdS(Ntsection(t_register_sectiont_MIDDLE(R RRR R((s//usr/lib/fence-agents/bundled/botocore/hooks.pyRscCs |j||||dtdS(NR>(R?t_FIRST(R RRR R((s//usr/lib/fence-agents/bundled/botocore/hooks.pyR"scCs |j||||dtdS(NR>(R?t_LAST(R RRR R((s//usr/lib/fence-agents/bundled/botocore/hooks.pyR$scCs|dk r||jkr|j|jdd}|rm|sStd|q|j|dcd7R(RR2R4R*R1t append_itemR/(R RRR RR>RCtunique_id_handler_item((s//usr/lib/fence-agents/bundled/botocore/hooks.pyR?s,     cCs|dk ry|j|jdd}Wntk r=dSX|r|dkrctd|q|dkr|jj|d}q|j|dcd8 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. cCs"idd6id6dd6|_dS(Ntchunktchildrentvalues(Rt_root(R ((s//usr/lib/fence-agents/bundled/botocore/hooks.pyR3scCs|jd}|j}x^|D]V}||dkrgi|d6dd6id6}||d|<|}q|d|}qW|ddkrtggg|dt key_partstcurrenttpartt new_child((s//usr/lib/fence-agents/bundled/botocore/hooks.pyRDs   cCs;t}|jd}|j}|j|||d|S(sCollect 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"). RUi(RRWRgt _get_items(R Rht collectedRjRk((s//usr/lib/fence-agents/bundled/botocore/hooks.pyR5s   cCs||fg}t|}x|r|j\}}|drw|d} | j| j| j} |jt| n||ks|d} | j||} | jd} |d}| dk r|j | |fn| dk r|j | |fqqqWdS(NRfRet*i( RaRGRRRt extendlefttreversedR4RR8(R t starting_nodeRjRotstarting_indextstackt key_parts_lent current_nodeRXt node_listtcomplete_orderRetdirectstwildcardt next_index((s//usr/lib/fence-agents/bundled/botocore/hooks.pyRns"         cCs5|jd}|j}|j|||dddS(sRemove 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. RURXiN(RWRgt _remove_item(R RhRiRjRk((s//usr/lib/fence-agents/bundled/botocore/hooks.pyRHs cCs3|dkrdS|t|kr/|dj||}|dk r|j||||d|t|dkr|d}||jkr|jj|q||jkr|jj|q||jkr|jj|qn|d r,|d r,|d||=q,q/tddj |ndS(NReiRfskey is not in trie: %sRU( RRaR4R}RtremoveRRR*RZ(R RwRjRiRXt next_nodeRx((s//usr/lib/fence-agents/bundled/botocore/hooks.pyR}(s$   cCs+|j}|j|j}||_|S(N(RIt_recursive_copyRJ(R tnew_copyt copied_attrs((s//usr/lib/fence-agents/bundled/botocore/hooks.pyRAs  cCs{i}xn|jD]`\}}t|trDtj||| s   db