From 64f5246d78736ca859aa0eb70927d10980687030 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Tue, 3 Nov 2026 08:16:17 +0100 Subject: [PATCH] lib.Uri.query_dict: Add property Uri exposes the query as a raw string only, so a consumer that wants the query's key/value pairs has to parse the string itself with urllib.parse. Add a cached property query_dict that returns None when the Uri has no query and a dictionary of the query's key/value pairs when it does. Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0 Signed-off-by: Jan Lindemann --- src/python/jw/pkg/lib/Uri.py | 8 ++++++++ test/unit/python/jw/pkg/lib/Uri/test.py | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/src/python/jw/pkg/lib/Uri.py b/src/python/jw/pkg/lib/Uri.py index de3a76b2..f6fe18cb 100644 --- a/src/python/jw/pkg/lib/Uri.py +++ b/src/python/jw/pkg/lib/Uri.py @@ -119,6 +119,14 @@ class Uri: def query(self) -> str: return self.__p.query + @cached_property + def query_dict(self) -> dict[str, str] | None: + if not self.query: + return None + from urllib.parse import parse_qsl + + return dict(parse_qsl(self.query)) + @cached_property def basename(self) -> str: return self.__p.path.rsplit('/')[-1] diff --git a/test/unit/python/jw/pkg/lib/Uri/test.py b/test/unit/python/jw/pkg/lib/Uri/test.py index 7ffa9052..60a958c3 100644 --- a/test/unit/python/jw/pkg/lib/Uri/test.py +++ b/test/unit/python/jw/pkg/lib/Uri/test.py @@ -109,4 +109,11 @@ assert u.new_replace_path('etc').to_string == \ assert 'pkg-filter=url=~jw.dev' in u.full assert Uri('ssh://host/path').query == '' +# query_dict is None without a query and a key/value dict with one +assert Uri('ssh://host/path').query_dict is None +u = Uri('kvfile://host/?pkg-filter=url=~jw.dev') +assert u.query_dict == {'pkg-filter': 'url=~jw.dev'} +u = Uri('https://example.com/?a=1&b=two') +assert u.query_dict == {'a': '1', 'b': 'two'} + print('All Uri tests passed') -- 2.56.1