MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / _parse_hostlist

Function _parse_hostlist

asyncpg/connect_utils.py:196–257  ·  view source on GitHub ↗
(hostlist, port, *, unquote=False)

Source from the content-addressed store, hash-verified

194
195
196def _parse_hostlist(hostlist, port, *, unquote=False):
197 if ',' in hostlist:
198 # A comma-separated list of host addresses.
199 hostspecs = hostlist.split(',')
200 else:
201 hostspecs = [hostlist]
202
203 hosts = []
204 hostlist_ports = []
205
206 if not port:
207 portspec = os.environ.get('PGPORT')
208 if portspec:
209 if ',' in portspec:
210 default_port = [int(p) for p in portspec.split(',')]
211 else:
212 default_port = int(portspec)
213 else:
214 default_port = 5432
215
216 default_port = _validate_port_spec(hostspecs, default_port)
217
218 else:
219 port = _validate_port_spec(hostspecs, port)
220
221 for i, hostspec in enumerate(hostspecs):
222 if hostspec[0] == '/':
223 # Unix socket
224 addr = hostspec
225 hostspec_port = ''
226 elif hostspec[0] == '[':
227 # IPv6 address
228 m = re.match(r'(?:\[([^\]]+)\])(?::([0-9]+))?', hostspec)
229 if m:
230 addr = m.group(1)
231 hostspec_port = m.group(2)
232 else:
233 raise exceptions.ClientConfigurationError(
234 'invalid IPv6 address in the connection URI: {!r}'.format(
235 hostspec
236 )
237 )
238 else:
239 # IPv4 address
240 addr, _, hostspec_port = hostspec.partition(':')
241
242 if unquote:
243 addr = urllib.parse.unquote(addr)
244
245 hosts.append(addr)
246 if not port:
247 if hostspec_port:
248 if unquote:
249 hostspec_port = urllib.parse.unquote(hostspec_port)
250 hostlist_ports.append(int(hostspec_port))
251 else:
252 hostlist_ports.append(default_port[i])
253

Callers 1

Calls 2

_validate_port_specFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected