o
    ;h&                     @   st  d dl Z d dlmZ d dlmZmZmZ d dlmZm	Z	m
Z
mZmZ e jjdkr5e jjdkr5d dlmZ nd dlmZ dd	lmZmZmZmZmZmZmZmZmZmZmZmZmZ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)m*Z* dZ+dZ,dZ-dZ.eedZ/G dd deZ0G dd de0Z1G dd de0Z2ddgZ3G dd deZ4G dd deZ5G dd  d e2Z6dS )!    N)ABC)IncompleteReadErrorStreamReaderTimeoutError)CallableListOptionalProtocolUnion      )timeout   )AskErrorAuthenticationError$AuthenticationWrongNumberOfArgsErrorBusyLoadingErrorClusterCrossSlotErrorClusterDownErrorConnectionErrorExecAbortErrorMasterDownErrorModuleError
MovedErrorNoPermissionErrorNoScriptErrorOutOfMemoryErrorReadOnlyError
RedisErrorResponseErrorTryAgainError)
EncodableT   )Encoder)SERVER_CLOSED_CONNECTION_ERRORSocketBufferz:Error loading the extension. Please check the server logs.z5Error unloading module: no such module with that namez/Error unloading module: operation not possible.z[Error unloading module: the module exports one or more module-side data types, can't unload)zxAUTH <password> called without any password configured for the default user. Are you sure your configuration is correct?z(Client sent AUTH, but no password is setc                   @   sp   e Zd Zdedededeeeeee	ee
eieeeeeeeeeeeeeeedZedd Zdd	 Zd
d ZdS )
BaseParserzmax number of clients reachedzinvalid passwordz,wrong number of arguments for 'auth' commandz,wrong number of arguments for 'AUTH' command)ZERRZOOMZ	WRONGPASSZ	EXECABORTZLOADINGZNOSCRIPTZREADONLYZNOAUTHZNOPERMZASKZTRYAGAINMOVEDZCLUSTERDOWNZ	CROSSSLOTZ
MASTERDOWNc                 C   s\   | dd }|| jv r*|t|d d }| j| }t|tr&||t}||S t|S )zParse an error response r   r"   N)splitEXCEPTION_CLASSESlen
isinstancedictgetr   )clsresponseZ
error_codeZexception_class r1   F/var/www/html/venv/lib/python3.10/site-packages/redis/_parsers/base.pyparse_errorY   s   


zBaseParser.parse_errorc                 C      t  NNotImplementedErrorselfr1   r1   r2   on_disconnecte      zBaseParser.on_disconnectc                 C   r4   r5   r6   r9   
connectionr1   r1   r2   
on_connecth   r;   zBaseParser.on_connectN)__name__
__module____qualname__r   r   r   MODULE_LOAD_ERRORr   MODULE_EXPORTS_DATA_TYPES_ERRORNO_SUCH_MODULE_ERROR MODULE_UNLOAD_NOT_POSSIBLE_ERRORNO_AUTH_SET_ERRORr   r   r   r   r   r   r   r    r   r   r   r   r*   classmethodr3   r:   r>   r1   r1   r1   r2   r&   6   sB    "
r&   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )	_RESPBasez&Base class for sync-based resp parsingc                 C   s   || _ d | _d | _d | _d S r5   )socket_read_sizeencoder_sock_bufferr9   rI   r1   r1   r2   __init__o   s   
z_RESPBase.__init__c                 C   s$   z|    W d S  ty   Y d S w r5   )r:   	Exceptionr8   r1   r1   r2   __del__u   s
   z_RESPBase.__del__c                 C   s(   |j | _ t| j | j|j| _|j| _dS )zCalled when the socket connectsN)rK   r%   rI   socket_timeoutrL   rJ   r<   r1   r1   r2   r>   {   s
   z_RESPBase.on_connectc                 C   s*   d| _ | jdur| j  d| _d| _dS )z"Called when the socket disconnectsN)rK   rL   closerJ   r8   r1   r1   r2   r:      s
   


z_RESPBase.on_disconnectc                 C   s   | j o| j |S r5   )rL   can_read)r9   r   r1   r1   r2   rS      s   z_RESPBase.can_readN)	r?   r@   rA   __doc__rN   rP   r>   r:   rS   r1   r1   r1   r2   rH   l   s    rH   c                
   @   sV   e Zd ZdZdZdefddZdefddZ		dd
ede	e
edee
 f fddZdS )AsyncBaseParserz5Base parsing class for the python-backed async parser_stream
_read_sizerI   c                 C   s   d | _ || _d S r5   rV   rM   r1   r1   r2   rN      s   
zAsyncBaseParser.__init__returnc                       t  r5   r6   r8   r1   r1   r2   can_read_destructive   s   z$AsyncBaseParser.can_read_destructiveFdisable_decodingNc                    rZ   r5   r6   )r9   r\   r1   r1   r2   read_response   s   zAsyncBaseParser.read_response)F)r?   r@   rA   rT   	__slots__intrN   boolr[   r
   r!   r   r   r]   r1   r1   r1   r2   rU      s    rU   s
   invalidateZ
