
    )Jf9                         d Z ddlZddlZddlmZ ddlmZ ddlmZmZ  ej	        e
          Zd Zd Zd Z G d	 d
e          ZdS )z^Callback management class, common area for keeping track of all callbacks in
the Pika stack.

    N)frame)amqp_object)xrangecanonical_strc                    	 t          | t          j                  r| j        S n# t          $ r Y nw xY wt          | t          j                  r| j        j        S t          | t          j                  r| j        S t          |           S )zWill take Frame objects, classes, etc and attempt to return a valid
    string identifier for them.

    :param pika.amqp_object.AMQPObject|pika.frame.Frame|int|str value: The
        value to sanitize
    :rtype: str

    )

issubclassr   
AMQPObjectNAME	TypeError
isinstancer   Methodmethodr   )values    P/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/pika/callback.pyname_or_valuer      s    e[344 	:	    %&& !|   %/00 z s    $ 
11c                 F     t          j                    fd            }|S )z9Automatically call name_or_value on the prefix passed in.c                     t          |           } d}d|v rt          |d                   |d<   n3t          |           dz
  |k    rt          | |                   | |<   |dz  }d|v rt          |d                   |d<   n.t          |           dz
  |k    rt          | |                   | |<    t          |           i |S )N   prefixkey)listr   lentuple)argskwargsoffsetfunctions      r   wrapperz sanitize_prefix.<locals>.wrapper.   s    Dzzv,VH-=>>F8YY]f$$(f66DLaKFF??)&-88F5MMYY]f$$(f66DLxt////    	functoolswrapsr   r   s   ` r   sanitize_prefixr$   +   s:     _X0 0 0 0 0 Nr   c                 F     t          j                    fd            }|S )zcAutomatically return false if the key or prefix is not in the callbacks
    for the instance.

    c                  *   d}d|v rt          |d                   }nt          | |                   }|dz  }d|v rt          |d                   }nt          | |                   }|| d         j        vs|| d         j        |         vrdS  | i |S )Nr   r   r   r   F)r   _stack)r   r   r   r   r   r   s        r   r   z)check_for_prefix_and_key.<locals>.wrapperG   s    v"6(#344FF"4<00FaKF F??u..CCV--C a''3d1gnV6L+L+L5 x((((r   r    r#   s   ` r   check_for_prefix_and_keyr(   A   s:     _X) ) ) ) ), Nr   c                   @   e Zd ZdZdZdZdZdZdZdZ	d Z
e	 	 	 dd            Zd Zed             Zed             Zeed                         Zeedd                        Zeed                         Zd Zd ZddZed             Zed             Zd Zd Zd
S )CallbackManagerzCallbackManager is a global callback system designed to be a single place
    where Pika can manage callbacks and process them. It should be referenced
    by the CallbackManager.instance() method instead of constructing new
    instances of it.

    calls	argumentsz$Duplicate callback found for "%s:%s"callbackone_shotonlyc                 ,    t                      | _        dS )z)Create an instance of the CallbackManagerN)dictr'   selfs    r   __init__zCallbackManager.__init__o   s    ffr   TNc                    || j         vrt                      | j         |<   || j         |         vrt                      | j         |         |<   | j         |         |         D ]}|| j                 |k    r|| j                 |k    rx|| j                 |k    rg|| j                 du r1|| j        xx         dz  cc<   t          	                    d|           n!t          
                    | j        ||           ||fc S |                     ||||          }| j         |         |                             |           t          	                    d|           ||fS )a  Add a callback to the stack for the specified key. If the call is
        specified as one_shot, it will be removed after being fired

        The prefix is usually the channel number but the class is generic
        and prefix and key may be any value. If you pass in only_caller
        CallbackManager will restrict processing of the callback to only
        the calling function/object that you specify.

        :param str|int prefix: Categorize the callback
        :param str|dict key: The key for the callback
        :param callable callback: The callback to call
        :param bool one_shot: Remove this callback after it is called
        :param object only_caller: Only allow one_caller value to call the
                                   event that fires the callback.
        :param dict arguments: Arguments to validate when processing
        :rtype: tuple(prefix, key)

        Tr   z*Incremented callback reference counter: %rz	Added: %r)r'   r1   r   CALLBACK	ARGUMENTSONLY_CALLERONE_SHOTCALLSLOGGERdebugwarningDUPLICATE_WARNING_callback_dictappend)r3   r   r   r-   r.   only_callerr,   callback_dicts           r   addzCallbackManager.adds   sw   6 $$"&&&DKdk&)))'+vvDK$ "[05 
	# 
	#Mdm,88!$.1Y>>!$"23{BB /477!$*---2---LL!M!.0 0 0 0 NN4#963GGGs{""" ++Hh,57 7FC ''666[-000s{r   c                 `    t                      | _        t                              d           dS )z1Clear all the callbacks if there are any defined.zCallbacks clearedN)r1   r'   r;   r<   r2   s    r   clearzCallbackManager.clear   s&    ff()))))r   c                 |    t                               d|           || j        vs| j        |         sdS | j        |= dS )zRemove all callbacks from the stack by a prefix. Returns True
        if keys were there to be removed

        :param str or int prefix: The prefix for keeping track of callbacks with
        :rtype: bool

        zClearing out %r from the stackFT)r;   r<   r'   )r3   r   s     r   cleanupzCallbackManager.cleanup   sF     	5v>>>$$DK,?$5Ktr   c                 v    || j         vs|| j         |         vrdS t          | j         |         |                   S )zReturn count of callbacks for a given prefix or key or None

        :param str|int prefix: Categorize the callback
        :param object|str|dict key: The key for the callback
        :rtype: None or int

        N)r'   r   r3   r   r   s      r   pendingzCallbackManager.pending   sA     $$C4;v3F,F,F44;v&s+,,,r   c                 @   t                               d||           || j        vs|| j        |         vrdS t                      }t          | j        |         |                   D ]j}|                     ||t          |                    rD|                    || j                            || j                 r|                     |||           k|D ]K}t                               d|||           	  ||i | )#  t           	                    d|||            xY wdS )a  Run through and process all the callbacks for the specified keys.
        Caller should be specified at all times so that callbacks which
        require a specific function to call CallbackManager.process will
        not be processed.

        :param str|int prefix: Categorize the callback
        :param object|str|int key: The key for the callback
        :param object caller: Who is firing the event
        :param list args: Any optional arguments
        :param dict keywords: Optional keyword arguments
        :rtype: bool

        zProcessing %s:%sFzCalling %s for "%s:%s"zCalling %s for "%s:%s" failedT)
