o
    h*                     @  s  U d Z ddlmZ ddlZddlZ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 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 ddlmZ ercdd	lm Z  dd
l!m"Z" ddl#m$Z$ e%ddgZ&de'd< e%g de&B Z(de'd< e(e%g dB Z)de'd< dZ*de'd< dZ+de'd< h dZ,de'd< dTd d!Z-	dUdVd'd(Z.dWd+d,Z/dXd1d2Z0		3dYdZd<d=Z1d[dAdBZ2d\dDdEZ3d]dIdJZ4d^dKdLZ5d_dPdQZ6d`dRdSZ7dS )azCBits and pieces used by the driver that don't really fit elsewhere.    )annotationsN)abc)	TYPE_CHECKINGAny	ContainerIterableMappingNoReturnOptionalSequenceUnion)	ASCENDING)	CursorNotFoundDuplicateKeyErrorExecutionTimeoutNotPrimaryErrorOperationFailureWriteConcernError
WriteErrorWTimeoutError_wtimeout_error)HelloCompat)_Hint)
_IndexList)_DocumentOutiP-  [   	frozenset_SHUTDOWN_CODES)iJ'  i{'  i{4  iR-  i|4     _NOT_PRIMARY_CODES)      Y   i)#  i     _RETRYABLE_ERROR_CODESi  int_REAUTHENTICATION_REQUIRED_CODE   _AUTHENTICATION_FAILURE_CODE>	   Z
createuserZ	saslstartZauthenticateZcopydbZ
updateuserZgetnonceZcopydbsaslstartZsaslcontinueZcopydbgetnonceset_SENSITIVE_COMMANDSkeysr   returnstrc                 C  s   d dd | D S )z9Generate an index name from the set of fields it is over._c                 S  s   g | ]}d j | qS )z{}_{})format).0item r2   I/var/www/html/venv/lib/python3.10/site-packages/pymongo/helpers_shared.py
<listcomp>q   s    z#_gen_index_name.<locals>.<listcomp>)join)r+   r2   r2   r3   _gen_index_nameo   s   r6   key_or_listr   	directionOptional[Union[int, str]]8Sequence[tuple[str, Union[int, str, Mapping[str, Any]]]]c                 C  s   |durt | tstdt|  | |fgS t | tr!| tfgS t | tjr+t| S t | tjr7t| 	 S t | tt
fsGtdt|  g }| D ]}t |trV|tf}|| qK|S )zHelper to generate a list of (key, direction) pairs.

    Takes such a list, or a single key, or a single key and direction.
    Nz'Expected a string and a direction, not zKif no direction is specified, key_or_list must be an instance of list, not )
isinstancer-   	TypeErrortyper   r   	ItemsViewlistr   itemstupleappend)r7   r8   valuesr1   r2   r2   r3   _index_listt   s(   




rD   
index_listdict[str, Any]c                 C  s   t | tttjfstdt|  t| stdi }t | tjr5| D ]}| | }t	|| |||< q#|S | D ]}t |t
rB|tf}|\}}t	|| |||< q7|S )zbHelper to generate an index specifying document.

    Takes a list of (key, direction) pairs.
    z@must use a dictionary or a list of (key, direction) pairs, not: zkey_or_list must not be empty)r;   r?   rA   r   r   r<   reprlen
