o
    Åîï]\"  ã                   @   s„   d 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
 G dd„ deƒ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.é    )ÚSON)Úcommon)Úvalidate_collation_or_none)ÚConfigurationError)ÚReadPreferencec                   @   sp   e Zd ZdZ	ddd„Zedd„ ƒZedd„ ƒZed	d
„ ƒZedd„ ƒZ	e
dd„ ƒZdd„ Zdd„ Zdd„ ZdS )Ú_AggregationCommandzñThe 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.
    Nc                 C   sä   d|v rt dƒ‚|| _t d|¡ || _d| _|r(d|d v s%d|d v r(d| _t d	|¡ || _t d
| j 	d
d ¡¡| _
| j di ¡ | j
d urT| jsT| j
| jd d
< || _|| _|| _|| _t| 	dd ¡ƒ| _| 	dd ¡| _d S )NÚexplainzBThe explain option is not supported. Use Database.command instead.ÚpipelineFz$outéÿÿÿÿz$mergeTÚoptionsÚ	batchSizeÚcursorÚ	collationÚmaxAwaitTimeMS)r   Ú_targetr   Úvalidate_listÚ	_pipelineÚ_performs_writeÚvalidate_is_mappingÚ_optionsÚ%validate_non_negative_integer_or_noneÚpopÚ_batch_sizeÚ
setdefaultÚ_cursor_classÚ_explicit_sessionÚ_user_fieldsÚ_result_processorr   Ú
_collationÚ_max_await_time_ms)ÚselfÚtargetÚcursor_classr	   r   Úexplicit_sessionÚuser_fieldsÚresult_processor© r&   úE/var/www/html/env/lib/python3.10/site-packages/pymongo/aggregation.pyÚ__init__    s0   ÿ
ÿz_AggregationCommand.__init__c                 C   ó   t ‚)z.The argument to pass to the aggregate command.©ÚNotImplementedError©r    r&   r&   r'   Ú_aggregation_targetG   ó   z'_AggregationCommand._aggregation_targetc                 C   r)   )z4The namespace in which the aggregate command is run.r*   r,   r&   r&   r'   Ú_cursor_namespaceL   r.   z%_AggregationCommand._cursor_namespacec                 C   r)   ©ú5The Collection used for the aggregate command cursor.r*   )r    Ú
cursor_docr&   r&   r'   Ú_cursor_collectionQ   r.   z&_AggregationCommand._cursor_collectionc                 C   r)   )z:The database against which the aggregation command is run.r*   r,   r&   r&   r'   Ú	_databaseV   r.   z_AggregationCommand._databasec                 C   ó   dS )z=Check whether the server version in-use supports aggregation.Nr&   )Ú	sock_infor&   r&   r'   Ú_check_compat[   r.   z!_AggregationCommand._check_compatc                 C   s    | j r|   |||||¡ d S d S ©N)r   )r    ÚresultÚsessionÚserverr6   Úslave_okr&   r&   r'   Ú_process_result`   s
   
ÿÿz#_AggregationCommand._process_resultc                 C   s   | j rtjS | j |¡S r8   )r   r   ÚPRIMARYr   Ú_read_preference_for)r    r:   r&   r&   r'   Úget_read_preferencee   s   z'_AggregationCommand.get_read_preferencec           
      C   s  |   |¡ td| jfd| jfgƒ}| | j¡ d|vr-|jdk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�}|  |||||¡ d	|v rm|d	 }	nd
| dg ¡| jdœ}	| j|  |	¡|	|j| jp…d
| j|| jd�S )NÚ	aggregater	   ÚreadConcerné   é   ÚwriteConcernT)Úparse_write_concern_errorÚread_concernÚwrite_concernr   r:   Úclientr$   r   r   r9   )ÚidÚ
firstBatchÚns)Ú
batch_sizeÚmax_await_time_msr:   r#   )r7   r   r-   r   Úupdater   Úmax_wire_versionr   r   rG   Ú_write_concern_forÚcommandr4   Únamer@   Úcodec_optionsr   rI   r   r=   Úgetr/   r   r3   Úaddressr   r   r   )
r    r:   r;   r6   r<   ÚcmdrG   rH   r9   r   r&   r&   r'   Ú
get_cursorj   sT   

ÿ
ÿ

ô

ýüz_AggregationCommand.get_cursor)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r(   Úpropertyr-   r/   r3   r4   Ústaticmethodr7   r=   r@   rX   r&   r&   r&   r'   r      s"    
ÿ'




r   c                       sH   e Zd Z‡ fdd„Zedd„ ƒZedd„ ƒZdd„ Zed	d
„ ƒZ‡  Z	S )Ú_CollectionAggregationCommandc                    sD   |  dd¡}tt| ƒj|i |¤Ž || _| js | j  dd ¡ d S d S )NÚ
use_cursorTr   )r   Úsuperr_   r(   Ú_use_cursorr   )r    ÚargsÚkwargsr`   ©Ú	__class__r&   r'   r(   ¬   s   ÿz&_CollectionAggregationCommand.__init__c                 C   ó   | j jS r8   ©r   rS   r,   r&   r&   r'   r-   ¶   ó   z1_CollectionAggregationCommand._aggregation_targetc                 C   rg   r8   )r   Ú	full_namer,   r&   r&   r'   r/   º   ri   z/_CollectionAggregationCommand._cursor_namespacec                 C   ó   | j S r0   ©r   )r    r   r&   r&   r'   r3   ¾   ó   z0_CollectionAggregationCommand._cursor_collectionc                 C   rg   r8   )r   Údatabaser,   r&   r&   r'   r4   Â   ri   z'_CollectionAggregationCommand._database)
rY   rZ   r[   r(   r]   r-   r/   r3   r4   Ú__classcell__r&   r&   re   r'   r_   «   s    


r_   c                       s   e Zd Z‡ fdd„Z‡  ZS )Ú _CollectionRawAggregationCommandc                    s<   t t| ƒj|i |¤Ž | jr| jsd| jd d< d S d S d S )Nr   r   r   )ra   rp   r(   rb   r   r   )r    rc   rd   re   r&   r'   r(   È   s   ÿz)_CollectionRawAggregationCommand.__init__)rY   rZ   r[   r(   ro   r&   r&   re   r'   rp   Ç   s    rp   c                   @   sD   e Zd Zedd„ ƒZedd„ ƒZedd„ ƒZdd„ Zed	d
„ ƒZ	dS )Ú_DatabaseAggregationCommandc                 C   r5   )Né   r&   r,   r&   r&   r'   r-   Ñ   s   z/_DatabaseAggregationCommand._aggregation_targetc                 C   s   d| j jf S )Nz%s.$cmd.aggregaterh   r,   r&   r&   r'   r/   Õ   s   z-_DatabaseAggregationCommand._cursor_namespacec                 C   rk   r8   rl   r,   r&   r&   r'   r4   Ù   rm   z%_DatabaseAggregationCommand._databasec                 C   s$   |  d| j¡ dd¡\}}| j| S )r1   rL   Ú.rr   )rU   r/   Úsplitr4   )r    r   Ú_Úcollnamer&   r&   r'   r3   Ý   s   
z._DatabaseAggregationCommand._cursor_collectionc                 C   s   | j dksd}t|ƒ‚d S )Né   z7Database.aggregate() is only supported on MongoDB 3.6+.)rP   r   )r6   Úerr_msgr&   r&   r'   r7   å   s   
þz)_DatabaseAggregationCommand._check_compatN)
rY   rZ   r[   r]   r-   r/   r4   r3   r^   r7   r&   r&   r&   r'   rq   Ð   s    


rq   N)r\   Úbson.sonr   Úpymongor   Úpymongo.collationr   Úpymongo.errorsr   Úpymongo.read_preferencesr   Úobjectr   r_   rp   rq   r&   r&   r&   r'   Ú<module>   s    	