r;   r<   r'   r   _should_process_callbackr@   r6   r9   _use_one_shot_callback	exception)	r3   r   r   callerr   keywords	callbacksrB   r-   s	            r   processzCallbackManager.process   sK     	'555$$4;v3F(F(F5FF	!$+f"5c":;; 	L 	LM,,]FDJJOO L  t}!=>>> / L//]KKK " 	 	HLL18VSIII$+(++++  !@(!'. . .ts   2C;; Dc                 (   |rt                      }t          t          | j        |         |                   dd          D ]^}| j        |         |         |dz
           }|| j                 |k    r/|                     ||g          r|                    |dz
             _|D ]Z}	 t                              d|| j        |         |         |                    | j        |         |         |= K# t          $ r Y Ww xY w| 
                    ||           dS )a  Remove a callback from the stack by prefix, key and optionally
        the callback itself. If you only pass in prefix and key, all
        callbacks for that prefix and key will be removed.

        :param str or int prefix: The prefix for keeping track of callbacks with
        :param str key: The callback key
        :param callable callback_value: The method defined to call on callback
        :param dict arguments: Optional arguments to check
        :rtype: bool

        r   r   zRemoving callback #%i: %rT)r   r   r   r'   r6   _arguments_matchr@   r;   r<   KeyError_cleanup_callback_dict)r3   r   r   callback_valuer,   offsets_to_remover   rB   s           r   removezCallbackManager.remove   s?     	 $ T[%8%=!>!>2FF 9 9 $F 3C 8! D!$-0NBB--mi[II C%,,VaZ888+  LL!<f!%V!4S!9&!AC C CF+C088   D 	##FC000ts   $AC,,
C98C9c                 N    | j         |         |= |                     ||           dS )zRemove all callbacks for the specified prefix and key.

        :param str prefix: The prefix for keeping track of callbacks with
        :param str key: The callback key

        N)r'   rW   rI   s      r   
