a
    O/                     @   s  d Z dZdZdZddlZddlZzddlZW n eyF   ddlZY n0 zddl	Z
W n eyn   ddl
Z
Y n0 G dd dejZedZee  i Zd	d
 ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdS )z,
QR | Redis-Based Data Structures in Python
z	Ted Nymanz0.6.0MIT    Nc                   @   s   e Zd ZdZdd ZdS )NullHandlerz3A logging handler that discards all logging recordsc                 C   s   d S N )selfrecordr   r   E/var/www/dev/btcams2022/python_venv/lib/python3.9/site-packages/qr.pyemit   s    zNullHandler.emitN)__name__
__module____qualname____doc__r	   r   r   r   r   r      s   r   qrc                  K   sf   d dd |  D }ztjt| dW S  ty`   tjf i | }|t|< tj|d Y S 0 dS )a0  
    Match up the provided kwargs with an existing connection pool.
    In cases where you may want a lot of queues, the redis library will
    by default open at least one connection for each. This uses redis'
    connection pool mechanism to keep the number of open file descriptors
    tractable.
    :c                 s   s&   | ]\}}t |d  t | V  qdS )z=>N)repr).0keyvaluer   r   r   	<genexpr>/       zgetRedis.<locals>.<genexpr>)Zconnection_poolN)joinitemsredisZRedisconnectionPoolsKeyErrorZConnectionPool)kwargsr   Zcpr   r   r   getRedis'   s    r   c                   @   s   e Zd ZdddZdd ZdS )workerNc                 O   s   || _ || _|| _|| _d S r   )qerrargsr   )r   r   r   r    r   r   r   r   __init__8   s    zworker.__init__c                    s    fdd}|S )Nc               
      s   j jdd} | sq z | gjR i j W q  ty } z6zj|gtR i t W n   Y n0 W Y d }~q d }~0 0 q d S )NT)block)r   popr    r   	Exceptionr   )nextefr   r   r   wrapped?   s    z worker.__call__.<locals>.wrappedr   )r   r(   r)   r   r'   r   __call__>   s    zworker.__call__)N)r
   r   r   r!   r*   r   r   r   r   r   7   s   
r   c                   @   s   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd Zd"ddZdd Zdd Zdd Zdd Zdd Zdd  Zd!S )#	BaseQueuez#Base functionality common to queuesc                    s(   t f i  } fdd||D S )Nc                    s   g | ]}|fi  qS r   r   )r   kr   tr   r   
<listcomp>W   r   z!BaseQueue.all.<locals>.<listcomp>)r   keys)r.   patternr   rr   r-   r   allT   s    zBaseQueue.allc                 K   s    t | _tf i || _|| _d S r   )pickle
serializerr   r   r   )r   r   r   r   r   r   r!   Y   s    zBaseQueue.__init__c                 C   s   | j | jS zReturn the length of the queue)r   Zllenr   r   r   r   r   __len__^   s    zBaseQueue.__len__c              
      s   z* fdd j  j|j|jd D W S  tyR     j  j| Y S  ty } z t	
dt|  W Y d}~dS d}~0 0 dS )"Get a slice or a particular index.c                    s   g | ]}  |qS r   _unpackr   ir7   r   r   r/   e   r   z)BaseQueue.__getitem__.<locals>.<listcomp>   Get item failed ** %sN)r   lranger   startstopAttributeErrorr;   Zlindexr$   logerrorr   r   valr&   r   r7   r   __getitem__b   s    *zBaseQueue.__getitem__c                 C   s   | j |dS )z#Prepares a message to go into Redisr>   )r5   dumpsr   rG   r   r   r   _packl   s    zBaseQueue._packc                 C   s(   z| j |W S  ty"   Y dS 0 dS )z!Unpacks a message stored in RedisN)r5   loads	TypeErrorrJ   r   r   r   r;   p   s    zBaseQueue._unpackc                 C   s0   | j | j}|r,|| | j | j}qdS )4Destructively dump the contents of the queue into fpN)r   rpopr   writer   fobjr%   r   r   r   dumpw   s    
zBaseQueue.dumpc                 C   s8   z$| j | j| | j| qW n   Y dS 0 dS z5Load the contents of the provided fobj into the queueN)r   lpushr   rK   r5   load)r   rR   r   r   r   rV   ~   s    $zBaseQueue.loadFc                 C   sr   |r:t |d}| | W d   qn1 s.0    Y  n4t |d}| | W d   n1 sd0    Y  dS z7Destructively dump the contents of the queue into fnamezw+Nza+filerS   r   fnametruncater(   r   r   r   	dumpfname   s
    *zBaseQueue.dumpfnamec                 C   s6   t |}| | W d   n1 s(0    Y  dS z9Load the contents of the contents of fname into the queueNrY   rV   r   r[   r(   r   r   r   	loadfname   s    
zBaseQueue.loadfnamec                 C   sX   | j jdd6}|D ]}|| j| | q|  W d   n1 sJ0    Y  dS "Extends the elements in the queue.F)ZtransactionN)r   pipelinerU   r   rK   executer   valspiperG   r   r   r   extend   s    zBaseQueue.extendc                 C   s   | d S )"Look at the next item in the queuer   r7   r   r   r   peek   s    zBaseQueue.peekc                    s     fdd j  jddD S )$Return all elements as a Python listc                    s   g | ]}  |qS r   r:   r   or7   r   r   r/      r   z&BaseQueue.elements.<locals>.<listcomp>r   rk   )r   r@   r   r7   r   r7   r   elements   s    zBaseQueue.elementsc                 C   s   t | jS )z"Return all elements as JSON object)jsonrI   rp   r7   r   r   r   elements_as_json   s    zBaseQueue.elements_as_jsonc                 C   s   | j | j dS )z%Removes all the elements in the queueN)r   deleter   r7   r   r   r   clear   s    zBaseQueue.clearN)F)r
   r   r   r   staticmethodr3   r!   r8   rH   rK   r;   rS   rV   r]   ra   ri   rl   rp   rr   rt   r   r   r   r   r+   R   s"   


	r+   c                   @   s>   e Zd ZdZedddZdd Zdd Zd	d
 Zdd Z	dS )DequezImplements a double-ended queue*c                 K   s   t jt| fi |S r   )r+   r3   rv   r1   r   r   r   r   r3      s    z	Deque.allc                 C   s.   | j | j| | td|| jf  dS )z(Push an element to the back of the deque Pushed ** %s ** for key ** %s **Nr   rU   r   rK   rD   debugr   elementr   r   r   	push_back   s    zDeque.push_backc                 C   s2   | j }| j|| |}td|| j f  dS )z)Push an element to the front of the dequery   N)r   r   ZrpushrK   rD   r{   )r   r}   r   Zpush_itr   r   r   
push_front   s    zDeque.push_frontc                 C   s,   | j | j}td|| jf  | |S )z*Pop an element from the front of the deque!Popped ** %s ** from key ** %s **)r   rO   r   rD   r{   r;   r   poppedr   r   r   	pop_front   s    zDeque.pop_frontc                 C   s,   | j | j}td|| jf  | |S )z)Pop an element from the back of the dequer   )r   lpopr   rD   r{   r;   r   r   r   r   pop_back   s    zDeque.pop_backN)rw   )
r
   r   r   r   ru   r3   r~   r   r   r   r   r   r   r   rv      s   rv   c                   @   s0   e Zd ZdZedddZdd Zddd	Zd
S )QueuezImplements a FIFO queuerw   c                 K   s   t jt| fi |S r   )r+   r3   r   rx   r   r   r   r3      s    z	Queue.allc                 C   s.   | j | j| | td|| jf  dS zPush an elementry   Nrz   r|   r   r   r   push   s    z
Queue.pushFc                 C   sD   |s| j | j}n| j | j\}}td|| jf  | |S zPop an elementr   r   rO   r   ZbrpoprD   r{   r;   r   r"   r   queuer   r   r   r#      s
    z	Queue.popN)rw   )Fr
   r   r   r   ru   r3   r   r#   r   r   r   r   r      s
   r   c                   @   sn   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZdd Z	dd Z
dddZdd ZdddZdd ZdS )PriorityQueuezA priority queuec                 C   s   | j | jS r6   )r   Zzcardr   r7   r   r   r   r8      s    zPriorityQueue.__len__c              
      s   z* fdd j  j|j|jd D W S  tyf    j  j||}|r` |d  Y S Y dS  ty } z t	dt
