U
    Lk7g                     @   st   d Z ddlZddlmZmZ ddlmZ eejdd Z	e	dkrVej
jejjdZ
nejZ
ddd	ZdddZdS )zE
Helper functions, constants, and types to aid with PyMongo support.
    N)binary	json_util)OperationFailure      )Zuuid_representationc              
   C   s&  |dkrdS i }|dk	r ||d< |dk	r0||d< |dkr@||d< |dk	rP||d< t dkrz:|szt|d	hkrz| jf |W S | jf d
|i|W S W nF tk
r } z(t dkr dt|krdt|krȂ W 5 d}~X Y nX | |}| D ]\}	}
t||	}||
}qd|kpd|k}|j	|dS )z9Pymongo>3.7 deprecates count in favour of count_documentsr   Nskiplimit)Nhint	collation      Zmax_time_msfilterr   z@$geoNear, $near, and $nearSphere are not allowed in this contextz%$where is not allowed in this context)with_limit_and_skip)
PYMONGO_VERSIONsetZestimated_document_countcount_documentsr   strfinditemsgetattrcount)Z
collectionr   r   r	   r   r   kwargserrcursoroptionZoption_valueZcursor_methodr    r   ?/tmp/pip-unpacked-wheel-n1etwkgt/mongoengine/pymongo_support.pyr      s@    



r   Fc                 C   s0   t dkr|  }n|  }|s,dd |D }|S )zJPymongo>3.7 deprecates collection_names in favour of list_collection_namesr   c                 S   s   g | ]}| d s|qS )zsystem.)
startswith).0cr   r   r   
<listcomp>N   s     
 z)list_collection_names.<locals>.<listcomp>)r   list_collection_namesZcollection_names)dbZinclude_system_collectionscollectionsr   r   r   r$   F   s    
r$   )NNNN)F)__doc__ZpymongoZbsonr   r   Zpymongo.errorsr   tupleZversion_tupler   ZLEGACY_JSON_OPTIONSZwith_optionsZUuidRepresentationZPYTHON_LEGACYZDEFAULT_JSON_OPTIONSr   r$   r   r   r   r   <module>   s          
1