
    $gK#                         d dl mZmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ  edd          Zd Z G d	 d
e          Zd ZddZd Zd Zd ZdS )    )gatherensure_futureget_event_loopiscoroutineiscoroutinefunction)
namedtuple)Iterable)partial)ListLoaderz
key,futurec                 X    t          t          | t                    r| j        n|           S N)r   
isinstancer
   func)fns    Y/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/graphene/utils/dataloader.pyiscoroutinefunctionorpartialr      s%    *R*A*AIrwwrJJJ    c                   l    e Zd ZdZdZdZ	 	 	 	 	 	 	 ddZed             ZddZ	d Z
d Zd Zd	 Zd
 ZdS )
DataLoaderTNc                 l   || _         ||| _        t          | j                  s"J d                    | j                              t	          | j                  s"t          d                    |                    ||| _        ||| _        ||| _        |pd | _	        ||ni | _
        g | _        d S )Nz-batch_load_fn must be coroutine. Received: {}zuDataLoader must be have a batch_load_fn which accepts Iterable<key> and returns Future<Iterable<value>>, but got: {}.c                     | S r    )xs    r   <lambda>z%DataLoader.__init__.<locals>.<lambda>?   s     r   )_loopbatch_load_fnr   formatcallable	TypeErrorbatchmax_batch_sizecacheget_cache_key_cache_queue)selfr   r!   r"   r#   r$   	cache_maploops           r   __init__zDataLoader.__init__   s     
$!.D+
 
 	V 	V:AA$BTUU	V 	V 	V *++ 	V&''	   DJ%"0DDJ*;{{#,#8iibr   c                 D    | j         st                      | _         | j         S r   )r   r   r'   s    r   r)   zDataLoader.loopD   s!    z 	*'))DJzr   c                 B   |"t          d                    |                    |                     |          }| j        r| j                            |          }|r|S | j                                        }| j        r
|| j        |<   |                     ||           |S )zZ
        Loads a key, returning a `Future` for the value represented by that key.
        NzDThe loader.load() function must be called with a value, but got: {}.)	r    r   r$   r#   r%   getr)   create_futuredo_resolve_reject)r'   key	cache_keycached_resultfutures        r   loadzDataLoader.loadK   s     ;#&++	   &&s++	 : 	% KOOI66M %$$ ((**: 	,%+DK	"sF+++r   c                     | j                             t          ||                     t          | j                   dk    r/| j        rt          | j        |            d S t          |            d S d S )N)r1   r4      )r&   appendr   lenr!   enqueue_post_future_jobr)   dispatch_queue)r'   r1   r4   s      r   r0   zDataLoader.do_resolve_rejecth   s|    6c&999::: t{q  z %'	488888 t$$$$$ ! r   c                      t          |t                    s"t          d                    |                    t	           fd|D              S )a%  
        Loads multiple keys, returning a list of values

        >>> a, b = await my_loader.load_many([ 'a', 'b' ])

        This is equivalent to the more verbose:

        >>> a, b = await gather(
        >>>    my_loader.load('a'),
        >>>    my_loader.load('b')
        >>> )
        zNThe loader.load_many() function must be called with Iterable<key> but got: {}.c                 :    g | ]}                     |          S r   )r5   ).0r1   r'   s     r   
<listcomp>z(DataLoader.load_many.<locals>.<listcomp>   s#    7773		#777r   )r   r	   r    r   r   )r'   keyss   ` r   	load_manyzDataLoader.load_manyv   s[     $)) 	#&,,	   7777$77788r   c                 f    |                      |          }| j                            |d           | S )zu
        Clears the value at `key` from the cache, if it exists. Returns itself for
        method chaining.
        N)r$   r%   pop)r'   r1   r2   s      r   clearzDataLoader.clear   s2    
 &&s++		4(((r   c                 8    | j                                          | S )z
        Clears the entire cache. To be used when some event results in unknown
        invalidations across this particular `DataLoader`. Returns itself for
        method chaining.
        )r%   rD   r,   s    r   	clear_allzDataLoader.clear_all   s     	r   c                    |                      |          }|| j        vrc| j                                        }t	          |t
                    r|                    |           n|                    |           || j        |<   | S )z
        Adds the provied key and value to the cache. If the key already exists, no
        change is made. Returns itself for method chaining.
        )r$   r%   r)   r/   r   	Exceptionset_exception
