lib.Uri.query: Add property
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m58s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 5m14s
CI / Packaging test (pull_request) Successful in 0s

lib.Uri doesn't support query strings, and this commit adds support: Expose
the query as a property, keep it through the path replacement methods, and
show it in the full forms.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.4.0
This commit is contained in:
Jan Lindemann 2026-10-31 15:56:08 +01:00
commit d6f622e000
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -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: