o
    LjS                     @   s8  U d dl Z d dlmZmZ d dlmZmZ d dlmZm	Z	m
Z
mZmZmZ d dlmZmZmZmZmZmZmZmZ d dlmZ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,m-Z- dd	lm.Z. d
dl/m0Z0 d
dl1m2Z2m3Z3 dZ4dZ5dZ6dZ7eedZ8de#iZ9de)iZ:e ;e<Z=G dd deZ>G dd de>Z?G dd de>Z@G dd dZAdZBdZCdZDdZEd ZFd!ZGd"ZHd#ZIeDeEeFeGeHfZJeDeeAjKfeEeeAjLfeFeeAjKfeGeeAjLfeCeeAjMfeHeeAjNfeIeeAjOfiZPeQeReSeTe e	f f eUd$< G d%d& d&eZVG d'd( d(eZWG d)d* d*e@ZXdS )+    N)ABCabstractmethod)IncompleteReadErrorStreamReader)	AwaitableCallableListOptionalProtocolUnion)MaintenanceNotificationNodeFailedOverNotificationNodeFailingOverNotificationNodeMigratedNotificationNodeMigratingNotificationNodeMovingNotificationOSSNodeMigratedNotificationOSSNodeMigratingNotification)deprecated_functionsafe_str   )AskErrorAuthenticationError$AuthenticationWrongNumberOfArgsErrorBusyLoadingErrorClusterCrossSlotErrorClusterDownErrorConnectionErrorExecAbortErrorExternalAuthProviderErrorMasterDownErrorModuleError
MovedErrorNoPermissionErrorNoScriptErrorNoSuchFieldsetErrorOutOfMemoryErrorReadOnlyError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 setzproblem with LDAP servicezno such fieldsetc                   @   s   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eedZedd Zedd	 Ze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)ERROOM	WRONGPASS	EXECABORTLOADINGNOSCRIPTREADONLYNOAUTHNOPERMASKTRYAGAINMOVEDCLUSTERDOWN	CROSSSLOT
MASTERDOWNc                 C   s`   | dd }|| jv r,|t|d d }| j| }t|tr&||t}|||dS t|S )zParse an error response r   r+   N)status_code)splitEXCEPTION_CLASSESlen
isinstancedictgetr(   )clsresponse
error_codeexception_class rK   Q/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/redis/_parsers/base.pyparse_errorp   s   


zBaseParser.parse_errorc                 C      d S NrK   selfrK   rK   rL   on_disconnect|      zBaseParser.on_disconnectc                 C   rN   rO   rK   rQ   
connectionrK   rK   rL   
on_connect   rS   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_ERROREXTERNAL_AUTH_PROVIDER_ERRORNO_SUCH_FIELDSET_ERRORr&   r   r   r$   r'   r#   r   r)   r"   r   r   r    rB   classmethodrM   r   rR   rV   rK   rK   rK   rL   r/   K   sN    $

r/   c                   @   sD   e Zd ZdZdd Zdd Zdd Zdd	 Zddede	fddZ
dS )	_RESPBasez&Base class for sync-based resp parsingc                 C   s   || _ d | _d | _d | _d S rO   )socket_read_sizeencoder_sock_bufferrQ   rc   rK   rK   rL   __init__   s   
z_RESPBase.__init__c                 C   s$   z|    W d S  ty   Y d S w rO   )rR   	ExceptionrP   rK   rK   rL   __del__   s
   z_RESPBase.__del__c                 C   s(   |j | _ t| j | j|j| _|j| _dS )zCalled when the socket connectsN)re   r.   rc   socket_timeoutrf   rd   rT   rK   rK   rL   rV      s
   z_RESPBase.on_connectc                 C   s*   d| _ | jdur| j  d| _d| _dS )z"Called when the socket disconnectsN)re   rf   closerd   rP   rK   rK   rL   rR      s
   


z_RESPBase.on_disconnectr   timeoutreturnc                 C   s   | j d u rdS | j |S )NF)rf   can_read)rQ   rm   rK   rK   rL   ro      s   
z_RESPBase.can_readN)r   )rW   rX   rY   __doc__rh   rj   rV   rR   floatboolro   rK   rK   rK   rL   rb      s    rb   c                
   @   sz   e Zd ZdZdZdefddZedddd	ed
e	fddZ
e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_sizerc   c                 C   s   d | _ || _d S rO   rt   rg   rK   rK   rL   rh      s   
zAsyncBaseParser.__init__8.0.0Use can_read() insteadcan_read_destructiveversionreasonnamern   c                       d S rO   rK   rP   rK   rK   rL   ry      s   z$AsyncBaseParser.can_read_destructivec                    r~   rO   rK   rP   rK   rK   rL   ro      s   zAsyncBaseParser.can_readFdisable_decodingNc                       t  rO   NotImplementedError)rQ   r   rK   rK   rL   read_response   s   zAsyncBaseParser.read_response)F)rW   rX   rY   rp   	__slots__intrh   r   r   rr   ry   ro   r   r*   r(   r   r   rK   rK   rK   rL   rs      s"    rs   c                   @   sL   e Zd ZdZedd Zedd Zedd Zedd	 Zed
d Z	dS )MaintenanceNotificationsParserzEProtocol defining maintenance push notification parsing functionalityc                 C   s   | d }t | d }t||S Nr+   r   )r   r   )rH   idslotsrK   rK   rL   parse_oss_maintenance_start_msg   s   
z>MaintenanceNotificationsParser.parse_oss_maintenance_start_msgc           
      C   sh   | d }| d }i }|D ]"\}}}t |}t |}t |}	||vr%g ||< || ||	i qt||S r   )r   appendr   )
rH   r   nodes_to_slots_mapping_datanodes_to_slots_mappingsrc_node	dest_noder   src_node_strdest_node_str	slots_strrK   rK   rL   #parse_oss_maintenance_completed_msg   s   	
zBMaintenanceNotificationsParser.parse_oss_maintenance_completed_msgc                 C   s   | d }| d }|||S r   rK   )rH   notification_typer   ttlrK   rK   rL   parse_maintenance_start_msg   s   
z:MaintenanceNotificationsParser.parse_maintenance_start_msgc                 C   s   | d }||S )Nr+   rK   )rH   r   r   rK   rK   rL   parse_maintenance_completed_msg   s   z>MaintenanceNotificationsParser.parse_maintenance_completed_msgc                 C   sd   | d }| d }| d d u rd\}}nt | d }|dd\}}|d ur)t|nd }t||||S )Nr+   r      )NN:)r   rsplitr   r   )rH   r   r   hostportvaluerK   rK   rL   parse_moving_msg   s   
z/MaintenanceNotificationsParser.parse_moving_msgN)
rW   rX   rY   rp   staticmethodr   r   r   r   r   rK   rK   rK   rL   r      s    


	
r   
invalidateMOVING	MIGRATINGMIGRATEDFAILING_OVERFAILED_OVER
SMIGRATING	SMIGRATED-MSG_TYPE_TO_MAINT_NOTIFICATION_PARSER_MAPPINGc                   @   s   e Zd ZU dZeed< dZee ed< dZee ed< dZ	ee ed< dZ
ee ed< dd	 Zd
d Zdd Z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_funcnode_moving_push_handler_funcmaintenance_push_handler_func#oss_cluster_maint_push_handler_funcc                 C   s   t  )zHandle pubsub push responsesr   rQ   rH   rK   rK   rL   handle_pubsub_push_responseK  s   z3PushNotificationsParser.handle_pubsub_push_responsec              
   K   s  |d }t |tr| }|tgtttR vr| |S z|tkr,| jr,| |W S |tkrC| j	rCt
| d }||}| 	|W S |tv rn| jrnt
| d }|tkrY||}nt
| d }|||}|d urn| |W S |tkr| jsx| jrt
| d }||}|d ur| jr| | | jr| | W d S W d S W d S W d S W d S  ty } ztd||| W Y d }~d S d }~ww )Nr   r+   "Error handling {} message ({}): {}rD   bytesdecode_INVALIDATION_MESSAGE_MAINTENANCE_MESSAGES_MOVING_MESSAGE_SMIGRATED_MESSAGEr   r   r   r   r   _SMIGRATING_MESSAGEr   ri   loggererrorformatrQ   rH   kwargsmsg_typeparser_functionnotificationr   erK   rK   rL   handle_push_responseO  s   





z,PushNotificationsParser.handle_push_responsec                 C   
   || _ d S rO   r   rQ   r   rK   rK   rL   set_pubsub_push_handler     
z/PushNotificationsParser.set_pubsub_push_handlerc                 C   r   rO   r   rQ   r   rK   rK   rL   set_invalidation_push_handler  r   z5PushNotificationsParser.set_invalidation_push_handlerc                 C   r   rO   r   rQ   r   rK   rK   rL   set_node_moving_push_handler  r   z4PushNotificationsParser.set_node_moving_push_handlerc                 C   r   rO   r   rQ   r   rK   rK   rL   set_maintenance_push_handler  r   z4PushNotificationsParser.set_maintenance_push_handlerc                 C   r   rO   r   rQ   r   rK   rK   rL   "set_oss_cluster_maint_push_handler  r   z:PushNotificationsParser.set_oss_cluster_maint_push_handler)rW   rX   rY   rp   r   __annotations__r   r	   r   r   r   r   r   r   r   r   r   r   rK   rK   rK   rL   r   B  s   
 ?r   c                   @   s   e Zd ZU dZeed< dZee ed< dZeede	d f  ed< dZ
eede	d f  ed< dZeede	d f  ed< d	d
 Zdd Zdd Zdd Zdd Zdd Zdd ZdS )AsyncPushNotificationsParserz<Protocol defining async RESP3-specific parsing functionalityr   Nr   .r   r   r   c                    r   )z+Handle pubsub push responses asynchronouslyr   r   rK   rK   rL   r     s   z8AsyncPushNotificationsParser.handle_pubsub_push_responsec              
      s  |d }t |tr| }|tgtttR vr"| |I dH S z|tkr3| jr3| |I dH W S t |tr<| }|tkrV| j	rVt
| d }||}| 	|I dH W S |tv r| jrt
| d }|tkrl||}nt
| d }|||}|dur| |I dH W S |tkr| jrt
| d }||}|dur| |I dH W S W dS W dS W dS  ty } ztd||| W Y d}~dS d}~ww )z$Handle push responses asynchronouslyr   Nr+   r   r   r   rK   rK   rL   r     s   



z1AsyncPushNotificationsParser.handle_push_responsec                 C   
   || _ dS )z$Set the pubsub push handler functionNr   r   rK   rK   rL   r        
z4AsyncPushNotificationsParser.set_pubsub_push_handlerc                 C   r   )z*Set the invalidation push handler functionNr   r   rK   rK   rL   r     r   z:AsyncPushNotificationsParser.set_invalidation_push_handlerc                 C   r   rO   r   r   rK   rK   rL   r     r   z9AsyncPushNotificationsParser.set_node_moving_push_handlerc                 C   r   rO   r   r   rK   rK   rL   r     r   z9AsyncPushNotificationsParser.set_maintenance_push_handlerc                 C   r   rO   r   r   rK   rK   rL   r     r   z?AsyncPushNotificationsParser.set_oss_cluster_maint_push_handler)rW   rX   rY   rp   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   rK   rK   rK   rL   r     s   
 ?r   c                       s   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
edddddef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)rd   rf   _pos_chunksrc   c                    s(   t  | d | _d| _g | _d| _d S )N    r   )superrh   rd   rf   r   r   rg   	__class__rK   rL   rh     s
   
z_AsyncRESPBase.__init__c                 C   s   d| _ | j  d S )Nr   )rf   r   clearrP   rK   rK   rL   _clear  s   z_AsyncRESPBase._clearc                 C   s4   |j | _| jdu rtt|j| _|   d| _dS )zCalled when the stream connectsNT)_readerru   r   r-   rd   r   
_connectedrT   rK   rK   rL   rV     s   

z_AsyncRESPBase.on_connectc                 C   s
   d| _ dS )z"Called when the stream disconnectsFN)r   rP   rK   rK   rL   rR     r   z_AsyncRESPBase.on_disconnectrw   rx   ry   rz   rn   c                    s   |   I d H S rO   )ro   rP   rK   rK   rL   ry     s   z#_AsyncRESPBase.can_read_destructivec                    s0   | j std| jrdS t| jjp| j S )NzBuffer is closed.T)r   OSErrorrf   rr   ru   at_eofrP   rK   rK   rL   ro   !  s   z_AsyncRESPBase.can_read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)
r   rC   rf   ru   readexactlyr   r   r-   r   r   )rQ   r   wantendresulttaildatar   rK   rK   rL   _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   )rf   findr   ru   readlineendswithr   r-   r   r   rC   )rQ   foundr   r   r   rK   rK   rL   	_readlineA  s   
z_AsyncRESPBase._readline)rW   rX   rY   rp   rs   r   r   rh   r   rV   rR   r   rr   ry   ro   r   r   r   __classcell__rK   rK   r   rL   r     s     
	r   )Yloggingabcr   r   asyncior   r   typingr   r   r   r	   r
   r   redis.maint_notificationsr   r   r   r   r   r   r   r   redis.utilsr   r   
exceptionsr   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   encodersr,   socketr-   r.   rZ   r\   r]   r[   r^   r_   r`   	getLoggerrW   r   r/   rb   rs   r   r   r   _MIGRATING_MESSAGE_MIGRATED_MESSAGE_FAILING_OVER_MESSAGE_FAILED_OVER_MESSAGEr   r   r   r   r   r   r   r   r   rE   strtupletyper   r   r   r   rK   rK   rK   rL   <module>   s   
  (
T


:'G"\^