
    h'                        d Z ddlZddlZddl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 ddlmZ  edd	g      Z eg d
      ez  Ze eg d      z  ZdZd ZddZd Z	 ddZd Z d Z!d Z"d Z#d Z$d Z%d Z&d Z'y)zCBits and pieces used by the driver that don't really fit elsewhere.    N)abc	iteritems
itervaluesstring_type)SON)	ASCENDING)CursorNotFoundDuplicateKeyErrorExecutionTimeoutNotPrimaryErrorOperationFailureWriteConcernError
WriteErrorWTimeoutError)HelloCompatiP-  [   )iJ'  i{'  i{4  iR-  i|4     )      Y   i)#  i  _c                 X    t         j                  | D cg c]  }d|z  	 c}      S c c}w )z9Generate an index name from the set of fields it is over.z%s_%s)_UUNDERjoin)keysitems     M/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/pymongo/helpers.py_gen_index_namer   G   s#    <<D9D49::9s   'c                     || |fgS t        | t              r	| t        fgS t        | t        t        f      st        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.
    zEif no direction is specified, key_or_list must be an instance of list)
isinstancer   r   listtuple	TypeError)key_or_list	directions     r   _index_listr&   L   sV    
 i())k;/ ),--K$7Z      c                    t        | t        j                        r t        dt	        t        |             z        t        | t        t        f      st        dt        |       z         t        |       st        d      t               }| D ]U  \  }}t        |t              st        d      t        |t        t        t        j                  f      st        d      |||<   W |S )zbHelper to generate an index specifying document.

    Takes a list of (key, direction) pairs.
    zhpassing a dict to sort/create_index/hint is not allowed - use a list of tuples instead. did you mean %r?z0must use a list of (key, direction) pairs, not: z&key_or_list must not be the empty listz,first item in each key pair must be a stringz[second item in each key pair must be 1, -1, '2d', or another valid MongoDB index specifier.)r    r   Mappingr#   r!   r   r"   reprlen
ValueErrorr   r   int)
index_listindexkeyvalues       r   _index_documentr2   ]   s    
 *ckk*i
345
 	

 
T5M2MPTU_P``aaz?ABBEE" e#{+JKK%+sCKK!@AB  c
 Lr'   c                    d| vr,t        | j                  d      | j                  d      | |      |r:d| v r6| d   }| j                  d      }|r|j                  d|i       t        |       | d   ry| }d| v r:t	        | d         D ])  }|j                  d      s|j                  d      r'|} n |d   }|j                  d      }	|r|	|	|v ry||v ry|	|	t
        v r.t        ||       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_errorr   _NOT_MASTER_CODESr   r   LEGACY_ERRORr
   r   r	   )
responsemax_wire_versionallowable_errorsparse_write_concern_error_error_labelsdetailsshardr9   r5   s
             r   _check_command_responserK   z   s    8LL (,,v"6BR
 	
 !%8H%D-.,,}-MM='23"6*~G 0 	Eyy"599T?		 XF;;vD '''' $$!&(33		!	!V	+/Cv/Mfh// $$h8HII	vtX7GHH	VT85EFF
643C
DDr'   c                    t        | |       | j                  dd      r;t        | j                  d| j                  d            | j                  d      |       | j                  dd      }|| S |j                  t        j
                        rt        ||       | }d| v r"| d   D ]  }|j                  d      |k(  s|} n |j                  d      }|dv rt        |d   ||       t        |d   ||       )	zBReturn getlasterror response as a dict, or raise OperationFailure.wtimeoutFr9   errr5    
errObjectsr:   )	rK   r>   r   
startswithr   rB   r   r
   r   )resultrD   	error_msgrI   errobjr5   s         r   _check_gle_responserU      s    F$45zz*e$ FJJxE1BCVZZPVEWY_``

5"%IK445i00G v\* 	Fzz% I- 	
 ;;vD$$f==
75>4
88r'   c                     | d   }|j                  d      dk(  rt        |j                  d      d|      t        |j                  d      |j                  d      |      )Nr5   r;   r9   )r>   r
   r   )write_errorserrors     r   _raise_last_write_errorrZ      sX     EyyE!		( 3UEBB
UYYx(%))F*;U
CCr'   c                     d| v r?| d   j                  d      r+t        | j                  d      | j                  d      |       t        | j                  d      | j                  d      |       )NerrInforM   r9   r5   )r>   r   r   )rY   s    r   r@   r@      s^    EeI.22:>EIIh/61BEJJ
EIIh/61BE
JJr'   c                 \    | j                  d      }|r| j                  d      }|r||d<   |S )z%Return the writeConcernError or None.r6   r7   )r>   )rR   wceerror_labelss      r   _get_wce_docr`      s6    
**(
)C
 zz-0!-CJr'   c                 r    | j                  d      }|rt        |       t        |       }|rt        |       yy)z?Backward compatibility helper for write command error handling.writeErrorsN)r>   rZ   r`   r@   )rR   rX   r^   s      r   _check_write_command_responserc      s7     ::m,L-
v
C
"3' r'   c                 b    | j                  d      }|rt        |       t        | d   d          y)z8Backward compatibility helper for insert error handling.rb   writeConcernErrorsrW   N)r>   rZ   r@   )bulk_write_resultrX   s     r   _raise_last_errorrg      s3     %((7L-01EFrJKr'   c                 6   t        | t        j                        r| S t        | t        j                  t        j                  f      rFt        d | D              st        |dt        j                        t        j                  | 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                y wN)r    r   ).0fields     r   	<genexpr>z'_fields_list_to_dict.<locals>.<genexpr>  s     Fe:e[1Fs   z2 must be a list of key names, each an instance of    z' must be a mapping or list of key names)r    r   r)   SequenceSetallr#   r   __name__dictfromkeys)fieldsoption_names     r   _fields_list_to_dictrw   
  s{     &#++&&3<<12FvFF$/1E1EG  }}VQ''
kS
TTr'   c                      t         j                  rGt        j                         } 	 t        j                  | d   | d   | d   dt         j                         ~ yy# t
        $ r Y ~ yw xY w# ~ w xY w)z1Print exceptions raised by subscribers to stderr.r   rn      N)sysstderrexc_info	tracebackprint_exceptionIOError)einfos    r   _handle_exceptionr   !  sh     zz	%%eAha%(D#**U    		 s#   0A 	A&"A) %A&&A) )A,rj   )NF)(__doc__rz   r}   bson.py3compatr   r   r   r   bson.sonr   pymongor   pymongo.errorsr	   r
   r   r   r   r   r   r   pymongo.hello_compatr   	frozenset_SHUTDOWN_CODESrA   _RETRYABLE_ERROR_CODESr   r   r&   r2   rK   rU   rZ   r@   r`   rc   rg   rw   r    r'   r   <module>r      s    J 
  B B  	 	 	 - 
 	
	 
  +Y.   ;
"< RW:Ez9BDK		(LU.r'   