
    _i0                       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rd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(z  Z*de)d<   e* e'g d          z  Z+de)d<   dZ,de)d<   dZ-de)d<   h dZ.de)d<   dSd!Z/dTd%Z0dUd(Z1	 dVdWd.Z2dXd1Z3dYd6Z4	 	 	 dZd[dBZ5d\dFZ6d]dHZ7d^dLZ8d_dMZ9d`dQZ:dadRZ;d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)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                    ddl m} i }|                                }| j        }| j        }|r|dz  |d<   |r
|s|dz  |d<   |r|dz  |d<   |S )Nr   )_csoti  	timeoutMSsocketTimeoutMSconnectTimeoutMS)pymongor8   get_timeoutsocket_timeoutconnect_timeout)r4   r8   detailstimeoutr>   r?   s         9D:\Nbitz\venv\Lib\site-packages\pymongo/helpers_shared.py_get_timeout_detailsrC   p   s    G!!G+N-O .&~ ;g ;%3d%:!" =&5&<"#N    r@   Optional[dict[str, float]]strc                l    d}| r/|dz  }dD ]}|| v r|d| d| |          dz  }|d d         }|dz  }|S )	N z (configured timeouts:)r:   r9   r;    z: zms,) )r@   resultrA   s      rB   format_timeout_detailsrN      ss    F **K 	? 	?G'!!>g>>)9>>>>#MrD   keysr   c                @    d                     d | D                       S )z9Generate an index name from the set of fields it is over._c                "    g | ]} d j         | S )z{}_{})format).0items     rB   
<listcomp>z#_gen_index_name.<locals>.<listcomp>   s!    <<<t^W^T*<<<rD   )join)rO   s    rB   _gen_index_namerX      s#    88<<t<<<===rD   key_or_listr   	directionOptional[Union[int, str]]8Sequence[tuple[str, Union[int, str, Mapping[str, Any]]]]c                l   |9t          | t                    st          dt          |                      | |fgS t          | t                    r
| t          fgS t          | t
          j                  rt          |           S t          | t
          j                  r!t          | 	                                          S t          | t          t          f          st          dt          |                      g }| D ]5}t          |t                    r	|t          f}|                    |           6|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 )
isinstancerF   	TypeErrortyper   r   	ItemsViewlistr	   itemstupleappend)rY   rZ   valuesrU   s       rB   _index_listrg      sC    +s++ 	[Yd;FWFWYYZZZi())k3'' 		 ),--S]33 	$$$S[11 	))++,,,K$77 	q^bcn^o^oqq   )+ 	  	 D$$$ )i(MM$rD   
index_listdict[str, Any]c                   t          | t          t          t          j        f          st          dt          |           z             t          |           st          d          i }t          | t          j                  r#| D ]}| |         }t          ||           |||<    n=| D ]:}t          |t                    r	|t          f}|\  }}t          ||           |||<   ;|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^   rb   rd   r   r	   r_   reprlen
ValueError_validate_index_key_pairrF   r   )rh   indexkeyvaluerU   s        rB   _index_documentrr      s	   
 j4"<== 
NQUV`QaQaa
 
 	
 z?? :8999E*ck**  	 	CsOE$S%000E#JJ	
  	 	D$$$ )i(JC$S%000E#JJLrD   rp   r   rq   Nonec                    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^   rF   r_   r`   r&   r   r	   )rp   rq   s     rB   rn   rn      s    c3 dbW[\_W`W`bbcccec3455 
#%[[# #
 
 	

 
rD   Fresponser   max_wire_versionOptional[int]allowable_errors$Optional[Container[Union[int, str]]]parse_write_concern_errorbool	pool_optsOptional[PoolOptions]c                   d| vr8t          |                     d          |                     d          | |          |rId| v rE| d         }|                     d          }|r|                    d|i           t          |           | d         rdS | }d| v rK| d                                         D ]0}|                    d          r|                    d          s|} n1|d         }	|                    d          }
|r|
|
|v rdS n|	|v rdS |
|
t
          v rt          |	|           n"t          j        |	v sd	|	v rt          |	|           |
