Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def join(self, url):
"""Join URLs
Construct a full (“absolute”) URL by combining a “base URL”
(self) with another URL (url).
Informally, this uses components of the base URL, in
particular the addressing scheme, the network location and
(part of) the path, to provide missing components in the
relative URL.
"""
# See docs for urllib.parse.urljoin
if not isinstance(url, URL):
raise TypeError("url should be URL")
return URL(urljoin(str(self), str(url)), encoded=True)
def with_scheme(self, scheme):
"""Return a new URL with scheme replaced."""
# N.B. doesn't cleanup query/fragment
if not isinstance(scheme, str):
raise TypeError("Invalid scheme type")
if not self.is_absolute():
raise ValueError("scheme replacement is not allowed " "for relative URLs")
return URL(self._val._replace(scheme=scheme.lower()), encoded=True)
Autoencode fragment if needed.
Clear fragment to default if None is passed.
"""
# N.B. doesn't cleanup query/fragment
if fragment is None:
raw_fragment = ""
elif not isinstance(fragment, str):
raise TypeError("Invalid fragment type")
else:
raw_fragment = self._FRAGMENT_QUOTER(fragment)
if self.raw_fragment == raw_fragment:
return self
return URL(self._val._replace(fragment=raw_fragment), encoded=True)
def with_path(self, path, *, encoded=False):
"""Return a new URL with path replaced."""
if not encoded:
path = self._PATH_QUOTER(path)
if self.is_absolute():
path = self._normalize_path(path)
if len(path) > 0 and path[0] != "/":
path = "/" + path
return URL(self._val._replace(path=path, query="", fragment=""), encoded=True)
def __le__(self, other):
if not type(other) is URL:
return NotImplemented
return self._val <= other._val
def update_query(self, *args, **kwargs):
"""Return a new URL with query part updated."""
s = self._get_str_query(*args, **kwargs)
new_query = MultiDict(parse_qsl(s, keep_blank_values=True))
query = MultiDict(self.query)
query.update(new_query)
return URL(self._val._replace(query=self._get_str_query(query)), encoded=True)
Clear user/password if user is None.
"""
# N.B. doesn't cleanup query/fragment
val = self._val
if user is None:
password = None
elif isinstance(user, str):
user = self._QUOTER(user)
password = val.password
else:
raise TypeError("Invalid user type")
if not self.is_absolute():
raise ValueError("user replacement is not allowed " "for relative URLs")
return URL(
self._val._replace(
netloc=self._make_netloc(
user, password, val.hostname, val.port, encode=False
)
),
encoded=True,
)
def with_port(self, port):
"""Return a new URL with port replaced.
Clear port to default if None is passed.
"""
# N.B. doesn't cleanup query/fragment
if port is not None and not isinstance(port, int):
raise TypeError("port should be int or None, got {}".format(type(port)))
if not self.is_absolute():
raise ValueError("port replacement is not allowed " "for relative URLs")
val = self._val
return URL(
self._val._replace(
netloc=self._make_netloc(
val.username, val.password, val.hostname, port, encode=False
)
),
encoded=True,
)
def join(self, url):
"""Join URLs
Construct a full (“absolute”) URL by combining a “base URL”
(self) with another URL (url).
Informally, this uses components of the base URL, in
particular the addressing scheme, the network location and
(part of) the path, to provide missing components in the
relative URL.
"""
# See docs for urllib.parse.urljoin
if not isinstance(url, URL):
raise TypeError("url should be URL")
return URL(urljoin(str(self), str(url)), encoded=True)
def relative(self):
"""Return a relative part of the URL.
scheme, user, password, host and port are removed.
"""
if not self.is_absolute():
raise ValueError("URL should be absolute")
val = self._val._replace(scheme="", netloc="")
return URL(val, encoded=True)