o
    sDi$                     @  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)_ServerMode)ClientSession)
Collection)CommandCursor)Database)
Connection)Server)_DocumentType	_PipelineTc                   @  sl   e Zd ZdZ				d3d4ddZed5ddZed6ddZd7d d!Zed8d#d$Z	d9d(d)Z
d:d1d2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.
    Ntarget%Union[Database[Any], Collection[Any]]cursor_classtype[CommandCursor[Any]]pipeliner   optionsMutableMapping[str, Any]letOptional[Mapping[str, Any]]user_fields"Optional[MutableMapping[str, Any]]result_processor9Optional[Callable[[Mapping[str, Any], Connection], 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 )NexplainzBThe explain option is not supported. Use Database.command instead.r   Fz$outz$mergeTr   r    r&   	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_user_fields_result_processorr   
_collation_max_await_time_ms_write_preference)	selfr   r   r   r   r    r"   r$   r&    r@   ]/home/alumno/antzosa8961/venv/lib/python3.10/site-packages/pymongo/synchronous/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@   rA   _aggregation_targetf      z'_AggregationCommand._aggregation_targetstrc                 C  rD   )z4The namespace in which the aggregate command is run.rE   rG   r@   r@   rA   _cursor_namespacek   rI   z%_AggregationCommand._cursor_namespace
cursor_docMapping[str, Any]Collection[Any]c                 C  rD   5The Collection used for the aggregate command cursor.rE   )r?   rL   r@   r@   rA   _cursor_collectionp      z&_AggregationCommand._cursor_collectionDatabase[Any]c                 C  rD   )z:The database against which the aggregation command is run.rE   rG   r@   r@   rA   	_databaset   rI   z_AggregationCommand._databasesessionOptional[ClientSession]!Union[_AggWritePref, _ServerMode]c                 C  s:   | j r| j S | j|}| jr|tjkrt| | _ }|S N)r>   r/   _read_preference_forr2   r   PRIMARYr   )r?   rU   prefr@   r@   rA   get_read_preferencey   s   z'_AggregationCommand.get_read_preferenceserverr   connr   read_preferencer   CommandCursor[_DocumentType]c                 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}| jrQ| || d|v rZ|d }	nd|d	g | jd
}	| j| |	|	|j| jprd| j|| jdd}
|
| |
S )N)	aggregater   readConcern   writeConcernT)parse_write_concern_errorread_concernwrite_concernr-   rU   clientr"   r,   r   result)id
firstBatchnsr&   )
batch_sizemax_await_time_msrU   r&   )rH   r1   updater4   r2   max_wire_versionr/   rf   _write_concern_forcommandrT   namecodec_optionsr<   rh   r:   r;   getrK   r9   rQ   addressr7   r=   _maybe_pin_connection)r?   rU   r]   r^   r_   cmdrf   rg   ri   r,   
cmd_cursorr@   r@   rA   
get_cursor   sV   





	z_AggregationCommand.get_cursor)NNNN)r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r   r'   r(   )r'   rC   r'   rJ   )rL   rM   r'   rN   r'   rS   )rU   rV   r'   rW   )
rU   rV   r]   r   r^   r   r_   r   r'   r`   )__name__
__module____qualname____doc__rB   propertyrH   rK   rQ   rT   r\   rz   r@   r@   r@   rA   r   '   s     7


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 )_CollectionAggregationCommandrN   r/   r'   rJ   c                 C     | j jS rX   r/   rs   rG   r@   r@   rA   rH         z1_CollectionAggregationCommand._aggregation_targetc                 C  r   rX   )r/   	full_namerG   r@   r@   rA   rK      r   z/_CollectionAggregationCommand._cursor_namespacer,   rM   c                 C     | j S rO   r/   )r?   r,   r@   r@   rA   rQ         z0_CollectionAggregationCommand._cursor_collectionrS   c                 C  r   rX   )r/   databaserG   r@   r@   rA   rT      r   z'_CollectionAggregationCommand._databaseNr{   r,   rM   r'   rN   r|   )	r}   r~   r   __annotations__r   rH   rK   rQ   rT   r@   r@   r@   rA   r      s   
 
r   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+   )superrB   r2   r4   )r?   r   r   	__class__r@   rA   rB      s   z)_CollectionRawAggregationCommand.__init__)r   r   r   r   r'   r(   )r}   r~   r   rB   __classcell__r@   r@   r   rA   r      s    r   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 )_DatabaseAggregationCommandrS   r/   r'   intc                 C  s   dS )N   r@   rG   r@   r@   rA   rH      rR   z/_DatabaseAggregationCommand._aggregation_targetrJ   c                 C  s   | j j dS )Nz.$cmd.aggregater   rG   r@   r@   rA   rK      s   z-_DatabaseAggregationCommand._cursor_namespacec                 C  r   rX   r   rG   r@   r@   rA   rT      r   z%_DatabaseAggregationCommand._databaser,   rM   rN   c                 C  s$   | d| jdd\}}| j| S )rP   rl   .r   )ru   rK   splitrT   )r?   r,   _collnamer@   r@   rA   rQ      s   
z._DatabaseAggregationCommand._cursor_collectionN)r'   r   r{   r|   r   )	r}   r~   r   r   r   rH   rK   rT   rQ   r@   r@   r@   rA   r      s   
 r   N)*r   
__future__r   collections.abcr   r   r   typingr   r   r   r	   pymongor
   pymongo.collationr   pymongo.errorsr   pymongo.read_preferencesr   r   r   "pymongo.synchronous.client_sessionr   pymongo.synchronous.collectionr   "pymongo.synchronous.command_cursorr   pymongo.synchronous.databaser   pymongo.synchronous.poolr   pymongo.synchronous.serverr   pymongo.typingsr   r   _IS_SYNCr   r   r   r   r@   r@   r@   rA   <module>   s.    &	