U
    ~fh*                  	   @  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rdd	lm Z  dd
l!m"Z" ddl#m$Z$ e%ddgZ&de'd< e%ddddddge&B Z(de'd< e(e%ddddddgB Z)de'd< dZ*de'd < d!Z+de'd"< d#d$d%d&d'd(d)d*d+h	Z,d,e'd-< d.d/d0d1d2Z-d`d3d4d5d6d7d8Z.d.d9d:d;d<Z/d=d=d>d?d@dAZ0dadCdDdEdFd>dGdHdIZ1dJdKdLdMdNZ2d=dKdOdPdQZ3dRdSdTdUdVZ4dRd>dTdWdXZ5dYd/dRdZd[d\Z6d>d]d^d_Z7dS )bzCBits 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_CODESiJ'  i{'  i{4  iR-  i|4     _NOT_PRIMARY_CODES      Y   i)#  i     _RETRYABLE_ERROR_CODESi  int_REAUTHENTICATION_REQUIRED_CODE   _AUTHENTICATION_FAILURE_CODEZauthenticateZ	saslstartZsaslcontinueZgetnonceZ
createuserZ
updateuserZcopydbgetnonceZcopydbsaslstartZcopydbset_SENSITIVE_COMMANDSr   str)keysreturnc                 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   :/tmp/pip-unpacked-wheel-36gvocj8/pymongo/helpers_shared.py
<listcomp>q   s     z#_gen_index_name.<locals>.<listcomp>)join)r,   r2   r2   r3   _gen_index_nameo   s    r6   r   zOptional[Union[int, str]]z8Sequence[tuple[str, Union[int, str, Mapping[str, Any]]]])key_or_list	directionr-   c                 C  s   |dk	r$t | tstd| |fgS t | tr8| tfgS t | tjrLt| S t | tjrdt|  S t | tt	fsztdg }| D ] }t |tr|tf}|
| q|S d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 directionzEif no direction is specified, key_or_list must be an instance of list)
isinstancer+   	TypeErrorr   r   	ItemsViewlistr   itemstupleappend)r7   r8   valuesr1   r2   r2   r3   _index_listt   s$    




rA   zdict[str, Any])
index_listr-   c                 C  s   t | tttjfs"tdt|  t| s2tdi }t | tjrh| D ]}| | }t	|| |||< qFn6| D ]0}t |t
r|tf}|\}}t	|| |||< ql|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)r9   r<   r>   r   r   r:   reprlen
ValueError_validate_index_key_pairr+   r   )rB   indexkeyvaluer1   r2   r2   r3   _index_document   s&    




rJ   r   None)rH   rI   r-   c                 C  s0   t | tstdt |tttjfs,tdd S )Nz6first item in each key pair must be an instance of strz[second item in each key pair must be 1, -1, '2d', or another valid MongoDB index specifier.)r9   r+   r:   r%   r   r   )rH   rI   r2   r2   r3   rF      s    
rF   Fr   zOptional[int]z$Optional[Container[Union[int, str]]]bool)responsemax_wire_versionallowable_errorsparse_write_concern_errorr-   c           
      C  sr  d| kr"t | d| d| ||rZd| krZ| d }| d}|rR|d|i t| | d rfdS | }d| kr| d  D ] }|dr~|ds~|} qq~|d }|d}	|r|	dk	r|	|krdS n||krdS |	dk	r|	tkrt|| n tj|ks
d	|krt|| |	d
kr.t	||	| |n2|	dkrHt
||	| |n|	dkr`t||	| |t ||	| |dS )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   ZLEGACY_ERRORr   r   r   )
rM   rN   rO   rP   _errorZ_labelsdetailsZshardrV   rR   r2   r2   r3   _check_command_response   sR    





r_   z	list[Any]r	   )write_errorsr-   c                 C  sD   | d }| ddkr(t| dd|t| d| d|d S )NrR   rW   rV   )rZ   r   r   )r`   errorr2   r2   r3   _raise_last_write_error   s    rc   )rb   r-   c                 C  s<   t | r t| d| d| t| d| d| d S )NrV   rR   )r   r   rZ   r   )rb   r2   r2   r3   r\     s    r\   zMapping[str, Any]zOptional[Mapping[str, Any]])resultr-   c                 C  s0   |  d}|r,|  d}|r,| }||d< |S )z%Return the writeConcernError or None.rS   rT   )rZ   copy)rd   wceZerror_labelsr2   r2   r3   _get_wce_doc
  s    

rg   c                 C  s.   |  d}|rt| t| }|r*t| dS )z?Backward compatibility helper for write command error handling.ZwriteErrorsN)rZ   rc   rg   r\   )rd   r`   rf   r2   r2   r3   _check_write_command_response  s    
rh   z'Union[Mapping[str, Any], Iterable[str]])fieldsoption_namer-   c                 C  s`   t | tjr| S t | tjtjfrNtdd | D sBt| dt| dS t| ddS )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)r9   r+   )r0   fieldr2   r2   r3   	<genexpr>3  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 namesN)	r9   r   r   r   Setallr:   dictfromkeys)ri   rj   r2   r2   r3   _fields_list_to_dict$  s    rr   )r-   c                  C  sV   t jrRt  } z>z$t| d | d | d dt j W n tk
rH   Y nX W 5 ~ X dS )z1Print exceptions raised by subscribers to stderr.r   rm      N)sysstderrexc_info	tracebackprint_exceptionOSError)Zeinfor2   r2   r3   _handle_exception:  s    $
rz   )N)NF)8__doc__
__future__r   rt   rw   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   rA   rJ   rF   r_   rc   r\   rg   rh   rr   rz   r2   r2   r2   r3   <module>   s   ,,


   C	