|
4 | 4 | from typing import Any, Optional |
5 | 5 | from urllib.parse import unquote_plus |
6 | 6 |
|
7 | | -from pymongo.asynchronous.srv_resolver import _have_dnspython, _SrvResolver |
| 7 | +from pymongo.asynchronous.srv_resolver import _SrvResolver |
8 | 8 | from pymongo.common import SRV_SERVICE_NAME, _CaseInsensitiveDictionary |
9 | 9 | from pymongo.errors import ConfigurationError, InvalidURI |
10 | 10 | from pymongo.uri_parser_shared import ( |
11 | 11 | _ALLOWED_TXT_OPTS, |
12 | | - _BAD_DB_CHARS, |
13 | 12 | DEFAULT_PORT, |
14 | 13 | SCHEME, |
15 | 14 | SCHEME_LEN, |
16 | | - SRV_SCHEME, |
17 | 15 | SRV_SCHEME_LEN, |
18 | 16 | _check_options, |
19 | | - parse_userinfo, |
| 17 | + _validate_uri, |
20 | 18 | split_hosts, |
21 | 19 | split_options, |
22 | 20 | ) |
@@ -103,102 +101,6 @@ async def parse_uri( |
103 | 101 | return result |
104 | 102 |
|
105 | 103 |
|
106 | | -def _validate_uri( |
107 | | - uri: str, |
108 | | - default_port: Optional[int] = DEFAULT_PORT, |
109 | | - validate: bool = True, |
110 | | - warn: bool = False, |
111 | | - normalize: bool = True, |
112 | | - srv_max_hosts: Optional[int] = None, |
113 | | -) -> dict[str, Any]: |
114 | | - if uri.startswith(SCHEME): |
115 | | - is_srv = False |
116 | | - scheme_free = uri[SCHEME_LEN:] |
117 | | - elif uri.startswith(SRV_SCHEME): |
118 | | - if not _have_dnspython(): |
119 | | - python_path = sys.executable or "python" |
120 | | - raise ConfigurationError( |
121 | | - 'The "dnspython" module must be ' |
122 | | - "installed to use mongodb+srv:// URIs. " |
123 | | - "To fix this error install pymongo again:\n " |
124 | | - "%s -m pip install pymongo>=4.3" % (python_path) |
125 | | - ) |
126 | | - is_srv = True |
127 | | - scheme_free = uri[SRV_SCHEME_LEN:] |
128 | | - else: |
129 | | - raise InvalidURI(f"Invalid URI scheme: URI must begin with '{SCHEME}' or '{SRV_SCHEME}'") |
130 | | - |
131 | | - if not scheme_free: |
132 | | - raise InvalidURI("Must provide at least one hostname or IP") |
133 | | - |
134 | | - user = None |
135 | | - passwd = None |
136 | | - dbase = None |
137 | | - collection = None |
138 | | - options = _CaseInsensitiveDictionary() |
139 | | - |
140 | | - host_plus_db_part, _, opts = scheme_free.partition("?") |
141 | | - if "/" in host_plus_db_part: |
142 | | - host_part, _, dbase = host_plus_db_part.partition("/") |
143 | | - else: |
144 | | - host_part = host_plus_db_part |
145 | | - |
146 | | - if dbase: |
147 | | - dbase = unquote_plus(dbase) |
148 | | - if "." in dbase: |
149 | | - dbase, collection = dbase.split(".", 1) |
150 | | - if _BAD_DB_CHARS.search(dbase): |
151 | | - raise InvalidURI('Bad database name "%s"' % dbase) |
152 | | - else: |
153 | | - dbase = None |
154 | | - |
155 | | - if opts: |
156 | | - options.update(split_options(opts, validate, warn, normalize)) |
157 | | - if "@" in host_part: |
158 | | - userinfo, _, hosts = host_part.rpartition("@") |
159 | | - user, passwd = parse_userinfo(userinfo) |
160 | | - else: |
161 | | - hosts = host_part |
162 | | - |
163 | | - if "/" in hosts: |
164 | | - raise InvalidURI("Any '/' in a unix domain socket must be percent-encoded: %s" % host_part) |
165 | | - |
166 | | - hosts = unquote_plus(hosts) |
167 | | - fqdn = None |
168 | | - srv_max_hosts = srv_max_hosts or options.get("srvMaxHosts") |
169 | | - if is_srv: |
170 | | - if options.get("directConnection"): |
171 | | - raise ConfigurationError(f"Cannot specify directConnection=true with {SRV_SCHEME} URIs") |
172 | | - nodes = split_hosts(hosts, default_port=None) |
173 | | - if len(nodes) != 1: |
174 | | - raise InvalidURI(f"{SRV_SCHEME} URIs must include one, and only one, hostname") |
175 | | - fqdn, port = nodes[0] |
176 | | - if port is not None: |
177 | | - raise InvalidURI(f"{SRV_SCHEME} URIs must not include a port number") |
178 | | - elif not is_srv and options.get("srvServiceName") is not None: |
179 | | - raise ConfigurationError( |
180 | | - "The srvServiceName option is only allowed with 'mongodb+srv://' URIs" |
181 | | - ) |
182 | | - elif not is_srv and srv_max_hosts: |
183 | | - raise ConfigurationError( |
184 | | - "The srvMaxHosts option is only allowed with 'mongodb+srv://' URIs" |
185 | | - ) |
186 | | - else: |
187 | | - nodes = split_hosts(hosts, default_port=default_port) |
188 | | - |
189 | | - _check_options(nodes, options) |
190 | | - |
191 | | - return { |
192 | | - "nodelist": nodes, |
193 | | - "username": user, |
194 | | - "password": passwd, |
195 | | - "database": dbase, |
196 | | - "collection": collection, |
197 | | - "options": options, |
198 | | - "fqdn": fqdn, |
199 | | - } |
200 | | - |
201 | | - |
202 | 104 | async def _parse_srv( |
203 | 105 | uri: str, |
204 | 106 | default_port: Optional[int] = DEFAULT_PORT, |
@@ -277,10 +179,6 @@ async def _parse_srv( |
277 | 179 | try: |
278 | 180 | if _IS_SYNC: |
279 | 181 | pprint.pprint(parse_uri(sys.argv[1])) # noqa: T203 |
280 | | - else: |
281 | | - import asyncio |
282 | | - |
283 | | - pprint.pprint(asyncio.run(parse_uri(sys.argv[1]))) # type:ignore[arg-type] # noqa: T203 |
284 | 182 | except InvalidURI as exc: |
285 | 183 | print(exc) # noqa: T201 |
286 | 184 | sys.exit(0) |
0 commit comments