U
    ~fh2                     @  s  d Z ddlmZ ddlmZmZ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 ddlmZmZ dd	lmZ dd
lmZ ddlmZmZmZ ddlmZ ddlm Z  ddl!m"Z"m#Z# er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ddddddZ-dddddZ.dddd d!Z/dd"dd#d$Z0dd%dd&d'Z1ddddd(d)d*d+d,Z2G d-d. d.Z3d/S )0zTools to parse mongo client options.

.. seealso:: This module is compatible with both the synchronous and asynchronous PyMongo APIs.
    )annotations)TYPE_CHECKINGAnyMappingOptionalSequencecast)_parse_codec_options)common)CompressionSettings)ConfigurationError)_EventListener_EventListeners)PoolOptions)ReadConcern)_ServerModemake_read_preferenceread_pref_mode_from_name)any_server_selector)get_ssl_context)WriteConcernvalidate_boolean)CodecOptions)MongoCredential)AutoEncryptionOpts)
SSLContext)_ServerSelectorstrOptional[str]Mapping[str, Any]zOptional[MongoCredential])usernamepassworddatabaseoptionsreturnc                 C  sH   | d| rdnd}| d}| s&|rDddlm} |||| |||S dS )z!Parse authentication credentials.ZauthmechanismDEFAULTNZ
authsourcer   )_build_credentials_tuple)getpymongo.auth_sharedr&   )r    r!   r"   r#   Z	mechanismsourcer&    r*   :/tmp/pip-unpacked-wheel-36gvocj8/pymongo/client_options.py_parse_credentials/   s    
r,   r   )r#   r$   c                 C  sF   d| kr| d S |  dd}t|}|  d}|  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    
r2   r   c                 C  s6   |  d}|  d}|  d}|  d}t||||S )zParse write concern options.wZ
wtimeoutmsZjournalfsync)r'   r   )r#   concernZwtimeoutjr4   r*   r*   r+   _parse_write_concernH   s
    



r7   r   c                 C  s   |  d}t|S )zParse read concern options.Zreadconcernlevel)r'   r   )r#   r5   r*   r*   r+   _parse_read_concernQ   s    
r8   z!tuple[Optional[SSLContext], bool]c                 C  s  |  d}|dk	rtd| |  d}|  d}|  d}|  d}|  dd}|  d	d}|  d
d}g }	dD ]}
|
| krp| |
 rp|	|
 qpdD ]}
|
| kr| |
 s|	|
 q|	r|dkrd}n|stdd|	 |rt|||||||}||fS d|fS )zParse ssl options.ZtlsNtlscertificatekeyfiletlscertificatekeyfilepassword	tlscafile
tlscrlfiletlsallowinvalidcertificatesFtlsallowinvalidhostnamestlsdisableocspendpointcheck)r9   r:   r;   r<   )r=   r>   r?   TzmTLS has not been enabled but the following tls parameters have been set: %s. Please set `tls=True` or remove.z, )r'   r   appendr   joinr   )r#   Zuse_tlscertfileZ
passphraseca_certsZcrlfileZallow_invalid_certificatesZallow_invalid_hostnamesZdisable_ocsp_endpoint_checkZenabled_tls_optsoptctxr*   r*   r+   _parse_ssl_optionsW   sL    





	rF   boolr   )r    r!   r"   r#   is_syncr$   c                 C  s  t | |||}|dtj}|dtj}|dtj}|dk	rP||krPtd|dtj}	|d}
|dtj}t	t
tt  |d	}|d
}|d}|d}t|dg |dd}t|\}}|d}|dtj}t||||	|
|||t|||||||||dS )zParse connection pool options.ZmaxpoolsizeZminpoolsizeZmaxidletimemsNz3minPoolSize must be smaller or equal to maxPoolSizeZconnecttimeoutmsZsockettimeoutmsZwaitqueuetimeoutmsevent_listenersappnamedriver
server_apiZcompressorsZzlibcompressionlevelr.   loadbalancedZmaxconnecting)max_connectingrL   load_balancedcredentialsrH   )r,   r'   r
   ZMAX_POOL_SIZEZMIN_POOL_SIZEZMAX_IDLE_TIME_SEC
ValueErrorZCONNECT_TIMEOUTZWAIT_QUEUE_TIMEOUTr   r   r   r   r   rF   ZMAX_CONNECTINGr   r   )r    r!   r"   r#   rH   rP   Zmax_pool_sizeZmin_pool_sizeZmax_idle_time_secondsconnect_timeoutsocket_timeoutZwait_queue_timeoutrI   rJ   rK   rL   Zcompression_settingsssl_contextZtls_allow_invalid_hostnamesrO   rN   r*   r*   r+   _parse_pool_options   sN    




 

rU   c                   @  s  e Zd ZdZd?dddddddd	Zedd
ddZedd
ddZedd
ddZedd
ddZ	edd
ddZ
edd
ddZedd
ddZedd
ddZedd
d d!Zed"d
d#d$Zedd
d%d&Zed'd
d(d)Zed*d
d+d,Zed-d
d.d/Zedd
d0d1Zedd
d2d3Zed4d
d5d6Zedd
d7d8Zed9d
d:d;Zedd
d<d=Zd>S )@ClientOptionsa&  Read only configuration options for an AsyncMongoClient/MongoClient.

    Should not be instantiated directly by application developers. Access
    a client's options via :attr:`pymongo.mongo_client.AsyncMongoClient.options` or :attr:`pymongo.mongo_client.MongoClient.options`
    instead.
    Tr   r   r   rG   )r    r!   r"   r#   rH   c                 C  s   || _ t|| _|d| _|dtj| _|dtj| _	t
