
    aj2#                         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y)    )gatherensure_futureget_event_loopiscoroutineiscoroutinefunction)
namedtuple)Iterable)partial)ListLoaderz
key,futurec                 X    t        t        | t              r| j                        S |       S N)r   
isinstancer
   func)fns    f/var/www/html/myl_app/scheduler-service/venv/lib/python3.12/site-packages/graphene/utils/dataloader.pyiscoroutinefunctionorpartialr      s"    *R*ArwwJJrJJ    c                   d    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y)
DataLoaderTNc                 ^   || _         ||| _        t        | j                        s J dj                  | j                               t	        | j                        st        dj                  |            ||| _        ||| _        ||| _        |xs d | _	        ||ni | _
        g | _        y )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:AA$BTBTU	V 
 **+V&'	  DJ%"0DDJ*;{#,#8ib$&r   c                 P    | j                   st               | _         | j                   S r   )r   r   r'   s    r   r)   zDataLoader.loopC   s    zz')DJzzr   c                 D   |t        dj                  |            | j                  |      }| j                  r| j                  j                  |      }|r|S | j                  j                         }| j                  r|| j                  |<   | j                  ||       |S )zZ
        Loads a key, returning a `Future` for the value represented by that key.
        zDThe 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.loadJ   s     ;#&+	  &&s+	 :: KKOOI6M$$ ((*::%+DKK	"sF+r   c                     | j                   j                  t        ||             t        | j                         dk(  r/| j                  rt        | j                  |        y t        |        y y )N)r1   r4      )r&   appendr   lenr!   enqueue_post_future_jobr)   dispatch_queue)r'   r1   r4   s      r   r0   zDataLoader.do_resolve_rejectg   sR    6c&9: t{{q zz'		48 t$ !r   c                     t        |t              st        dj                  |            t	        |D cg c]  }| j                  |       c} S c c}w )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: {}.)r   r	   r    r   r   r5   )r'   keysr1   s      r   	load_manyzDataLoader.load_manyu   sL     $)#&,	  $73		#7887s   Ac                 `    | j                  |      }| j                  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   s+    
 &&s+		4(r   c                 :    | j                   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%   rA   r,   s    r   	clear_allzDataLoader.clear_all   s     	r   c                     | j                  |      }|| j                  vr\| j                  j                         }t	        |t
              r|j                  |       n|j                  |       || 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   so    
 &&s+	 DKK' YY,,.F%+$$U+!!%(%+DKK	"r   )NNNNNNNr   )__name__
__module____qualname__r!   r"   r#   r*   propertyr)   r5   r0   r>   rA   rC   rI   r   r   r   r   r      s`    ENE ''R  :%9.r   r   c                 F    fd}| j                  t         |              y )Nc                  $   K   t                y wr   )r;   )loaders   r   dispatchz)enqueue_post_future_job.<locals>.dispatch   s     vs   )	call_soonr   )r)   rP   rQ   s    ` r   r:   r:      s     	NN=(*-r   c                 ^     t        d       fdt        dt                     D        S )Nr7   c              3   .   K   | ]  }||z      y wr   r   ).0i
chunk_sizeiterable_objs     r   	<genexpr>zget_chunks.<locals>.<genexpr>   s$       	QZ(s   r   )maxranger9   )rX   rW   s   ``r   
get_chunksr\      s/    Q
#Jq#l+Z8 r   c                     | j                   }g | _         | j                  }|r7|t        |      k  r)t        ||      }|D ]  }t	        t        | |              yt	        t        | |             y)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)rP   queuer"   chunkschunks        r   r;   r;      sl     MMEFM **N.3u:5E>2 	?E.vu=>	? 	*659:r   c           	        K   |D cg c]  }|j                    }}| j                  |      }|rt        |      s%t        | |t	        dj                  |                  S 	 | d {   }t        |t              st	        dj                  |            t        |      }t        |      t        |      k7  rt	        dj                  ||            t        ||      D ]L  \  }}t        |t              r|j                  j                  |       2|j                  j                  |       N y c c}w 7 # t        $ r}t        | ||      cY d }~S d }~ww xY ww)NzDataLoader 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:
{})r1   r   r   failed_dispatchr    r   r   r	   listr9   ziprE   r4   rF   rG   )rP   r_   loadedr=   batch_futurevaluesrH   es           r   r^   r^      sK    %*+6FJJ+D+ ''-L {<82 &&

 
	
!1##&(+= &.  fv;#d)#& &v&	 	 !/ 	0MFE%+++E2((/		0[ ,( $>  1vua001sS   ED-AE D4 %D2&CD4 ,E2D4 4	E=E	E
EEEc                 ~    |D ]8  }| j                  |j                         |j                  j                  |       : y)z
    Do not cache individual loads if the entire batch dispatch fails,
    but still reject each request so they do not hang.
    N)rA   r1   r4   rF   )rP   r_   errorrf   s       r   rc   rc     s5    
  +VZZ ##E*+r   N)r7   )asyncior   r   r   r   r   collectionsr   collections.abcr	   	functoolsr
   typingr   r   r   objectr   r:   r\   r;   r^   rc   r   r   r   <module>rr      sX     # $  	Hl	+K\ \~.;*61r+r   