url#

Common URL utilities.

Classes#

Url

Object used to represent a Url. The string representation of this object is a url string.

Functions#

hex_octal_to_int(ho)

percent_decode(path)

path_to_url(path)

urlparse(→ Url)

url_to_s3_info(url)

Convert an s3 url to a tuple of bucket and key.

is_url(url)

is_ipv4_address(string_ip)

is_ipv6_address(string_ip)

is_ip_address(string_ip)

join(*args)

has_scheme(value)

strip_scheme(url)

mask_anaconda_token(url)

split_anaconda_token(url)

split_platform(known_subdirs, url)

_split_platform_re(known_subdirs)

has_platform(url, known_subdirs)

split_scheme_auth_token(url)

split_conda_url_easy_parts(known_subdirs, url)

get_proxy_username_and_pass(scheme)

add_username_and_password(→ str)

Inserts username and password into provided url

maybe_add_auth(→ str)

Add auth if the url doesn't currently have it.

maybe_unquote(url)

remove_auth(→ str)

Remove embedded authentication from URL.

Attributes#

file_scheme

def url_to_path(url):

url_attrs

join_url

hex_octal_to_int(ho)#
percent_decode(path)#
file_scheme = 'file://'#

def url_to_path(url): assert url.startswith(file_scheme), "{} is not a file-scheme URL".format(url) decoded = percent_decode(url[len(file_scheme):]) if decoded.startswith('/') and decoded[2] == ':':

# A Windows path. decoded.replace('/', '')

return decoded

path_to_url(path)#
url_attrs = ('scheme', 'path', 'query', 'fragment', 'username', 'password', 'hostname', 'port')#
class Url#

Bases: namedtuple('Url', url_attrs)

Object used to represent a Url. The string representation of this object is a url string.

This object was inspired by the urllib3 implementation as it gives you a way to construct URLs from various parts. The motivation behind this object was making something that is interoperable with built the urllib.parse.urlparse function and has more features than the built-in ParseResult object.

Initialize self. See help(type(self)) for accurate signature.

property auth#
property netloc#
__str__()#

Return str(self).

as_dict() dict#

Provide a public interface for namedtuple's _asdict

replace(**kwargs) Url#

Provide a public interface for namedtuple's _replace

classmethod from_parse_result(parse_result: urllib.parse.ParseResult) Url#
urlparse(url: str) Url#
url_to_s3_info(url)#

Convert an s3 url to a tuple of bucket and key.

Examples

>>> url_to_s3_info("s3://bucket-name.bucket/here/is/the/key")
('bucket-name.bucket', '/here/is/the/key')
is_url(url)#

Examples

>>> is_url(None)
False
>>> is_url("s3://some/bucket")
True
is_ipv4_address(string_ip)#

Examples

>>> [is_ipv4_address(ip) for ip in ('8.8.8.8', '192.168.10.10', '255.255.255.255')]
[True, True, True]
>>> [is_ipv4_address(ip) for ip in ('8.8.8', '192.168.10.10.20', '256.255.255.255', '::1')]
[False, False, False, False]
is_ipv6_address(string_ip)#

Examples

>> [is_ipv6_address(ip) for ip in ('::1', '2001:db8:85a3::370:7334', '1234:'*7+'1234')] [True, True, True] >> [is_ipv6_address(ip) for ip in ('192.168.10.10', '1234:'*8+'1234')] [False, False]

is_ip_address(string_ip)#

Examples

>> is_ip_address('192.168.10.10') True >> is_ip_address('::1') True >> is_ip_address('www.google.com') False

join(*args)#
join_url#
has_scheme(value)#
strip_scheme(url)#

Examples

>>> strip_scheme("https://www.conda.io")
'www.conda.io'
>>> strip_scheme("s3://some.bucket/plus/a/path.ext")
'some.bucket/plus/a/path.ext'
mask_anaconda_token(url)#
split_anaconda_token(url)#

Examples

>>> split_anaconda_token("https://1.2.3.4/t/tk-123-456/path")
(u'https://1.2.3.4/path', u'tk-123-456')
>>> split_anaconda_token("https://1.2.3.4/t//path")
(u'https://1.2.3.4/path', u'')
>>> split_anaconda_token("https://some.domain/api/t/tk-123-456/path")
(u'https://some.domain/api/path', u'tk-123-456')
>>> split_anaconda_token("https://1.2.3.4/conda/t/tk-123-456/path")
(u'https://1.2.3.4/conda/path', u'tk-123-456')
>>> split_anaconda_token("https://1.2.3.4/path")
(u'https://1.2.3.4/path', None)
>>> split_anaconda_token("https://10.2.3.4:8080/conda/t/tk-123-45")
(u'https://10.2.3.4:8080/conda', u'tk-123-45')
split_platform(known_subdirs, url)#

Examples

>>> from conda.base.constants import KNOWN_SUBDIRS
>>> split_platform(KNOWN_SUBDIRS, "https://1.2.3.4/t/tk-123/linux-ppc64le/path")
(u'https://1.2.3.4/t/tk-123/path', u'linux-ppc64le')
_split_platform_re(known_subdirs)#
has_platform(url, known_subdirs)#
split_scheme_auth_token(url)#

Examples

>>> split_scheme_auth_token("https://u:[email protected]/t/x1029384756/more/path")
('conda.io/more/path', 'https', 'u:p', 'x1029384756')
>>> split_scheme_auth_token(None)
(None, None, None, None)
split_conda_url_easy_parts(known_subdirs, url)#
get_proxy_username_and_pass(scheme)#
add_username_and_password(url: str, username: str, password: str) str#

Inserts username and password into provided url

>>> add_username_and_password('https://anaconda.org', 'TestUser', 'Password')
'https://TestUser:[email protected]'
maybe_add_auth(url: str, auth: str, force=False) str#

Add auth if the url doesn't currently have it.

By default, does not replace auth if it already exists. Setting force to True overrides this behavior.

Examples

>>> maybe_add_auth("https://www.conda.io", "user:passwd")
'https://user:[email protected]'
>>> maybe_add_auth("https://www.conda.io", "")
'https://www.conda.io'
maybe_unquote(url)#
remove_auth(url: str) str#

Remove embedded authentication from URL.

>>> remove_auth("https://user:[email protected]")
'https://anaconda.com'