U
    ~fh$                     @  s  d Z ddlmZ ddlmZmZm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 ddlmZmZ erdd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z#m$Z$ dZ%G dd dZ&G dd de&Z'G dd de'Z(G dd de&Z)dS )z;Perform aggregation operations on a collection or database.    )annotations)CallableMappingMutableMapping)TYPE_CHECKINGAnyOptionalUnion)common)validate_collation_or_none)ConfigurationError)ReadPreference_AggWritePref)_ServerMode)ClientSession)
Collection)CommandCursor)Database)
Connection)Server)_DocumentType	_PipelineTc                   @  s   e Zd ZdZd+ddddddd	d
ddd
ddZeddddZeddddZdddddZeddddZ	dd d!d"d#Z
dd$d%d&d'd(d)d*ZdS ),_AggregationCommandzThe internal abstract base class for aggregation cursors.

    Should not be called directly by application developers. Use
    :meth:`pymongo.collection.Collection.aggregate`, or
    :meth:`pymongo.database.Database.aggregate` instead.
    NzUnion[Database, Collection]ztype[CommandCursor]r   zMutableMapping[str, Any]boolzOptional[Mapping[str, Any]]z"Optional[MutableMapping[str, Any]]z9Optional[Callable[[Mapping[str, Any], Connection], None]]r   None)
targetcursor_classpipelineoptionsexplicit_sessionletuser_fieldsresult_processorcommentreturnc
           
      C  s  d|krt d|| _td|}|| _d| _|rPd|d ksJd|d krPd| _td	| |d k	rxtd
| ||d
< |	d k	r|	|d< || _td| j	dd | _
| jdi  | j
d k	r| js| j
| jd d< || _|| _|| _|| _t|	dd | _|	dd | _d | _d S )NexplainzBThe explain option is not supported. Use Database.command instead.r   Fz$outz$mergeTr   r    r#   	batchSizecursor	collationZmaxAwaitTimeMS)r   _targetr
   Zvalidate_list	_pipeline_performs_writeZvalidate_is_mapping_optionsZ%validate_non_negative_integer_or_nonepop_batch_size
setdefault_cursor_class_explicit_session_user_fields_result_processorr   
_collation_max_await_time_ms_write_preference)
selfr   r   r   r   r   r    r!   r"   r#    r9   C/tmp/pip-unpacked-wheel-36gvocj8/pymongo/synchronous/aggregation.py__init__/   s>     z_AggregationCommand.__init__zUnion[str, int]r$   c                 C  s   t dS )z.The argument to pass to the aggregate command.NNotImplementedErrorr8   r9   r9   r:   _aggregation_targeth   s    z'_AggregationCommand._aggregation_targetstrc                 C  s   t dS )z4The namespace in which the aggregate command is run.Nr=   r?   r9   r9   r:   _cursor_namespacem   s    z%_AggregationCommand._cursor_namespaceMapping[str, Any]r   )
cursor_docr$   c                 C  s   t dS )5The Collection used for the aggregate command cursor.Nr=   )r8   rD   r9   r9   r:   _cursor_collectionr   s    z&_AggregationCommand._cursor_collectionr   c                 C  s   t dS )z:The database against which the aggregation command is run.Nr=   r?   r9   r9   r:   	_databasev   s    z_AggregationCommand._databasezOptional[ClientSession]z!Union[_AggWritePref, _ServerMode])sessionr$   c                 C  s:   | j r| j S | j|}| jr6|tjkr6t| | _ }|S N)r7   r*   Z_read_preference_forr,   r   ZPRIMARYr   )r8   rH   Zprefr9   r9   r:   get_read_preference{   s    z'_AggregationCommand.get_read_preferencer   r   r   zCommandCursor[_DocumentType])rH   serverconnread_preferencer$   c                 C  s  | j | jd}|| j d|kr<| jr2|jdkr<| jj}nd }d|kr\| jr\| j|}nd }|j	| j
j||| jjd||| j|| j
j| jd}| jr| || d|kr|d }	nd|d	g | jd
}	| j| |	|	|j| jpd| j|| j| jdd}
|
| |
S )N)Z	aggregater   ZreadConcern   ZwriteConcernT)Zparse_write_concern_errorread_concernwrite_concernr)   rH   clientr!   r(   r   result)idZ
firstBatchnsr#   )Z
batch_sizeZmax_await_time_msrH   r   r#   )r@   r+   updater-   r,   Zmax_wire_versionr*   rO   Z_write_concern_forcommandrG   nameZcodec_optionsr5   rQ   r3   r4   getrB   r1   rF   addressr/   r6   r2   Z_maybe_pin_connection)r8   rH   rK   rL   rM   cmdrO   rP   rR   r(   Z
cmd_cursorr9   r9   r:   
get_cursor   sZ    





