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
 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dlmZ ddlmZ ddlmZ dd Zdd Zdd Zdd Zdd Z dd Z!G dd de"Z#dS )z$Tools to parse mongo client options.    )_parse_codec_options)_build_credentials_tuple)validate_boolean)common)CompressionSettings)ConfigurationError)_EventListeners)PoolOptions)ReadConcern)make_read_preferenceread_pref_mode_from_name)any_server_selector)get_ssl_context)WriteConcernc             C   s<   |j d| rdnd}|j d}| s&|r8t||| |||S dS )z!Parse authentication credentials.ZauthmechanismDEFAULTNZ
authsource)getr   )usernamepassworddatabaseoptionsZ	mechanismsource r   U/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/pymongo/client_options.py_parse_credentials!   s    
r   c             C   sF   d| kr| d S | j dd}t|}| j d}| j dd}t|||S )zParse read preference options.read_preferenceZreadpreferenceZprimaryZreadpreferencetagsZmaxstalenessseconds   )r   r   r   )r   namemodetagsZmax_stalenessr   r   r   _parse_read_preference+   s    
r    c             C   s6   | j d}| j d}| j d}| j d}t||||S )zParse write concern options.wZ
wtimeoutmsZjournalfsync)r   r   )r   concernZwtimeoutjr"   r   r   r   _parse_write_concern7   s
    



r%   c             C   s   | j d}t|S )zParse read concern options.Zreadconcernlevel)r   r
   )r   r#   r   r   r   _parse_read_concern@   s    
r&   c          	      s    j d}|dk	rtd|  j d} j d} j d} j d} j d} j dd	} j d
} j dd	}	 fdd D }
|dkr|
rtddj|
 |
r|dkrd	}|d	krt||||||||	}||fS d|fS )zParse ssl options.sslNZssl_certfileZssl_keyfileZssl_pem_passphraseZssl_ca_certsZssl_cert_reqsssl_match_hostnameTZssl_crlfileZssl_check_ocsp_endpointc                s"   g | ]}|j d r | r|qS )ssl_)
startswith).0k)r   r   r   
<listcomp>U   s    z&_parse_ssl_options.<locals>.<listcomp>Fzmssl has not been enabled but the following ssl parameters have been set: %s. Please set `ssl=True` or remove.z, )r   r   r   joinr   )r   Zuse_sslcertfilekeyfileZ
passphraseca_certs	cert_reqsmatch_hostnameZcrlfileZcheck_ocsp_endpointZssl_kwarg_keysctxr   )r   r   _parse_ssl_optionsF   s:    







r5   c             C   s  | j dtj}| j dtj}| j dtj}|dk	rB||krBtd| j dtj}| j dd}| j d	}| j d
tj}| j d}| j d}	| j d}
| j d}| j d}t| j dg | j dd}t	| \}}| j d}t
||||||||||t|	|
||||dS )zParse connection pool options.ZmaxpoolsizeZminpoolsizeZmaxidletimemsNz3minPoolSize must be smaller or equal to maxPoolSizeZconnecttimeoutmsZsocketkeepaliveTZsockettimeoutmsZwaitqueuetimeoutmsZwaitqueuemultipleevent_listenersappnamedriver
server_apiZcompressorsZzlibcompressionlevelr   loadbalanced)r9   load_balancedr   )r   r   ZMAX_POOL_SIZEZMIN_POOL_SIZEZMAX_IDLE_TIME_SEC
ValueErrorZCONNECT_TIMEOUTZWAIT_QUEUE_TIMEOUTr   r5   r	   r   )r   Zmax_pool_sizeZmin_pool_sizeZmax_idle_time_secondsconnect_timeoutZsocket_keepalivesocket_timeoutZwait_queue_timeoutZwait_queue_multipler6   r7   r8   r9   Zcompression_settingsssl_contextr(   r;   r   r   r   _parse_pool_optionso   sB    









r@   c               @   s   e Zd Zd Zdd Zedd Zedd Zedd Zed	d
 Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd  Zed!d" Zed#d$ Zed%d& Zd'S )(ClientOptionsc             C   s   || _ t|| _t||||| _|jd| _|jdtj| _	|jdtj
| _t|| _t|| _|jd| _t|| _t|| _|jd| _|jdtj| _|jdtj| _|jdtj| _|jd	t| _|jd
| _|jd| _d S )NZdirectconnectionZlocalthresholdmsZserverselectiontimeoutmsZ
replicasetconnectZheartbeatfrequencymsZretrywritesZ
retryreadsserver_selectorauto_encryption_optsr:   ) _ClientOptions__optionsr   _ClientOptions__codec_optionsr   _ClientOptions__credentialsr   !_ClientOptions__direct_connectionr   ZLOCAL_THRESHOLD_MS"_ClientOptions__local_threshold_msZSERVER_SELECTION_TIMEOUT(_ClientOptions__server_selection_timeoutr@   _ClientOptions__pool_optionsr    _ClientOptions__read_preference _ClientOptions__replica_set_namer%   _ClientOptions__write_concernr&   _ClientOptions__read_concern_ClientOptions__connectZHEARTBEAT_FREQUENCY#_ClientOptions__heartbeat_frequencyZRETRY_WRITES_ClientOptions__retry_writesZRETRY_READS_ClientOptions__retry_readsr   _ClientOptions__server_selector$_ClientOptions__auto_encryption_opts_ClientOptions__load_balanced)selfr   r   r   r   r   r   r   __init__   s.    





