o
    sDim/                     @  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ridd	lm Z  d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< d`d!d"Z/dad&d'Z0dbd*d+Z1	dcddd1d2Z2ded5d6Z3dfd;d<Z4		=	dgdhdHdIZ5didMdNZ6djdPdQZ7dkdUdVZ8dldWdXZ9dmd\d]Z:dnd^d_Z;dS )ozCBits 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)PoolOptions)_DocumentOutiP-  [   zfrozenset[int]_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>	   copydbgetnonce	saslstart
createuser
updateuserauthenticatesaslcontinuecopydbgetnoncecopydbsaslstartzset[str]_SENSITIVE_COMMANDSoptionsr   returndict[str, float]c                 C  s\   ddl m} i }| }| j}| j}|r|d |d< |r$|s$|d |d< |r,|d |d< |S )Nr   )_csoti  	timeoutMSsocketTimeoutMSconnectTimeoutMS)pymongor6   get_timeoutsocket_timeoutconnect_timeout)r3   r6   detailstimeoutr<   r=    r@   T/home/alumno/antzosa8961/venv/lib/python3.10/site-packages/pymongo/helpers_shared.py_get_timeout_detailsp   s   rB   r>   Optional[dict[str, float]]strc                 C  sT   d}| r(|d7 }dD ]}|| v r|d| d| |  d7 }q
|d d }|d7 }|S )	N z (configured timeouts:)r8   r7   r9    z: zms,)r@   )r>   resultr?   r@   r@   rA   format_timeout_details   s   rJ   keysr   c                 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itemr@   r@   rA   
<listcomp>   s    z#_gen_index_name.<locals>.<listcomp>)join)rK   r@   r@   rA   _gen_index_name   s   rR   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 )
isinstancerD   	TypeErrortyper   r   	ItemsViewlistr   itemstupleappend)rS   rT   valuesrO   r@   r@   rA   _index_list   s(   




r`   
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)rW   r[   r]   r   r   rX   reprlen
ValueError_validate_index_key_pairrD   r   )ra   indexkeyvaluerO   r@   r@   rA   _index_document   s(   





rj   rh   r   ri   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 )rW   rD   rX   rY   r%   r   r   )rh   ri   r@   r@   rA   rf      s   
rf   Fresponser   max_wire_versionOptional[int]allowable_errors$Optional[Container[Union[int, str]]]parse_write_concern_errorbool	pool_optsOptional[PoolOptions]c                 C  sx  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r|rt
|}|	t|7 }	t|	|
| ||
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_errorr_   r   r   r   LEGACY_ERRORr   rB   rJ   r   r   )rl   rm   ro   rq   rs   _error_labelsr>   shardrz   rv   timeout_detailsr@   r@   rA   _check_command_response   s^   



r   write_errors	list[Any]r	   c                 C  s@   | d }| ddkrt| dd|t| d| d|)NrG   rv   r{   rz   )r~   r   r   )r   errorr@   r@   rA   _raise_last_write_error  s   r   r   c                 C  s8   t | rt| d| d| t| d| d| )Nrz   rv   )r   r   r~   r   )r   r@   r@   rA   r   (  s   r   rI   Mapping[str, Any]Optional[Mapping[str, Any]]c                 C  s0   |  d}|r|  d}|r| }||d< |S )z%Return the writeConcernError or None.rw   rx   )r~   copy)rI   wceerror_labelsr@   r@   rA   _get_wce_doc/  s   

r   c                 C  s2   |  d}|rt| t| }|rt| dS dS )z?Backward compatibility helper for write command error handling.writeErrorsN)r~   r   r   r   )rI   r   r   r@   r@   rA   _check_write_command_response=  s   
r   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)rW   rD   )rN   fieldr@   r@   rA   	<genexpr>X  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)	rW   r   r   r   SetallrX   dictfromkeys)r   r   r@   r@   rA   _fields_list_to_dictI  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   r      N)sysstderrexc_info	tracebackprint_exceptionOSError)einfor@   r@   rA   _handle_exception_  s   $r   )r3   r   r4   r5   )r>   rC   r4   rD   )rK   r   r4   rD   r   )rS   r   rT   rU   r4   rV   )ra   r   r4   rb   )rh   r   ri   r   r4   rk   )NFN)rl   r   rm   rn   ro   rp   rq   rr   rs   rt   r4   rk   )r   r   r4   r	   )r   r   r4   r	   )rI   r   r4   r   )rI   r   r4   rk   )r   r   r   rD   r4   r   )r4   rk   )<__doc__
__future__r   r   r   collectionsr   typingr   r   r   r   r   r	   r
   r   r   r:   r   pymongo.errorsr   r   r   r   r   r   r   r   r   pymongo.hellor   pymongo.cursor_sharedr   pymongo.operationsr   pymongo.pool_optionsr   pymongo.typingsr   	frozensetr   __annotations__r   r$   r&   r(   r2   rB   rJ   rR   r`   rj   rf   r   r   r   r   r   r   r   r@   r@   r@   rA   <module>   s`   ,,








H
	