invalidatec                   @   J   e Zd ZU dZeed< dZee ed< dd Zdd Z	d	d
 Z
dd ZdS )PushNotificationsParserz6Protocol defining RESP3-specific parsing functionalitypubsub_push_handler_funcNinvalidation_push_handler_funcc                 C   r4   )zHandle pubsub push responsesr6   r9   r0   r1   r1   r2   handle_pubsub_push_response   s   z3PushNotificationsParser.handle_pubsub_push_responsec                 K   s*   |d t vr| |S | jr| |S d S )Nr   _INVALIDATION_MESSAGErc   rd   r9   r0   kwargsr1   r1   r2   handle_push_response   s
   

z,PushNotificationsParser.handle_push_responsec                 C   
   || _ d S r5   rc   r9   rc   r1   r1   r2   set_pubsub_push_handler      
z/PushNotificationsParser.set_pubsub_push_handlerc                 C   rl   r5   rd   r9   rd   r1   r1   r2   set_invalidation_push_handler   rp   z5PushNotificationsParser.set_invalidation_push_handlerr?   r@   rA   rT   r   __annotations__rd   r   rf   rk   ro   rs   r1   r1   r1   r2   rb      s   
 rb   c                   @   ra   )AsyncPushNotificationsParserz<Protocol defining async RESP3-specific parsing functionalityrc   Nrd   c                    rZ   )z+Handle pubsub push responses asynchronouslyr6   re   r1   r1   r2   rf      s   z8AsyncPushNotificationsParser.handle_pubsub_push_responsec                    s8   |d t vr| |I dH S | jr| |I dH S dS )z$Handle push responses asynchronouslyr   Nrg   ri   r1   r1   r2   rk      s   z1AsyncPushNotificationsParser.handle_push_responsec                 C   
   || _ dS )z$Set the pubsub push handler functionNrm   rn   r1   r1   r2   ro         
z4AsyncPushNotificationsParser.set_pubsub_push_handlerc                 C   rw   )z*Set the invalidation push handler functionNrq   rr   r1   r1   r2   rs      rx   z:AsyncPushNotificationsParser.set_invalidation_push_handlerrt   r1   r1   r1   r2   rv      s   
 rv   c                       sv   e Zd ZdZejd Zdef fddZdd Zdd	 Z	d
d Z
defddZdedefddZdefddZ  ZS )_AsyncRESPBasez!Base class for async resp parsing)rJ   rL   _pos_chunksrI   c                    s(   t  | d | _d| _g | _d| _d S )N    r   )superrN   rJ   rL   r{   rz   rM   	__class__r1   r2   rN      s
   
z_AsyncRESPBase.__init__c                 C   s   d| _ | j  d S )Nr|   )rL   r{   clearr8   r1   r1   r2   _clear   s   z_AsyncRESPBase._clearc                 C   s4   |j | _| jdu rtd|j| _|   d| _dS )zCalled when the stream connectsNBuffer is closed.T)_readerrW   r   rJ   r   
_connectedr<   r1   r1   r2   r>      s   

z_AsyncRESPBase.on_connectc                 C   s
   d| _ dS )z"Called when the stream disconnectsFN)r   r8   r1   r1   r2   r:      rx   z_AsyncRESPBase.on_disconnectrY   c              	      s~   | j std| jrdS z'td4 I d H  | j W  d   I d H  W S 1 I d H s-w   Y  W d S  ty>   Y dS w )Nr   Tr   F)r   r   rL   async_timeoutrW   Zat_eofr   r8   r1   r1   r2   r[      s   4z#_AsyncRESPBase.can_read_destructivelengthc              
      s   |d }| j | }t| j|kr| j| j |d  }n7| j| j d }z| j|t| I dH }W n tyD } ztt|d}~ww || dd }| j	| |  j |7  _ |S )z
        Read `length` bytes of data.  These are assumed to be followed
        by a '
' terminator which is subsequently discarded.
        r   N)
rz   r+   rL   rW   Zreadexactlyr   r   r$   r{   append)r9   r   Zwantendresulttaildataerrorr1   r1   r2   _read   s    

z_AsyncRESPBase._readc                    s   | j d| j}|dkr| j | j| }n'| j | jd }| j I dH }|ds/tt|| dd }| j	| |  jt
|d 7  _|S )zq
        read an unknown number of bytes up to the next '
'
        line separator, which is discarded.
        s   
r   Nr   r   )rL   findrz   rW   readlineendswithr   r$   r{   r   r+   )r9   foundr   r   r   r1   r1   r2   	_readline  s   
z_AsyncRESPBase._readline)r?   r@   rA   rT   rU   r^   r_   rN   r   r>   r:   r`   r[   bytesr   r   __classcell__r1   r1   r~   r2   ry      s    
	ry   )7sysabcr   asyncior   r   r   typingr   r   r   r	   r
   version_infomajorminorr   r   
exceptionsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   encodersr#   socketr$   r%   rB   rD   rE   rC   rF   r&   rH   rU   rh   rb   rv   ry   r1   r1   r1   r2   <module>   s4    P

6#