|  W Y d}~dS d}~0 0 dS )r9   c                    s   g | ]}  |qS r   r:   r<   r7   r   r   r/      r   z-PriorityQueue.__getitem__.<locals>.<listcomp>r>   r   Nr?   )r   zranger   rA   rB   rC   r;   r$   rD   rE   r   rF   r   r7   r   rH      s    *zPriorityQueue.__getitem__c                 C   s,   |   }|r(| j|d | |   }qdS )rN   r   N)r#   r5   rS   rQ   r   r   r   rS      s    zPriorityQueue.dumpc              
   C   sT   z(| j |\}}| j| j|| qW n& tyN } zW Y d}~dS d}~0 0 dS rT   )r5   rV   r   zaddr   r$   )r   rR   r   scorer&   r   r   r   rV      s
    zPriorityQueue.loadFc                 C   sr   |r:t |d}| | W d   qn1 s.0    Y  n4t |d}| | W d   n1 sd0    Y  dS rW   rX   rZ   r   r   r   r]     s
    *zPriorityQueue.dumpfnamec                 C   s6   t |}| | W d   n1 s(0    Y  dS r^   r_   r`   r   r   r   ra   
  s    
zPriorityQueue.loadfnamec                 C   s^   | j jdd<}|D ]\}}|| j| || q| W  d   S 1 sP0    Y  dS rb   )r   rd   r   r   rK   re   )r   rg   rh   rG   r   r   r   r   ri     s    zPriorityQueue.extendc                 C   sL   | j j| jdddd}|r@|d \}}| |}|r<||fS |S |rHdS dS )rj   r   T
withscoresNg        N)r   r   r   r;   )r   r   rG   r   r   r   r   r   rl     s    
zPriorityQueue.peekc                    s     fdd j  jddD S )rm   c                    s   g | ]}  |qS r   r:   rn   r7   r   r   r/   %  r   z*PriorityQueue.elements.<locals>.<listcomp>r   rk   )r   r   r   r7   r   r7   r   rp   #  s    zPriorityQueue.elementsc                 C   s   | j  }|j| jdddd}|| jdd}| \}}|r|d \}}| |}|rp||fW  d   S |W  d   S |rW d   dS W d   dS 1 s0    Y  dS )z5Get the element with the lowest score, and pop it offr   Tr   Nr   )r   rd   r   r   Zzremrangebyrankre   r;   )r   r   rh   ro   resultscountr   r   r   r   r   r#   '  s    
zPriorityQueue.popc                 C   s   | j | j| ||S )z!Add an element with a given score)r   r   r   rK   )r   r   r   r   r   r   r   7  s    zPriorityQueue.pushN)F)F)F)r
   r   r   r   r8   rH   rS   rV   r]   ra   ri   rl   rp   r#   r   r   r   r   r   r      s   	
	

r   c                   @   s@   e Zd ZdZedddZdd Zdd Zd	d
 ZdddZ	dS )CappedCollectionzh
    Implements a capped collection (the collection never
    gets larger than the specified size).
    rw   c                 K   s   t jt| fi |S r   )r+   r3   r   rx   r   r   r   r3   A  s    zCappedCollection.allc                 K   s   t j| |fi | || _d S r   )r+   r!   size)r   r   r   r   r   r   r   r!   E  s    zCappedCollection.__init__c                 C   s`   | j }| j <}|| j| || jd|d }|  W d    n1 sR0    Y  d S )Nr   r>   )r   r   rd   rU   r   rK   ltrimre   )r   r}   r   rh   r   r   r   r   I  s    $zCappedCollection.pushc                 C   sj   | j  L}|D ]}|| j| | q|| jd| jd  |  W d   n1 s\0    Y  dS )rc   r   r>   N)r   rd   rU   r   rK   r   r   re   rf   r   r   r   ri   P  s
    zCappedCollection.extendFc                 C   sD   |s| j | j}n| j | j\}}td|| jf  | |S )Nr   r   r   r   r   r   r#   X  s
    zCappedCollection.popN)rw   )F)
r
   r   r   r   ru   r3   r!   r   ri   r#   r   r   r   r   r   ;  s   r   c                   @   s0   e Zd ZdZedddZdd Zddd	Zd
S )StackzImplements a LIFO stackrw   c                 K   s   t jt| fi |S r   )r+   r3   r   rx   r   r   r   r3   c  s    z	Stack.allc                 C   s.   | j | j| | td|| jf  dS r   rz   r|   r   r   r   r   g  s    z
Stack.pushFc                 C   sD   |s| j | j}n| j | j\}}td|| jf  | |S r   )r   r   r   ZblpoprD   r{   r;   r   r   r   r   r#   l  s
    z	Stack.popN)rw   )Fr   r   r   r   r   r   `  s
   r   )r   
__author____version____license__r   loggingrq   ImportErrorZ
simplejsoncPickler4   Handlerr   	getLoggerrD   
addHandlerr   r   objectr   r+   rv   r   r   r   r   r   r   r   r   <module>   s2   
Y]%