ValueError_validate_index_key_pairr-   r   )rE   indexkeyvaluer1   r2   r2   r3   _index_document   s(   





rN   rL   r   rM   Nonec                 C  sD   t | tstdt|  t |tttjfs tdt| d S )Nz<first item in each key pair must be an instance of str, not zasecond item in each key pair must be 1, -1, '2d', or another valid MongoDB index specifier., not )r;   r-   r<   r=   r%   r   r   )rL   rM   r2   r2   r3   rJ      s   
rJ   Fresponser   max_wire_versionOptional[int]allowable_errors$Optional[Container[Union[int, str]]]parse_write_concern_errorboolc           
      C  s`  d| vrt | d| d| ||r-d| v r-| d }| d}|r)|d|i t| | d r3dS | }d| v rP| d  D ]}|drO|dsO|} nq?|d }|d}	|rl|	durf|	|v redS n||v rldS |	durz|	tv ryt|| ntj|v sd	|v rt|| |	d
v rt	||	| ||	dkrt
||	| ||	dkrt||	| |t ||	| |)z+Check the response to a command for errors.okz$errcodewriteConcernErrorerrorLabelsNrawerrmsgznode is recovering)*  i*  i&1  2   +   )r   getupdate_raise_write_concern_errorrC   r   r   r   ZLEGACY_ERRORr   r   r   )
rP   rQ   rS   rU   _errorZ_labelsdetailsZshardr\   rX   r2   r2   r3   _check_command_response   sX   



re   write_errors	list[Any]r	   c                 C  s@   | d }| ddkrt| dd|t| d| d|)NrX   r]   r\   )r`   r   r   )rf   errorr2   r2   r3   _raise_last_write_error   s   rj   ri   c                 C  s8   t | rt| d| d| t| d| d| )Nr\   rX   )r   r   r`   r   )ri   r2   r2   r3   rb     s   rb   resultMapping[str, Any]Optional[Mapping[str, Any]]c                 C  s0   |  d}|r|  d}|r| }||d< |S )z%Return the writeConcernError or None.rY   rZ   )r`   copy)rk   wceZerror_labelsr2   r2   r3   _get_wce_doc  s   

rp   c                 C  s2   |  d}|rt| t| }|rt| dS dS )z?Backward compatibility helper for write command error handling.ZwriteErrorsN)r`   rj   rp   rb   )rk   rf   ro   r2   r2   r3   _check_write_command_response  s   
rq   fields'Union[Mapping[str, Any], Iterable[str]]option_namec                 C  s\   t | tjr| S t | tjtjfr'tdd | D s!t| dt| dS t| d)zTakes a sequence of field names and returns a matching dictionary.

    ["a", "b"] becomes {"a": 1, "b": 1}

    and

    ["a.b.c", "d", "a.c"] becomes {"a.b.c": 1, "d": 1, "a.c": 1}
    c                 s  s    | ]}t |tV  qd S N)r;   r-   )r0   fieldr2   r2   r3   	<genexpr>6  s    z'_fields_list_to_dict.<locals>.<genexpr>z5 must be a list of key names, each an instance of str   z' must be a mapping or list of key names)	r;   r   r   r   Setallr<   dictfromkeys)rr   rt   r2   r2   r3   _fields_list_to_dict'  s   r}   c                  C  s\   t jr,t  } z"zt| d | d | d dt j W W ~ dS  ty)   Y W ~ dS w ~ w dS )z1Print exceptions raised by subscribers to stderr.r   rx      N)sysstderrexc_info	tracebackprint_exceptionOSError)Zeinfor2   r2   r3   _handle_exception=  s   $r   )r+   r   r,   r-   ru   )r7   r   r8   r9   r,   r:   )rE   r   r,   rF   )rL   r   rM   r   r,   rO   )NF)
rP   r   rQ   rR   rS   rT   rU   rV   r,   rO   )rf   rg   r,   r	   )ri   r   r,   r	   )rk   rl   r,   rm   )rk   rl   r,   rO   )rr   rs   rt   r-   r,   rl   )r,   rO   )8__doc__
__future__r   r   r   collectionsr   typingr   r   r   r   r   r	   r
   r   r   Zpymongor   Zpymongo.errorsr   r   r   r   r   r   r   r   r   Zpymongo.hellor   Zpymongo.cursor_sharedr   Zpymongo.operationsr   Zpymongo.typingsr   r   r   __annotations__r   r$   r&   r(   r*   r6   rD   rN   rJ   re   rj   rb   rp   rq   r}   r   r2   r2   r2   r3   <module>   sX   ,,






C
	


