MCPcopy Index your code
hub / github.com/MagicStack/asyncpg / _parse_hostlist

Function _parse_hostlist

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

Source from the content-addressed store, hash-verified

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

Callers 1

Calls 2

_validate_port_specFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…