3
O6b"                 @   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               @   sn   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 )_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.
    Nc             C   s   d|krt d|| _tjd| || _d| _|rPd|d ksJd|d krPd| _tjd	| || _tjd
| jj	d
d | _
| jjdi  | j
d k	r| j r| j
| jd d
< || _|| _|| _|| _t|j	dd | _|j	dd | _d S )NexplainzBThe explain option is not supported. Use Database.command instead.pipelineFz$out   z$mergeToptions	batchSizecursor	collationZmaxAwaitTimeMSr   )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)selftargetZcursor_classr	   r   explicit_sessionuser_fieldsZresult_processor r!   R/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/pymongo/aggregation.py__init__    s,    z_AggregationCommand.__init__c             C   s   t dS )z.The argument to pass to the aggregate command.N)NotImplementedError)r   r!   r!   r"   _aggregation_targetG   s    z'_AggregationCommand._aggregation_targetc             C   s   t dS )z4The namespace in which the aggregate command is run.N)r$   )r   r!   r!   r"   _cursor_namespaceL   s    z%_AggregationCommand._cursor_namespacec             C   s   t dS )z5The Collection used for the aggregate command cursor.N)r$   )r   Z
cursor_docr!   r!   r"   _cursor_collectionQ   s    z&_AggregationCommand._cursor_collectionc             C   s   t dS )z:The database against which the aggregation command is run.N)r$   )r   r!   r!   r"   	_databaseV   s    z_AggregationCommand._databasec             C   s   dS )z=Check whether the server version in-use supports aggregation.Nr!   )	sock_infor!   r!   r"   _check_compat[   s    z!_AggregationCommand._check_compatc             C   s   | j r| j ||||| d S )N)r   )r   resultsessionserverr)   secondary_okr!   r!   r"   _process_result`   s    z#_AggregationCommand._process_resultc             C   s   | j rtjS | jj|S )N)r   r   ZPRIMARYr   Z_read_preference_for)r   r,   r!   r!   r"   get_read_preferencef   s    z'_AggregationCommand.get_read_preferencec             C   s,  | j | td| jfd| jfg}|j| j d|kr\|jdkrH| j sR|jdkr\| jj	}nd }d|kr|| jr|| jj
|}nd }|j| jj||| j|| jjd||| j|| jj| jd}| j||||| d	|kr|d	 }	nd
|jdg | jd}	| j| j|	|	|j| jpd
| j|| jd}
|
j| |
S )NZ	aggregater	   ZreadConcern      ZwriteConcernT)Zparse_write_concern_errorread_concernwrite_concernr   r,   clientr    r   r   r+   )idZ
firstBatchns)Z
batch_sizeZmax_await_time_msr,   r   )r*   r   r%   r   updater   max_wire_versionr   r   r3   Z_write_concern_forcommandr(   namer0   Zcodec_optionsr   r5   r   r/   getr&   r   r'   addressr   r   r   Z_maybe_pin_connection)r   r,   r-   r)   r.   cmdr3   r4   r+   r   Z
cmd_cursorr!   r!   r"   
get_cursork   sN    










z_AggregationCommand.get_cursor)NN)__name__
__module____qualname____doc__r#   propertyr%   r&   r'   r(   staticmethodr*   r/   r0   r?   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                s<   |j dd}tt| j|| || _| js8| jj dd  d S )N
use_cursorTr   )r   superrF   r#   _use_cursorr   )r   argskwargsrG   )	__class__r!   r"   r#      s
    z&_CollectionAggregationCommand.__init__c             C   s   | j jS )N)r   r;   )r   r!   r!   r"   r%      s    z1_CollectionAggregationCommand._aggregation_targetc             C   s   | j jS )N)r   Z	full_name)r   r!   r!   r"   r&      s    z/_CollectionAggregationCommand._cursor_namespacec             C   s   | j S )z5The Collection used for the aggregate command cursor.)r   )r   r   r!   r!   r"   r'      s    z0_CollectionAggregationCommand._cursor_collectionc             C   s   | j jS )N)r   Zdatabase)r   r!   r!   r"   r(      s    z'_CollectionAggregationCommand._database)
r@   rA   rB   r#   rD   r%   r&   r'   r(   __classcell__r!   r!   )rL   r"   rF      s
   
rF   c                   s   e Zd Z fddZ  ZS ) _CollectionRawAggregationCommandc                s2   t t| j|| | jr.| j r.d| jd d< d S )Nr   r   r   )rH   rN   r#   rI   r   r   )r   rJ   rK   )rL   r!   r"   r#      s    z)_CollectionRawAggregationCommand.__init__)r@   rA   rB   r#   rM   r!   r!   )rL   r"   rN      s   rN   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   s   dS )Nr
   r!   )r   r!   r!   r"   r%      s    z/_DatabaseAggregationCommand._aggregation_targetc             C   s   d| j jf S )Nz%s.$cmd.aggregate)r   r;   )r   r!   r!   r"   r&      s    z-_DatabaseAggregationCommand._cursor_namespacec             C   s   | j S )N)r   )r   r!   r!   r"   r(      s    z%_DatabaseAggregationCommand._databasec             C   s$   |j d| jjdd\}}| j| S )z5The Collection used for the aggregate command cursor.r7   .r
   )r<   r&   splitr(   )r   r   _Zcollnamer!   r!   r"   r'      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+.)r9   r   )r)   err_msgr!   r!   r"   r*      s    
z)_DatabaseAggregationCommand._check_compatN)
r@   rA   rB   rD   r%   r&   r(   r'   rE   r*   r!   r!   r!   r"   rO      s
   rO   N)rC   Zbson.sonr   Zpymongor   Zpymongo.collationr   Zpymongo.errorsr   Zpymongo.read_preferencesr   objectr   rF   rN   rO   r!   r!   r!   r"   <module>   s    	