zClientOptions.__init__c             C   s   | j S )z7The original options used to create this ClientOptions.)rE   )rW   r   r   r   _options   s    zClientOptions._optionsc             C   s   | j S )z>Whether to begin discovering a MongoDB topology automatically.)rP   )rW   r   r   r   rB      s    zClientOptions.connectc             C   s   | j S )z5A :class:`~bson.codec_options.CodecOptions` instance.)rF   )rW   r   r   r   codec_options   s    zClientOptions.codec_optionsc             C   s   | j S )z;A :class:`~pymongo.auth.MongoCredentials` instance or None.)rG   )rW   r   r   r   credentials   s    zClientOptions.credentialsc             C   s   | j S )z:Whether to connect to the deployment in 'Single' topology.)rH   )rW   r   r   r   direct_connection   s    zClientOptions.direct_connectionc             C   s   | j S )z&The local threshold for this instance.)rI   )rW   r   r   r   local_threshold_ms   s    z ClientOptions.local_threshold_msc             C   s   | j S )z:The server selection timeout for this instance in seconds.)rJ   )rW   r   r   r   server_selection_timeout   s    z&ClientOptions.server_selection_timeoutc             C   s   | j S )N)rT   )rW   r   r   r   rC      s    zClientOptions.server_selectorc             C   s   | j S )z$The monitoring frequency in seconds.)rQ   )rW   r   r   r   heartbeat_frequency   s    z!ClientOptions.heartbeat_frequencyc             C   s   | j S )z.A :class:`~pymongo.pool.PoolOptions` instance.)rK   )rW   r   r   r   pool_options   s    zClientOptions.pool_optionsc             C   s   | j S )zA read preference instance.)rL   )rW   r   r   r   r      s    zClientOptions.read_preferencec             C   s   | j S )zReplica set name or None.)rM   )rW   r   r   r   replica_set_name   s    zClientOptions.replica_set_namec             C   s   | j S )z8A :class:`~pymongo.write_concern.WriteConcern` instance.)rN   )rW   r   r   r   write_concern   s    zClientOptions.write_concernc             C   s   | j S )z6A :class:`~pymongo.read_concern.ReadConcern` instance.)rO   )rW   r   r   r   read_concern   s    zClientOptions.read_concernc             C   s   | j S )z9If this instance should retry supported write operations.)rR   )rW   r   r   r   retry_writes   s    zClientOptions.retry_writesc             C   s   | j S )z8If this instance should retry supported read operations.)rS   )rW   r   r   r   retry_reads   s    zClientOptions.retry_readsc             C   s   | j S )z:A :class:`~pymongo.encryption.AutoEncryptionOpts` or None.)rU   )rW   r   r   r   rD     s    z"ClientOptions.auto_encryption_optsc             C   s   | j S )z@True if the client was configured to connect to a load balancer.)rV   )rW   r   r   r   r;     s    zClientOptions.load_balancedN)__name__
__module____qualname____doc__rX   propertyrY   rB   rZ   r[   r\   r]   r^   rC   r_   r`   r   ra   rb   rc   rd   re   rD   r;   r   r   r   r   rA      s(   rA   N)$ri   Zbson.codec_optionsr   Zpymongo.authr   Zpymongo.commonr   Zpymongor   Zpymongo.compression_supportr   Zpymongo.errorsr   Zpymongo.monitoringr   Zpymongo.poolr	   Zpymongo.read_concernr
   Zpymongo.read_preferencesr   r   Zpymongo.server_selectorsr   Zpymongo.ssl_supportr   Zpymongo.write_concernr   r   r    r%   r&   r5   r@   objectrA   r   r   r   r   <module>   s(   
	)%