remove_allzCallbackManager.remove_all  s/     K$##FC00000r   c                 @   || j                  dS |sdS t          |d         t                    r'|                     |d         || j                            S |                     t          |d         d          r|d         j        n|d         || j                            S )a\  Validate if the arguments passed in match the expected arguments in
        the callback_dict. We expect this to be a frame passed in to *args for
        process or passed in as a list from remove.

        :param dict callback_dict: The callback dictionary to evaluate against
        :param list args: The arguments passed in as a list

        NTFr   r   )r7   r   r1   _dict_arguments_match_obj_arguments_matchhasattrr   )r3   rB   r   s      r   rU   z CallbackManager._arguments_match  s     (04 	5d1gt$$ 	M--d1g.;DN.KM M M((%d1gx88EDGNNd1g$.)+ + 	+r   c                 Z    | j         || j        || j        || j        |i}|r
d|| j        <   |S )aI  Return the callback dictionary.

        :param callable callback: The callback to call
        :param bool one_shot: Remove this callback after it is called
        :param object only_caller: Only allow one_caller value to call the
                                   event that fires the callback.
        :rtype: dict

        r   )r6   r9   r8   r7   r:   )r3   r-   r.   rA   r,   r   s         r   r?   zCallbackManager._callback_dict2  sC     M8M8kNI	
  	" !E$*r   c                     |r0|| j         |         v r!| j         |         |         s| j         |         |= || j         v r| j         |         s| j         |= dS dS dS )zRemove empty dict nodes in the callback stack.

        :param str or int prefix: The prefix for keeping track of callbacks with
        :param str key: The callback key

        N)r'   rI   s      r   rW   z&CallbackManager._cleanup_callback_dictF  st      	)3$+f---dk&6I#6N-F#C(T[  V)< F### !   r   c                     t                               d| |           |D ]?}|                     |          ||         k    rt                               d|            dS @dS )zChecks an dict to see if it has attributes that meet the expectation.

        :param dict value: The dict to evaluate
        :param dict expectation: The values to check against
        :rtype: bool

        zComparing %r to %rz"Values in dict do not match for %sFT)r;   r<   getr   expectationr   s      r   r^   z%CallbackManager._dict_arguments_matchR  sl     	)5+>>> 	 	Cyy~~S!111A3GGGuu 2 tr   c                    |D ]}t          | |          s,t                              dt          |           |            dS t	          | |          ||         k    r,t                              dt          |           |            dS dS )zChecks an object to see if it has attributes that meet the
        expectation.

        :param object value: The object to evaluate
        :param dict expectation: The values to check against
        :rtype: bool

        z'%r does not have required attribute: %sFz Values in %s do not match for %sT)r`   r;   r<   typegetattrre   s      r   r_   z$CallbackManager._obj_arguments_matchb  s      	 	C5#&& F!%[[#/ / /uuuc""k#&666?e " " "uu 7 tr   c                     |                      ||          st                              d||           dS || j                 du p|| j                 o|| j                 |k    S )zReturns True if the callback should be processed.

        :param dict callback_dict: The callback configuration
        :param object caller: Who is firing the event
        :param list args: Any optional arguments
        :rtype: bool

        z!Arguments do not match for %r, %rFN)rU   r;   r<   r8   )r3   rB   rO   r   s       r   rL   z(CallbackManager._should_process_callbackw  sy     $$]D99 	LL<m  5d./47 <t/0 ;t/0F:	=r   c                 2   t                               d           || j        xx         dz  cc<   t                               d|| j                            || j                 dk    r0|                     |||| j                 || j                            dS dS )aD  Process the one-shot callback, decrementing the use counter and
        removing it from the stack if it's now been fully used.

        :param str or int prefix: The prefix for keeping track of callbacks with
        :param str key: The callback key
        :param dict callback_dict: The callback dict to process

        z"Processing use of oneshot callbackr   z%i registered uses leftr   N)r;   r<   r:   rZ   r6   r7   )r3   r   r   rB   s       r   rM   z&CallbackManager._use_one_shot_callback  s     	9:::dj!!!Q&!!!.dj0IJJJ$))KK]4=%A%dn57 7 7 7 7 *)r   )TNN)NN)N)__name__
__module____qualname____doc__r:   r7   r>   r6   r9   r8   r4   r$   rC   rE   rG   rJ   r(   rR   rZ   r\   rU   r?   rW   staticmethodr^   r_   rL   rM    r   r   r*   r*   a   s         EI>HHK   
 2 2 2 _2h* * *
   _ 
- 
- _
- # #  _#J     _@ 1 1  _1+ + +(  (
$ 
$ 
$ 
$   \   \(= = ="7 7 7 7 7r   r*   )ro   r!   loggingpikar   r   pika.compatr   r   	getLoggerrl   r;   r   r$   r(   objectr*   rq   r   r   <module>rw      s                      - - - - - - - -		8	$	$     8  ,  @v7 v7 v7 v7 v7f v7 v7 v7 v7 v7r   