set_result)r'   r1   valuer2   r4   s        r   primezDataLoader.prime   s    
 &&s++	 DK'' Y,,..F%++ )$$U++++!!%(((%+DK	"r   )NNNNNNNr   )__name__
__module____qualname__r!   r"   r#   r*   propertyr)   r5   r0   rA   rD   rF   rL   r   r   r   r   r      s        ENE ( ( ( (T   X   :% % %9 9 9.        r   r   c                 X    fd}|                      t           |                       d S )Nc                  *   K   t                      d S r   )r;   )loaders   r   dispatchz)enqueue_post_future_job.<locals>.dispatch   s      vr   )	call_soonr   )r)   rS   rT   s    ` r   r:   r:      s>         	NN=((**-----r   r7   c                 |     t          d           fdt          dt                               D             S )Nr7   c              3   2   K   | ]}||z            V  d S r   r   )r>   i
chunk_sizeiterable_objs     r   	<genexpr>zget_chunks.<locals>.<genexpr>   sE         	QZ'(     r   r   )maxranger9   )rZ   rY   s   ``r   
get_chunksr^      sW    Q
##J    q#l++Z88   r   c                     | j         }g | _         | j        }|rG|t          |          k     r4t          ||          }|D ]}t	          t          | |                      dS t	          t          | |                     dS )zh
    Given the current state of a Loader instance, perform a batch load
    from its current queue.
    N)r&   r"   r9   r^   r   dispatch_queue_batch)rS   queuer"   chunkschunks        r   r;   r;      s     MEFM *N ;.3u::55E>22 	? 	?E.vu==>>>>	? 	? 	*6599:::::r   c           	        K   d |D             }|                      |          }|rt          |          s1t          | |t          d                    |                              S 	 | d {V }t          |t                    s"t          d                    |                    t          |          }t          |          t          |          k    r#t          d                    ||                    t          ||          D ]O\  }}t          |t                    r|j                            |           5|j                            |           Pd S # t          $ r}t          | ||          cY d }~S d }~ww xY w)Nc                     g | ]	}|j         
S r   )r1   )r>   loadeds     r   r?   z(dispatch_queue_batch.<locals>.<listcomp>   s    +++6FJ+++r   zDataLoader must be constructed with a function which accepts Iterable<key> and returns Future<Iterable<value>>, but the function did not return a Coroutine: {}.zDataLoader must be constructed with a function which accepts Iterable<key> and returns Future<Iterable<value>>, but the function did not return a Future of a Iterable: {}.zDataLoader must be constructed with a function which accepts Iterable<key> and returns Future<Iterable<value>>, but the function did not return a Future of a Iterable with the same length as the Iterable of keys.

Keys:
{}

Values:
{})r   r   failed_dispatchr    r   r   r	   listr9   ziprH   r4   rI   rJ   )rS   ra   r@   batch_futurevaluesrf   rK   es           r   r`   r`      s     ++U+++D ''--L  
{<88 
2 &&& 

 

 
	
!1#######&(++ 	= &..   fv;;#d))##& &v&&	 	 	 !// 	0 	0MFE%++ 0++E2222((////		0 	0  1 1 1vua000000001s   'C1E 
E?$E:4E?:E?c                 x    |D ]6}|                      |j                   |j                            |           7dS )z
    Do not cache individual loads if the entire batch dispatch fails,
    but still reject each request so they do not hang.
    N)rD   r1   r4   rI   )rS   ra   errorrf   s       r   rg   rg     sJ    
  + +VZ   ##E****+ +r   N)r7   )asyncior   r   r   r   r   collectionsr   collections.abcr	   	functoolsr
   typingr   r   r   objectr   r:   r^   r;   r`   rg   r   r   r   <module>ru      sF                # " " " " " $ $ $ $ $ $            	Hl	+	+K K K] ] ] ] ] ] ] ]@. . .   ; ; ;*61 61 61r+ + + + +r   