o
    h%                     @  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rld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)AsyncClientSession)AsyncCollection)AsyncCommandCursor)AsyncDatabase)AsyncConnection)Server)_ServerMode)_DocumentType	_PipelineFc                   @  sl   e Zd ZdZ				d5d6ddZed7ddZed8ddZd9d"d#Zed:d%d&Z	d;d*d+Z
d<d3d4ZdS )=_AggregationCommanda  The internal abstract base class for aggregation cursors.

    Should not be called directly by application developers. Use
    :meth:`pymongo.asynchronous.collection.AsyncCollection.aggregate`, or
    :meth:`pymongo.asynchronous.database.AsyncDatabase.aggregate` instead.
    Ntarget%Union[AsyncDatabase, AsyncCollection]cursor_classtype[AsyncCommandCursor]pipeliner   optionsMutableMapping[str, Any]explicit_sessionboolletOptional[Mapping[str, Any]]user_fields"Optional[MutableMapping[str, Any]]result_processor>Optional[Callable[[Mapping[str, Any], AsyncConnection], None]]commentr   returnNonec
           
      C  s  d|v rt d|| _td|}|| _d| _|r(d|d v s%d|d v r(d| _td	| |d ur<td
| ||d
< |	d urD|	|d< || _td| j	dd | _
| jdi  | j
d urj| jsj| j
| jd d< || _|| _|| _|| _t|	dd | _|	dd | _d | _d S )NZexplainzGThe explain option is not supported. Use AsyncDatabase.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(    r>   S/var/www/html/venv/lib/python3.10/site-packages/pymongo/asynchronous/aggregation.py__init__/   s<   
z_AggregationCommand.__init__Union[str, int]c                 C     t )z.The argument to pass to the aggregate command.NotImplementedErrorr=   r>   r>   r?   _aggregation_targeth      z'_AggregationCommand._aggregation_targetstrc                 C  rB   )z4The namespace in which the aggregate command is run.rC   rE   r>   r>   r?   _cursor_namespacem   rG   z%_AggregationCommand._cursor_namespace
cursor_docMapping[str, Any]r   c                 C  rB   :The AsyncCollection used for the aggregate command cursor.rC   )r=   rJ   r>   r>   r?   _cursor_collectionr      z&_AggregationCommand._cursor_collectionr   c                 C  rB   )z:The database against which the aggregation command is run.rC   rE   r>   r>   r?   	_databasev   rG   z_AggregationCommand._databasesessionOptional[AsyncClientSession]!Union[_AggWritePref, _ServerMode]c                 C  s:   | j r| j S | j|}| jr|tjkrt| | _ }|S N)r<   r/   Z_read_preference_forr1   r   ZPRIMARYr   )r=   rQ   Zprefr>   r>   r?   get_read_preference{   s   z'_AggregationCommand.get_read_preferenceserverr   connr   read_preferencer   !AsyncCommandCursor[_DocumentType]c                   s  | j | jd}|| j d|vr| jr|jdkr| jj}nd }d|vr/| jr/| j|}nd }|j	| j
j||| jjd||| j|| j
j| jdI d H }| jrU| || d|v r^|d }	nd|d	g | jd
}	| j| |	|	|j| jpvd| j|| j| jdd}
|
|I d H  |
S )N)Z	aggregater   ZreadConcern   ZwriteConcernT)Zparse_write_concern_errorread_concernwrite_concernr.   rQ   clientr$   r-   r   result)idZ
firstBatchnsr(   )Z
batch_sizeZmax_await_time_msrQ   r    r(   )rF   r0   updater2   r1   Zmax_wire_versionr/   r[   Z_write_concern_forcommandrP   nameZcodec_optionsr:   r]   r8   r9   getrI   r6   rN   addressr4   r;   r7   Z_maybe_pin_connection)r=   rQ   rV   rW   rX   cmdr[   r\   r^   r-   Z
cmd_cursorr>   r>   r?   
get_cursor   sZ   





z_AggregationCommand.get_cursor)NNNN)r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r   r)   r*   )r)   rA   r)   rH   )rJ   rK   r)   r   r)   r   )rQ   rR   r)   rS   )
rQ   rR   rV   r   rW   r   rX   r   r)   rY   )__name__
__module____qualname____doc__r@   propertyrF   rI   rN   rP   rU   rg   r>   r>   r>   r?   r   '   s     9


r   c                   @  sJ   e Zd ZU ded< edddZedddZdddZedddZdS )_CollectionAggregationCommandr   r/   r)   rH   c                 C     | j jS rT   r/   rc   rE   r>   r>   r?   rF         z1_CollectionAggregationCommand._aggregation_targetc                 C  rp   rT   )r/   Z	full_namerE   r>   r>   r?   rI      rr   z/_CollectionAggregationCommand._cursor_namespacer-   rK   c                 C     | j S rL   r/   )r=   r-   r>   r>   r?   rN         z0_CollectionAggregationCommand._cursor_collectionr   c                 C  rp   rT   )r/   ZdatabaserE   r>   r>   r?   rP      rr   z'_CollectionAggregationCommand._databaseNrh   r-   rK   r)   r   ri   )	rj   rk   rl   __annotations__rn   rF   rI   rN   rP   r>   r>   r>   r?   ro      s   
 
ro   c                      s   e Zd Zd fddZ  ZS )	 _CollectionRawAggregationCommandargsr   kwargsr)   r*   c                   s.   t  j|i | | jsd| jd d< d S d S )Nr   r-   r,   )superr@   r1   r2   )r=   ry   rz   	__class__r>   r?   r@      s   z)_CollectionRawAggregationCommand.__init__)ry   r   rz   r   r)   r*   )rj   rk   rl   r@   __classcell__r>   r>   r|   r?   rx      s    rx   c                   @  sJ   e Zd ZU ded< edddZeddd	Zedd
dZdddZdS )_DatabaseAggregationCommandr   r/   r)   intc                 C  s   dS )N   r>   rE   r>   r>   r?   rF      rO   z/_DatabaseAggregationCommand._aggregation_targetrH   c                 C  s   | j j dS )Nz.$cmd.aggregaterq   rE   r>   r>   r?   rI      s   z-_DatabaseAggregationCommand._cursor_namespacec                 C  rs   rT   rt   rE   r>   r>   r?   rP      ru   z%_DatabaseAggregationCommand._databaser-   rK   r   c                 C  s$   | d| jdd\}}| j| S )rM   r`   .r   )rd   rI   splitrP   )r=   r-   _Zcollnamer>   r>   r?   rN      s   
z._DatabaseAggregationCommand._cursor_collectionN)r)   r   rh   ri   rv   )	rj   rk   rl   rw   rn   rF   rI   rP   rN   r>   r>   r>   r?   r      s   
 r   N)*rm   
__future__r   collections.abcr   r   r   typingr   r   r   r	   Zpymongor
   Zpymongo.collationr   Zpymongo.errorsr   Zpymongo.read_preferencesr   r   Z#pymongo.asynchronous.client_sessionr   Zpymongo.asynchronous.collectionr   Z#pymongo.asynchronous.command_cursorr   Zpymongo.asynchronous.databaser   Zpymongo.asynchronous.poolr   Zpymongo.asynchronous.serverr   r   Zpymongo.typingsr   r   Z_IS_SYNCr   ro   rx   r   r>   r>   r>   r?   <module>   s.    )	