lib.Uri.query: Add property #163
2 changed files with 58 additions and 24 deletions
|
|
@ -12,7 +12,7 @@ if TYPE_CHECKING:
|
|||
class Uri:
|
||||
|
||||
def __assemble(
|
||||
self, scheme: bool, credentials: bool, secure: bool, path: bool
|
||||
self, scheme: bool, credentials: bool, secure: bool, path: bool, query: bool
|
||||
) -> str:
|
||||
ret = ''
|
||||
if self.__p.path.startswith('~'):
|
||||
|
|
@ -35,6 +35,8 @@ class Uri:
|
|||
ret += ':' + self.port_str
|
||||
if path:
|
||||
ret += self.path
|
||||
if query and self.__p.query:
|
||||
ret += f'?{self.__p.query}'
|
||||
return ret
|
||||
|
||||
def __init__(self, string: str) -> None:
|
||||
|
|
@ -113,6 +115,10 @@ class Uri:
|
|||
def path(self) -> str:
|
||||
return self.__p.path
|
||||
|
||||
@cached_property
|
||||
def query(self) -> str:
|
||||
return self.__p.query
|
||||
|
||||
@cached_property
|
||||
def basename(self) -> str:
|
||||
return self.__p.path.rsplit('/')[-1]
|
||||
|
|
@ -120,13 +126,21 @@ class Uri:
|
|||
@cached_property
|
||||
def authority(self) -> str:
|
||||
return self.__assemble(
|
||||
scheme = False, credentials = True, secure = False, path = False
|
||||
scheme = False,
|
||||
credentials = True,
|
||||
secure = False,
|
||||
path = False,
|
||||
query = False
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def origin(self) -> str:
|
||||
return self.__assemble(
|
||||
scheme = False, credentials = False, secure = True, path = False
|
||||
scheme = False,
|
||||
credentials = False,
|
||||
secure = True,
|
||||
path = False,
|
||||
query = False
|
||||
)
|
||||
|
||||
@cached_property
|
||||
|
|
@ -136,47 +150,54 @@ class Uri:
|
|||
@cached_property
|
||||
def id(self) -> str:
|
||||
return self.__assemble(
|
||||
scheme = True, credentials = True, secure = True, path = False
|
||||
scheme = True,
|
||||
credentials = True,
|
||||
secure = True,
|
||||
path = False,
|
||||
query = False
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def full(self) -> str:
|
||||
return self.__assemble(
|
||||
scheme = True, credentials = True, secure = False, path = True
|
||||
scheme = True,
|
||||
credentials = True,
|
||||
secure = False,
|
||||
path = True,
|
||||
query = True
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def safe_full_with_username(self) -> str:
|
||||
return self.__assemble(
|
||||
scheme = True, credentials = True, secure = True, path = True
|
||||
scheme = True, credentials = True, secure = True, path = True, query = True
|
||||
)
|
||||
|
||||
def __new_with_path(self, base: str, path: str) -> Self:
|
||||
# -- Build a fresh instance rather than copying self: a copy would
|
||||
# inherit computed cached_property values (e.g. __p, path, full),
|
||||
# which would go stale as soon as __string is replaced below.
|
||||
# which would go stale as soon as __string is replaced below. The
|
||||
# query, if any, is a component of the URI apart from the path:
|
||||
# strip it from the base before the path is spliced in, and
|
||||
# re-append it to the result
|
||||
query = self.__p.query
|
||||
base = base.split('?', 1)[0]
|
||||
ret = object.__new__(type(self))
|
||||
ret.__string = base
|
||||
ret.__username = None
|
||||
ret.__password = None
|
||||
if not path:
|
||||
return ret
|
||||
if ret.__string.endswith('://'):
|
||||
# -- Empty authority: the trailing '/' is part of the '://'
|
||||
# separator, so a leading slash in path must be kept to stay
|
||||
# an absolute path
|
||||
ret.__string += path if path.startswith('/') else '/' + path
|
||||
return ret
|
||||
if ret.__string[-1] == '/':
|
||||
if path[0] == '/':
|
||||
ret.__string += path[1:]
|
||||
if path:
|
||||
if ret.__string.endswith('://'):
|
||||
# -- Empty authority: the trailing '/' is part of the
|
||||
# '://' separator, so a leading slash in path must be
|
||||
# kept to stay an absolute path
|
||||
ret.__string += path if path.startswith('/') else '/' + path
|
||||
elif ret.__string[-1] == '/':
|
||||
ret.__string += path[1:] if path[0] == '/' else path
|
||||
else:
|
||||
ret.__string += path
|
||||
else:
|
||||
if path[0] == '/':
|
||||
ret.__string += path
|
||||
else:
|
||||
ret.__string += '/' + path
|
||||
ret.__string += path if path[0] == '/' else '/' + path
|
||||
if query:
|
||||
ret.__string += f'?{query}'
|
||||
return ret
|
||||
|
||||
def new_add_path(self, path: str) -> Self:
|
||||
|
|
|
|||
|
|
@ -96,4 +96,17 @@ assert u.to_string == 'ssh://user:pass@host:22/path'
|
|||
u = Uri('https://user:secret@example.com/repo.git')
|
||||
assert '<hidden>' in str(u)
|
||||
|
||||
# The query is identity, not path: it is exposed, it survives a
|
||||
# path change, and it shows in the full form
|
||||
u = Uri('kvfile://host/?pkg-filter=url=~jw.dev')
|
||||
assert u.query == 'pkg-filter=url=~jw.dev'
|
||||
assert u.path == '/'
|
||||
assert u.new_replace_path('etc/foo.conf').to_string == \
|
||||
'kvfile://host/etc/foo.conf?pkg-filter=url=~jw.dev'
|
||||
u = Uri('kvfile:///?pkg-filter=url=~jw.dev')
|
||||
assert u.new_replace_path('etc').to_string == \
|
||||
'kvfile:///etc?pkg-filter=url=~jw.dev'
|
||||
assert 'pkg-filter=url=~jw.dev' in u.full
|
||||
assert Uri('ssh://host/path').query == ''
|
||||
|
||||
print('All Uri tests passed')
|
||||
|
|
|
|||
Loading…
Reference in a new issue