
    [j,                       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mZmZmZ ddl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)  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<   dJdZ0	 dKdLd"Z1dMd%Z2dNd*Z3	 	 dOdPd4Z4dQd8Z5dRd:Z6dSd>Z7dTd?Z8dUdCZ9dVdDZ: edEe
dFe	f         G          Z;dWdIZ<dS )XzCBits and pieces used by the driver that don't really fit elsewhere.    )annotationsN)abc)TYPE_CHECKINGAnyCallable	ContainerIterableMappingNoReturnOptionalSequenceTypeVarUnioncast)SON)	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keysr   returnstrc                @    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     S/var/www/app.qaans.com/backend/venv/lib/python3.11/site-packages/pymongo/helpers.py
<listcomp>z#_gen_index_name.<locals>.<listcomp>`   s!    <<<t^W^T*<<<    )join)r+   s    r4   _gen_index_namer8   ^   s#    88<<t<<<===r6   key_or_listr   	directionOptional[Union[int, str]]8Sequence[tuple[str, Union[int, str, Mapping[str, Any]]]]c                ,   |)t          | t                    st          d          | |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          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 directionzEif no direction is specified, key_or_list must be an instance of list)
isinstancer-   	TypeErrorr   r   	ItemsViewlistr
   itemstupleappend)r9   r:   valuesr3   s       r4   _index_listrF   c   s    +s++ 	A?@@@i())k3'' 	e ),--S]33 	e$$$S[11 	e))++,,,K$77 	ecddd(* 	  	 D$$$ )i(MM$r6   
index_listSON[str, Any]c                   t          | t          t          t          j        f          st          dt          |           z             t          |           st          d          t                      }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>   rA   rC   r   r
   r?   reprlen
ValueErrorr   _validate_index_key_pairr-   r   )rG   indexkeyvaluer3   s        r4   _index_documentrQ      s   
 j4"<== 
NQUV`QaQaa
 
 	
 z?? :899955E*ck**  	 	CsOE$S%000E#JJ	
  	 	D$$$ )i(JC$S%000E#JJLr6   rO   r   rP   Nonec                    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.)r>   r-   r?   r)   r   r
   )rO   rP   s     r4   rM   rM      s`    c3 RPQQQec3455 
>
 
 	

 
r6   Fresponser   max_wire_versionOptional[int]allowable_errors$Optional[Container[Union[int, str]]]parse_write_concern_errorboolc                N   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    r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_errorrE   r$   r   r   LEGACY_ERRORr   r   r   )
rT   rU   rW   rY   _error_labelsdetailsshardra   r]   s
             r4   _check_command_responserm      s-    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	vtX7GHHH	VT85EFFF
643C
D
DDr6   write_errors	list[Any]r   c                    | d         }|                     d          dk    r$t          |                     d          d|          t          |                     d          |                     d          |          )Nr]   rb   ra   )re   r   r   )rn   errors     r4   _raise_last_write_errorrs      sm     EyyE!!		( 3 3UEBBB
UYYx((%))F*;*;U
C
CCr6   rr   c                    t          |           r7t          |                     d          |                     d          |           t          |                     d          |                     d          |           )Nra   r]   )r   r   re   r   )rr   s    r4   rg   rg      sg    u KEIIh//61B1BEJJJ
EIIh//61B1BE
J
JJr6   resultMapping[str, Any]Optional[Mapping[str, Any]]c                l    |                      d          }|r|                      d          }|r||d<   |S )z%Return the writeConcernError or None.r^   r_   )re   )ru   wceerror_labelss      r4   _get_wce_docr{      sF    
**(
)
)C
 . zz-00 	.!-CJr6   c                    |                      d          }|rt          |           t          |           }|rt          |           dS dS )z?Backward compatibility helper for write command error handling.writeErrorsN)re   rs   r{   rg   )ru   rn   ry   s      r4   _check_write_command_responser~     s^     ::m,,L .---
v

C
 ("3'''''( (r6   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>   r-   )r2   fields     r4   	<genexpr>z'_fields_list_to_dict.<locals>.<genexpr>   s,      >>e:eS))>>>>>>r6   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     r4   _fields_list_to_dictr     s     &#+&& &3<122 (>>v>>>>> 	c{aaabbb}}VQ'''
{KKK
L
LLr6   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    r4   _handle_exceptionr   '  s     z 	%eAha%(D#*UUUU 	 	 	D	 IIII s)   4A A) 
A# A) "A##A) )A,F.)boundfuncc                :     d fd}t          t          |          S )Nargsr   kwargsr,   c                 \   |                     dd          }ddlm} ddlm} 	  | i |S # t
          $ rt}|r |j        t          k    r[d }| D ]/}t          ||          r|} nt          ||          r	|j	        } n0|r|
                    d           n  | i |cY d }~S  d }~ww xY w)N	no_reauthFr   )_BulkWriteContext)
ConnectionT)reauthenticate)poppymongo.messager   pymongo.poolr   r   r]   r*   r>   connauthenticate)	r   r   r   r   r   excr   argr   s	           r4   innerz_handle_reauth.<locals>.inner<  s0   JJ{E22	555555++++++	4(((( 	 	 	 x:::   C!#z22 "!#'899 "x  %%T%::::tT,V,,,,,,,,)	s!   - 
B+A(B&B+%B&&B+)r   r   r   r   r,   r   )r   r   )r   r   s   ` r4   _handle_reauthr   ;  s1         : 5>>r6   )r+   r   r,   r-   r   )r9   r   r:   r;   r,   r<   )rG   r   r,   rH   )rO   r   rP   r   r,   rR   )NF)
rT   r   rU   rV   rW   rX   rY   rZ   r,   rR   )rn   ro   r,   r   )rr   r   r,   r   )ru   rv   r,   rw   )ru   rv   r,   rR   )r   r   r   r-   r,   rv   )r,   rR   )r   r   r,   r   )=__doc__
__future__r   r   r   collectionsr   typingr   r   r   r   r	   r
   r   r   r   r   r   r   bson.sonr   pymongor   pymongo.errorsr   r   r   r   r   r   r   r   r   pymongo.hellor   pymongo.cursorr   pymongo.operationsr   pymongo.typingsr   r!   r"   __annotations__r$   r(   r*   r8   rF   rQ   rM   rm   rs   rg   r{   r~   r   r   r   r    r6   r4   <module>r      s   J I I " " " " " " 



                                                 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 & % % % % % -$$$$$$------,,,,,, 'Y
      I	
 	
 	
	 	 
      %7  : : %      (+  * * * *> > > > @D    8   :
 
 
 
 >B&+	@E @E @E @E @EFD D D DK K K K	 	 	 		( 	( 	( 	(M M M M,   " GCxS)***     r6   