z_AggregationCommand.get_cursor)NNNN)__name__
__module____qualname____doc__r;   propertyr@   rB   rF   rG   rJ   r[   r9   r9   r9   r:   r   '   s       "9
r   c                   @  s\   e Zd ZU ded< eddddZeddddZd	dd
ddZeddddZdS )_CollectionAggregationCommandr   r*   rA   r<   c                 C  s   | j jS rI   r*   rW   r?   r9   r9   r:   r@      s    z1_CollectionAggregationCommand._aggregation_targetc                 C  s   | j jS rI   )r*   Z	full_namer?   r9   r9   r:   rB      s    z/_CollectionAggregationCommand._cursor_namespacerC   r(   r$   c                 C  s   | j S )rE   r*   )r8   r(   r9   r9   r:   rF      s    z0_CollectionAggregationCommand._cursor_collectionr   c                 C  s   | j jS rI   )r*   Zdatabaser?   r9   r9   r:   rG      s    z'_CollectionAggregationCommand._databaseN)	r\   r]   r^   __annotations__r`   r@   rB   rF   rG   r9   r9   r9   r:   ra      s   
ra   c                      s&   e Zd Zdddd fddZ  ZS ) _CollectionRawAggregationCommandr   r   )argskwargsr$   c                   s&   t  j|| | js"d| jd d< d S )Nr   r(   r'   )superr;   r,   r-   )r8   rg   rh   	__class__r9   r:   r;      s    z)_CollectionRawAggregationCommand.__init__)r\   r]   r^   r;   __classcell__r9   r9   rj   r:   rf      s   rf   c                   @  s\   e Zd ZU ded< eddddZedddd	Zeddd
dZdddddZdS )_DatabaseAggregationCommandr   r*   intr<   c                 C  s   dS )N   r9   r?   r9   r9   r:   r@      s    z/_DatabaseAggregationCommand._aggregation_targetrA   c                 C  s   | j j dS )Nz.$cmd.aggregaterb   r?   r9   r9   r:   rB      s    z-_DatabaseAggregationCommand._cursor_namespacec                 C  s   | j S rI   rd   r?   r9   r9   r:   rG      s    z%_DatabaseAggregationCommand._databaserC   r   rc   c                 C  s$   | d| jdd\}}| j| S )rE   rT   .ro   )rX   rB   splitrG   )r8   r(   _Zcollnamer9   r9   r:   rF      s    z._DatabaseAggregationCommand._cursor_collectionN)	r\   r]   r^   re   r`   r@   rB   rG   rF   r9   r9   r9   r:   rm      s   
rm   N)*r_   
__future__r   collections.abcr   r   r   typingr   r   r   r	   Zpymongor
   Zpymongo.collationr   Zpymongo.errorsr   Zpymongo.read_preferencesr   r   r   Z"pymongo.synchronous.client_sessionr   Zpymongo.synchronous.collectionr   Z"pymongo.synchronous.command_cursorr   Zpymongo.synchronous.databaser   Zpymongo.synchronous.poolr   Zpymongo.synchronous.serverr   Zpymongo.typingsr   r   Z_IS_SYNCr   ra   rf   rm   r9   r9   r9   r:   <module>   s,    )	