Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __new__(cls, urlstring=None, force_abs=True,
scheme='file', netloc='', path='',
hostname=None, port=None, username=None, password=None):
"""
Create a new `Url` object. See the `Url`:class: documentation
for invocation syntax.
"""
if urlstring is not None:
if isinstance(urlstring, Url):
# copy constructor
return tuple.__new__(cls, (
urlstring.scheme, urlstring.netloc, urlstring.path,
urlstring.hostname, urlstring.port,
urlstring.username, urlstring.password,
))
else:
# parse `urlstring` and use kwd arguments as default values
try:
urldata = urlparse.urlsplit(
urlstring, scheme=scheme, allow_fragments=False)
if urldata.scheme == 'file' and not os.path.isabs(
urldata.path) and force_abs:
urldata = urlparse.urlsplit(
'file://' + os.path.abspath(urldata.path))
return tuple.__new__(cls, (
>>> u2 = u1.adjoin('moredata')
>>> str(u2)
'http://www.example.org/data/moredata'
Even if `relpath` starts with `/`, it is still appended to the
path in the base URL::
>>> u3 = u2.adjoin('/evenmore')
>>> str(u3)
'http://www.example.org/data/moredata/evenmore'
"""
if relpath.startswith('/'):
relpath = relpath[1:]
return Url(scheme=self.scheme, netloc=self.netloc,
path=os.path.join((self.path or '/'), relpath),
hostname=self.hostname, port=self.port,
username=self.username, password=self.password)
def __setitem__(self, key, value):
try:
dict.__setitem__(self, Url(key, self._force_abs), value)
except:
dict.__setitem__(self, key, value)
>>> u == 'file:///tmp/foo'
True
>>> u == 'http://example.org'
False
>>> u == 42
False
"""
try:
# The `tuple.__eq__` call can only be used if both `self`
# and `other` are `tuple` subclasses; we know that `self`
# is, but we need to check `other`.
return ((isinstance(other, tuple) and tuple.__eq__(self, other))
or str(self) == str(other)
or tuple.__eq__(self, Url(other)))
except ValueError:
# `other` is not a URL and cannot be made into one
return False
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError as ex:
# map `key` to a URL and try with that
try:
return dict.__getitem__(self, Url(key, self._force_abs))
except:
raise ex
def __setitem__(self, key, value):
try:
dict.__setitem__(self, key, Url(value, self._force_abs))
except:
dict.__setitem__(self, key, value)