o
    sDi                     @  s   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZmZmZmZmZmZmZmZmZmZ d	Zed	d
d	dddfdddZed	d
d	dddfdddZdS ) z*Tools to parse and validate a MongoDB URI.    )annotations)AnyOptional)unquote_plus)SRV_SERVICE_NAME_CaseInsensitiveDictionary)ConfigurationError
InvalidURI)_SrvResolver)
_ALLOWED_TXT_OPTSDEFAULT_PORTSCHEME
SCHEME_LENSRV_SCHEME_LEN_check_options_make_options_case_sensitive_validate_urisplit_hostssplit_optionsTFNuristrdefault_portOptional[int]validateboolwarn	normalizeconnect_timeoutOptional[float]srv_service_nameOptional[str]srv_max_hostsreturndict[str, Any]c           	      C  sB   t | |||||}|t| ||||||| t|d |d< |S )a  Parse and validate a MongoDB URI.

    Returns a dict of the form::

        {
            'nodelist': <list of (host, port) tuples>,
            'username': <username> or None,
            'password': <password> or None,
            'database': <database name> or None,
            'collection': <collection name> or None,
            'options': <dict of MongoDB URI options>,
            'fqdn': <fqdn of the MongoDB+SRV URI> or None
        }

    If the URI scheme is "mongodb+srv://" DNS SRV and TXT lookups will be done
    to build nodelist and options.

    :param uri: The MongoDB URI to parse.
    :param default_port: The port number to use when one wasn't specified
          for a host in the URI.
    :param validate: If ``True`` (the default), validate and
          normalize all options. Default: ``True``.
    :param warn: When validating, if ``True`` then will warn
          the user then ignore any invalid options or values. If ``False``,
          validation will error when options are unsupported or values are
          invalid. Default: ``False``.
    :param normalize: If ``True``, convert names of URI options
          to their internally-used names. Default: ``True``.
    :param connect_timeout: The maximum time in milliseconds to
          wait for a response from the DNS server.
    :param srv_service_name: A custom SRV service name

    .. versionchanged:: 4.14
       ``options`` is now type ``dict`` as opposed to a ``_CaseInsensitiveDictionary``.

    .. versionchanged:: 4.6
       The delimiting slash (``/``) between hosts and connection options is now optional.
       For example, "mongodb://example.com?tls=true" is now a valid URI.

    .. versionchanged:: 4.0
       To better follow RFC 3986, unquoted percent signs ("%") are no longer
       supported.

    .. versionchanged:: 3.9
        Added the ``normalize`` parameter.

    .. versionchanged:: 3.6
        Added support for mongodb+srv:// URIs.

    .. versionchanged:: 3.5
        Return the original value of the ``readPreference`` MongoDB URI option
        instead of the validated read preference mode.

    .. versionchanged:: 3.1
        ``warn`` added so invalid options can be ignored.
    options)r   update
_parse_srvr   )	r   r   r   r   r   r   r   r!   result r(   \/home/alumno/antzosa8961/venv/lib/python3.10/site-packages/pymongo/synchronous/uri_parser.py	parse_uri)   s   Br*   c                 C  s  |  trd}| td  }	nd}| td  }	t }
|	d\}}}d|v r.|d\}}}n|}|r<|
t|||| |d u rF|
dt	}d|v rS|
d\}}}n|}t|}|p_|
d}|rt|d d}|d	 \}}|pt|
d
}t||||}| }| }|rt||||}t|t rtd| D ]\}}||
vr||
|< q|
dr|rtd|
dr|rtdd|
vrd|
vr|rdnd|
d< nt||d}t||
 ||
dS )NFT?/srvServiceName@srvMaxHosts)r   r   connectTimeoutMSzDOnly authSource, replicaSet, and loadBalanced are supported from DNSloadBalancedz0You cannot specify loadBalanced with srvMaxHosts
replicaSetz.You cannot specify replicaSet with srvMaxHoststlsssltrue)nodelistr$   )
startswithr   r   r   r   	partitionr%   r   getr   
rpartitionr   r   r
   	get_hostsget_optionssetr   r   itemsr	   r   )r   r   r   r   r   r   r   r!   is_srvscheme_freer$   host_plus_db_part_opts	host_parthostsnodesfqdnportdns_resolverdns_optionsparsed_dns_optionsoptvalr(   r(   r)   r&   |   s`   


r&   )r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r   r"   r#   )__doc__
__future__r   typingr   r   urllib.parser   pymongo.commonr   r   pymongo.errorsr   r	    pymongo.synchronous.srv_resolverr
   pymongo.uri_parser_sharedr   r   r   r   r   r   r   r   r   r   _IS_SYNCr*   r&   r(   r(   r(   r)   <module>   s2   0U