d
v rt          |	|
| |          |
dk    r5|r!t          |          }|	t          |          z  }	t          |	|
| |          |
dk    rt          |	|
| |          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_errorrf   r    r   r   LEGACY_ERRORr   rC   rN   r   r   )ru   rv   rx   rz   r|   _error_labelsr@   shardr   r   timeout_detailss               rB   _check_command_responser      sU    8LL  LL  	
 
 	
 ! +%8H%D%D-.,,}-- 	4MM='2333"6***~ G e_++-- 	 	Eyy"" 599T?? XF;;vD  ''' ('''F %%%!&(333 &		!V	+	+/Cv/M/Mfh/// $$$h8HIII	 	>29==O,_===FvtX7GHHH	VT85EFFF
643C
D
DDrD   write_errors	list[Any]r
   c                    | d         }|                     d          dk    r$t          |                     d          d|          t          |                     d          |                     d          |          )NrJ   r   r   r   )r   r   r   )r   errors     rB   _raise_last_write_errorr     sm     EyyE!!		( 3 3UEBBB
UYYx((%))F*;*;U
C
CCrD   r   c                    t          |           r7t          |                     d          |                     d          |           t          |                     d          |                     d          |           )Nr   r   )r   r   r   r   )r   s    rB   r   r   (  sg    u KEIIh//61B1BEJJJ
EIIh//61B1BE
J
JJrD   rM   Mapping[str, Any]Optional[Mapping[str, Any]]c                    |                      d          }|r0|                      d          }|r|                                }||d<   |S )z%Return the writeConcernError or None.r   r   )r   copy)rM   wceerror_labelss      rB   _get_wce_docr   /  sS    
**(
)
)C
 . zz-00 	.((**C!-CJrD   c                    |                      d          }|rt          |           t          |           }|rt          |           dS dS )z?Backward compatibility helper for write command error handling.writeErrorsN)r   r   r   r   )rM   r   r   s      rB   _check_write_command_responser   =  s^     ::m,,L .---
v

C
 ("3'''''( (rD   fields'Union[Mapping[str, Any], Iterable[str]]option_namec                6   t          | t          j                  r| S t          | t          j        t          j        f          rFt          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              3  @   K   | ]}t          |t                    V  d S N)r^   rF   )rT   fields     rB   	<genexpr>z'_fields_list_to_dict.<locals>.<genexpr>X  s,      >>e:eS))>>>>>>rD   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)r   r   s     rB   _fields_list_to_dictr   I  s     &#+&& &3<122 (>>v>>>>> 	c{aaabbb}}VQ'''
{KKK
L
LLrD   c                     t           j        rbt          j                    } 	 t          j        | d         | d         | d         dt           j                   n# t
          $ r Y nw xY w~ dS # ~ w xY wdS )z1Print exceptions raised by subscribers to stderr.r   r      N)sysstderrexc_info	tracebackprint_exceptionOSError)einfos    rB   _handle_exceptionr   _  s     z 	%eAha%(D#*UUUU 	 	 	D	 IIII s)   4A A) 
A# A) "A##A) )A,)r4   r   r5   r6   )r@   rE   r5   rF   )rO   r   r5   rF   r   )rY   r   rZ   r[   r5   r\   )rh   r   r5   ri   )rp   r   rq   r   r5   rs   )NFN)ru   r   rv   rw   rx   ry   rz   r{   r|   r}   r5   rs   )r   r   r5   r
   )r   r   r5   r
   )rM   r   r5   r   )rM   r   r5   rs   )r   r   r   rF   r5   r   )r5   rs   )<__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)   r3   rC   rN   rX   rg   rr   rn   r   r   r   r   r   r   r   rL   rD   rB   <module>r      s   J I I " " " " " " 



          
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
      
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 & % % % % % -++++++------000000,,,,,,
 #,)
# #     I	
 	
 	
	 	 
      *<ii  	? 	? 	*  	 	 	 	 (+  * * * * %'  & & & &

! 
! 
!  
 
 
 
    	 	 	 	> > > > @D    <   :
 
 
 
 >B&+'+EE EE EE EE EEPD D D DK K K K   	( 	( 	( 	(M M M M,     rD   