From d6f622e00037e2079833b6bf90dbecf336a2ed8f Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sat, 31 Oct 2026 15:56:08 +0100 Subject: [PATCH] lib.Uri.query: Add property 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 Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.4.0 --- src/python/jw/pkg/lib/Uri.py | 69 ++++++++++++++++--------- test/unit/python/jw/pkg/lib/Uri/test.py | 13 +++++ 2 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/python/jw/pkg/lib/Uri.py b/src/python/jw/pkg/lib/Uri.py index 51534410..de3a76b2 100644 --- a/src/python/jw/pkg/lib/Uri.py +++ b/src/python/jw/pkg/lib/Uri.py @@ -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: diff --git a/test/unit/python/jw/pkg/lib/Uri/test.py b/test/unit/python/jw/pkg/lib/Uri/test.py index dc68b84f..7ffa9052 100644 --- a/test/unit/python/jw/pkg/lib/Uri/test.py +++ b/test/unit/python/jw/pkg/lib/Uri/test.py @@ -96,4 +96,17 @@ assert u.to_string == 'ssh://user:pass@host:22/path' u = Uri('https://user:secret@example.com/repo.git') assert '' 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') -- 2.56.1