o
    Åîï]P'  ã                   @   s   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 eddgƒZeg d	¢ƒeB Zeeg d
¢ƒB ZdZdd„ Zd#dd„Zdd„ Z		d$dd„Zdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd „ Z#d!d"„ Z$dS )%zCBits and pieces used by the driver that don't really fit elsewhere.é    N)ÚabcÚ	iteritemsÚ
itervaluesÚstring_type)ÚSON)Ú	ASCENDING)ÚCursorNotFoundÚDuplicateKeyErrorÚExecutionTimeoutÚNotMasterErrorÚOperationFailureÚ
WriteErrorÚWriteConcernErrorÚWTimeoutErroriP-  é[   )i{'  i{4  iR-  i|4  é½   )é   é   éY   i)#  Ú_c                 C   s   t  dd„ | D ƒ¡S )z9Generate an index name from the set of fields it is over.c                 S   s   g | ]}d | ‘qS )z%s_%s© )Ú.0Úitemr   r   úA/var/www/html/env/lib/python3.10/site-packages/pymongo/helpers.pyÚ
<listcomp>;   s    z#_gen_index_name.<locals>.<listcomp>)Ú_UUNDERÚjoin)Úkeysr   r   r   Ú_gen_index_name9   s   r   c                 C   s@   |dur	| |fgS t | tƒr| tfgS t | ttfƒstdƒ‚| S )z�Helper to generate a list of (key, direction) pairs.

    Takes such a list, or a single key, or a single key and direction.
    NzEif no direction is specified, key_or_list must be an instance of list)Ú
isinstancer   r   ÚlistÚtupleÚ	TypeError)Úkey_or_listÚ	directionr   r   r   Ú_index_list>   s   


r%   c                 C   sš   t | tjƒrtdtt| ƒƒ ƒ‚t | ttfƒstdt| ƒ ƒ‚t| ƒs't	dƒ‚t
ƒ }| D ]\}}t |tƒs9tdƒ‚t |tttjfƒsFtdƒ‚|||< q,|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 stringzjsecond item in each key pair must be 1, -1, '2d', 'geoHaystack', or another valid MongoDB index specifier.)r   r   ÚMappingr"   r    r   r!   ÚreprÚlenÚ
ValueErrorr   r   Úint)Ú
index_listÚindexÚkeyÚvaluer   r   r   Ú_index_documentN   s$   
þÿ

r/   Fc                 C   sN  d| vrt |  d¡|  d¡| ƒ‚|rd| v rt| d ƒ | d s£| }d| v r=t| d ƒD ]}| d¡r<| d¡s<|} nq,|d }|du sI||vr¥| d¡}|tv rWt|| ƒ‚d|v s_d	|v rdt|| ƒ‚|d
kryd| dd¡ }t || d¡| ƒ‚|dv rƒt||| ƒ‚|dkr�t||| ƒ‚|dkr—t||| ƒ‚|pšd}t || || ƒ‚dS dS )z0Check the response to a command for errors.
    Úokz$errÚcodeÚwriteConcernErrorÚrawÚerrmsgNú
not masterznode is recoveringzdb assertion failurez%db assertion failure, assertion: '%s'Ú	assertionÚ ÚassertionCode©éø*  iù*  i&1  é2   é+   z%s)	r   ÚgetÚ_raise_write_concern_errorr   Ú_NOT_MASTER_CODESr   r	   r
   r   )ÚresponseÚmsgÚallowable_errorsÚparse_write_concern_errorÚdetailsÚshardr4   r1   r   r   r   Ú_check_command_responsei   sR   
þ€



ÿþ×rF   c                 C   sÂ   t | ƒ |  dd¡rt|  d|  d¡¡|  d¡| ƒ‚|  dd¡}|du r&| S | d¡r0t|| ƒ‚| }d	| v rH| d	 D ]}| d¡|krG|} nq:| d¡}|d
v rYt|d || ƒ‚t|d || ƒ‚)zBReturn getlasterror response as a dict, or raise OperationFailure.ÚwtimeoutFr4   Úerrr1   r7   Nr5   Ú
errObjectsr9   )rF   r=   r   Ú
startswithr   r	   r   )ÚresultÚ	error_msgrD   Úerrobjr1   r   r   r   Ú_check_gle_response¢   s,   þ

þ
rN   c                 C   s@   | d }|  d¡dkrt|  d¡d|ƒ‚t|  d¡|  d¡|ƒ‚)Néÿÿÿÿr1   r:   r4   )r=   r	   r   )Úwrite_errorsÚerrorr   r   r   Ú_raise_last_write_errorÅ   s   rR   c                 C   sF   d| v r| d   d¡rt|   d¡|   d¡| ƒ‚t|   d¡|   d¡| ƒ‚)NÚerrInforG   r4   r1   )r=   r   r   )rQ   r   r   r   r>   Î   s   ÿÿr>   c                 C   s4   |   d¡}|rt|ƒ |   d¡}|rt|ƒ dS dS )zDBackward compatibility helper for write command error handling.
    ÚwriteErrorsr2   N©r=   rR   r>   )rK   rP   rQ   r   r   r   Ú_check_write_command_response×   s   

ÿrV   c                 C   s*   |   d¡}|rt|ƒ t| d d ƒ dS )z=Backward compatibility helper for insert error handling.
    rT   ÚwriteConcernErrorsrO   NrU   )Úbulk_write_resultrP   r   r   r   Ú_raise_last_errorä   s   
rY   c                 C   s`   t | tjƒr| S t | tjtjfƒr)tdd„ | D ƒƒs#td|tjf ƒ‚t	 
| d¡S td|f ƒ‚)z»Takes 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)r   r   )r   Úfieldr   r   r   Ú	<genexpr>ü   s   € z'_fields_list_to_dict.<locals>.<genexpr>z6%s must be a list of key names, each an instance of %sé   z)%s must be a mapping or list of key names)r   r   r&   ÚSequenceÚSetÚallr"   r   Ú__name__ÚdictÚfromkeys)ÚfieldsÚoption_namer   r   r   Ú_fields_list_to_dictï   s   	ÿÿÿrf   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ÚIOError)Úeinfor   r   r   Ú_handle_exception  s   ÿýýøro   rZ   )NNF)%Ú__doc__rh   rk   Úbson.py3compatr   r   r   r   Úbson.sonr   Úpymongor   Úpymongo.errorsr   r	   r
   r   r   r   r   r   Ú	frozensetÚ_SHUTDOWN_CODESr?   Ú_RETRYABLE_ERROR_CODESr   r   r%   r/   rF   rN   rR   r>   rV   rY   rf   ro   r   r   r   r   Ú<module>   s:   (
þ
ú

ÿ9#		