|||||| _t|| _|d| _t|| _t|| _|d| _|dtj| _|dtj| _|dtj| _|d	t| _|d
| _|d| _|d| _|dtj| _ d S )NZdirectconnectionZlocalthresholdmsZserverselectiontimeoutmsZ
replicasetconnectZheartbeatfrequencymsZretrywritesZ
retryreadsserver_selectorauto_encryption_optsrM   Z	timeoutmsZservermonitoringmode)!_ClientOptions__optionsr	   _ClientOptions__codec_optionsr'   !_ClientOptions__direct_connectionr
   ZLOCAL_THRESHOLD_MS"_ClientOptions__local_threshold_msZSERVER_SELECTION_TIMEOUT(_ClientOptions__server_selection_timeoutrU   _ClientOptions__pool_optionsr2   _ClientOptions__read_preference _ClientOptions__replica_set_namer7   _ClientOptions__write_concernr8   _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_ClientOptions__timeoutZSERVER_MONITORING_MODE&_ClientOptions__server_monitoring_mode)selfr    r!   r"   r#   rH   r*   r*   r+   __init__   s2    
 


 zClientOptions.__init__)r$   c                 C  s   | j S )z7The original options used to create this ClientOptions.)rZ   rm   r*   r*   r+   _options   s    zClientOptions._optionszOptional[bool]c                 C  s   | j S )z>Whether to begin discovering a MongoDB topology automatically.)rd   ro   r*   r*   r+   rW      s    zClientOptions.connectr   c                 C  s   | j S )z5A :class:`~bson.codec_options.CodecOptions` instance.)r[   ro   r*   r*   r+   codec_options   s    zClientOptions.codec_optionsc                 C  s   | j S )z:Whether to connect to the deployment in 'Single' topology.)r\   ro   r*   r*   r+   direct_connection   s    zClientOptions.direct_connectionintc                 C  s   | j S )z&The local threshold for this instance.)r]   ro   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.)r^   ro   r*   r*   r+   server_selection_timeout  s    z&ClientOptions.server_selection_timeoutr   c                 C  s   | j S )N)rh   ro   r*   r*   r+   rX   
  s    zClientOptions.server_selectorc                 C  s   | j S )z$The monitoring frequency in seconds.)re   ro   r*   r*   r+   heartbeat_frequency  s    z!ClientOptions.heartbeat_frequencyr   c                 C  s   | j S )z.A :class:`~pymongo.pool.PoolOptions` instance.)r_   ro   r*   r*   r+   pool_options  s    zClientOptions.pool_optionsr   c                 C  s   | j S )zA read preference instance.)r`   ro   r*   r*   r+   r-     s    zClientOptions.read_preferencec                 C  s   | j S )zReplica set name or None.)ra   ro   r*   r*   r+   replica_set_name  s    zClientOptions.replica_set_namer   c                 C  s   | j S )z8A :class:`~pymongo.write_concern.WriteConcern` instance.)rb   ro   r*   r*   r+   write_concern"  s    zClientOptions.write_concernr   c                 C  s   | j S )z6A :class:`~pymongo.read_concern.ReadConcern` instance.)rc   ro   r*   r*   r+   read_concern'  s    zClientOptions.read_concernzOptional[float]c                 C  s   | j S )z_The configured timeoutMS converted to seconds, or None.

        .. versionadded:: 4.2
        )rk   ro   r*   r*   r+   timeout,  s    zClientOptions.timeoutc                 C  s   | j S )z9If this instance should retry supported write operations.)rf   ro   r*   r*   r+   retry_writes4  s    zClientOptions.retry_writesc                 C  s   | j S )z8If this instance should retry supported read operations.)rg   ro   r*   r*   r+   retry_reads9  s    zClientOptions.retry_readszOptional[AutoEncryptionOpts]c                 C  s   | j S )z:A :class:`~pymongo.encryption.AutoEncryptionOpts` or None.)ri   ro   r*   r*   r+   rY   >  s    z"ClientOptions.auto_encryption_optsc                 C  s   | j S )z@True if the client was configured to connect to a load balancer.)rj   ro   r*   r*   r+   rO   C  s    zClientOptions.load_balancedzlist[_EventListeners]c                 C  s   | j jdk	st| j j S )zThe event listeners registered for this client.

        See :mod:`~pymongo.monitoring` for details.

        .. versionadded:: 4.0
        N)r_   Z_event_listenersAssertionErrorrI   ro   r*   r*   r+   rI   H  s    zClientOptions.event_listenersc                 C  s   | j S )zSThe configured serverMonitoringMode option.

        .. versionadded:: 4.5
        )rl   ro   r*   r*   r+   server_monitoring_modeS  s    z$ClientOptions.server_monitoring_modeN)T)__name__
__module____qualname____doc__rn   propertyrp   rW   rq   rr   rt   ru   rX   rv   rw   r-   rx   ry   rz   r{   r|   r}   rY   rO   rI   r   r*   r*   r*   r+   rV      sV    "
rV   N)4r   
__future__r   typingr   r   r   r   r   r   Zbson.codec_optionsr	   Zpymongor
   Zpymongo.compression_supportr   Zpymongo.errorsr   Zpymongo.monitoringr   r   Zpymongo.pool_optionsr   Zpymongo.read_concernr   Zpymongo.read_preferencesr   r   r   Zpymongo.server_selectorsr   Zpymongo.ssl_supportr   Zpymongo.write_concernr   r   r   r(   r   Zpymongo.encryption_optionsr   Zpymongo.pyopenssl_contextr   Zpymongo.topology_descriptionr   r,   r2   r7   r8   rF   rU   rV   r*   r*   r*   r+   <module>   s4    	;0