3
]ð]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d	d
dddgƒeB Zeeddddgƒ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 j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   ú2/tmp/pip-build-20mum3z4/pymongo/pymongo/helpers.pyú
<listcomp>;   s    z#_gen_index_name.<locals>.<listcomp>)Ú_UUNDERÚjoin)Úkeysr   r   r   Ú_gen_index_name9   s    r   c             C   sD   |dk	r| |fgS t | tƒr&| tfgS t | ttfƒs<tdƒ‚| S 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)Zkey_or_listÚ	directionr   r   r   Ú_index_list>   s    


r$   c             C   s    t | tjƒr"tdtt| ƒƒ ƒ‚nt | ttfƒs@tdt| ƒ ƒ‚t| ƒsPt	dƒ‚t
ƒ }xD| D ]<\}}t |tƒsvtdƒ‚t |tttjfƒs�tdƒ‚|||< q\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 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)Z
index_listÚindexÚkeyÚvaluer   r   r   Ú_index_documentN   s     
r-   Fc             C   s`  d| kr t | jdƒ| jdƒ| ƒ‚|r8d| kr8t| d ƒ | d �s\| }d| kr€x0t| d ƒD ] }|jdƒr\|jdƒ r\|}P q\W |d }|dksš||k�r\|jdƒ}|tkr¸t|| ƒ‚nd|ksÈd	|krÒt|| ƒ‚|d
krüd|jddƒ }t ||jdƒ| ƒ‚|dk�rt||| ƒ‚n.|dk�r,t||| ƒ‚n|dk�rBt||| ƒ‚|�pJd}t || || ƒ‚dS )z0Check the response to a command for errors.
    Úokz$errÚcodeÚwriteConcernErrorÚrawÚerrmsgNz
not masterznode is recoveringzdb assertion failurez%db assertion failure, assertion: '%s'Z	assertionÚ ZassertionCodeéø*  éù*  é&1  é2   é+   z%s)r4   r5   r6   )	r   ÚgetÚ_raise_write_concern_errorr   Ú_NOT_MASTER_CODESr   r	   r
   r   )ÚresponseÚmsgZallowable_errorsZparse_write_concern_errorÚdetailsZshardr2   r/   r   r   r   Ú_check_command_responsei   sF    







r?   c             C   sÈ   t | ƒ | jddƒr4t| jd| jdƒƒ| jdƒ| ƒ‚| jddƒ}|dkrL| S |jdƒr`t|| ƒ‚| }d	| kr’x$| d	 D ]}|jdƒ|krv|}P qvW |jdƒ}|dkr´t|d || ƒ‚t|d || ƒ‚dS )zBReturn getlasterror response as a dict, or raise OperationFailure.ÚwtimeoutFr2   Úerrr/   r3   Nz
not masterZ
errObjectséø*  éù*  é&1  )rB   rC   rD   )r?   r9   r   Ú
startswithr   r	   r   )ÚresultZ	error_msgr>   Zerrobjr/   r   r   r   Ú_check_gle_response¢   s(    


rG   c             C   sD   | d }|j dƒdkr(t|j dƒd|ƒ‚t|j dƒ|j dƒ|ƒ‚d S )Né   r/   iø*  r2   éÿÿÿÿ)r9   r	   r   )Úwrite_errorsÚerrorr   r   r   Ú_raise_last_write_errorÅ   s    rL   c             C   sJ   d| kr.| d j dƒr.t| j dƒ| j dƒ| ƒ‚t| j dƒ| j dƒ| ƒ‚d S )NZerrInfor@   r2   r/   )r9   r   r   )rK   r   r   r   r:   Î   s
    r:   c             C   s0   | j dƒ}|rt|ƒ | j dƒ}|r,t|ƒ dS )zDBackward compatibility helper for write command error handling.
    ÚwriteErrorsr0   N)r9   rL   r:   )rF   rJ   rK   r   r   r   Ú_check_write_command_response×   s    

rN   c             C   s*   | j dƒ}|rt|ƒ t| d d ƒ dS )z=Backward compatibility helper for insert error handling.
    rM   ZwriteConcernErrorsrH   NrI   )r9   rL   r:   )Zbulk_write_resultrJ   r   r   r   Ú_raise_last_errorä   s    
rO   c             C   sd   t | tjƒr| S t | tjtjfƒrRtdd„ | D ƒƒsFtd|tjf ƒ‚t	j
| dƒS td|f ƒ‚dS )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 %srH   z)%s must be a mapping or list of key namesN)r   r   r%   ÚSequenceÚSetÚallr"   r   Ú__name__ÚdictÚfromkeys)ÚfieldsZoption_namer   r   r   Ú_fields_list_to_dictï   s    	rY   c              C   sV   t jrRt jƒ } z>y$tj| d | d | d dt jƒ W n tk
rH   Y nX W d~ X dS )z1Print exceptions raised by subscribers to stderr.r   rH   é   N)ÚsysÚstderrÚexc_infoÚ	tracebackÚprint_exceptionÚIOError)Zeinfor   r   r   Ú_handle_exception  s    
ra   )N)NNF)%Ú__doc__r[   r^   Zbson.py3compatr   r   r   r   Zbson.sonr   Zpymongor   Zpymongo.errorsr   r	   r
   r   r   r   r   r   Ú	frozensetZ_SHUTDOWN_CODESr;   Z_RETRYABLE_ERROR_CODESr   r   r$   r-   r?   rG   rL   r:   rN   rO   rY   ra   r   r   r   r   Ú<module>   sD   (


 
8#		