From 04a48c94f07da50ce225b2e1f7f6d47f62519061 Mon Sep 17 00:00:00 2001 From: Jan-Benedikt Jagusch Date: Tue, 11 Jul 2023 15:32:48 +0200 Subject: [PATCH 1/4] Implement _from_parsed_url for (H)DictStore and add test. --- minimalkv/memory/__init__.py | 8 +++++++ tests/test_get_store_from_url.py | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tests/test_get_store_from_url.py diff --git a/minimalkv/memory/__init__.py b/minimalkv/memory/__init__.py index 5dc55b99..072f5131 100644 --- a/minimalkv/memory/__init__.py +++ b/minimalkv/memory/__init__.py @@ -1,6 +1,8 @@ from io import BytesIO from typing import Dict, Iterator, Optional +from uritools import SplitResult + from minimalkv import CopyMixin, KeyValueStore @@ -43,3 +45,9 @@ def iter_keys(self, prefix: str = "") -> Iterator[str]: """ return filter(lambda k: k.startswith(prefix), iter(self.d)) + + @classmethod + def _from_parsed_url( + cls, parsed_url: SplitResult, query: Dict[str, str] + ) -> "DictStore": + return cls() diff --git a/tests/test_get_store_from_url.py b/tests/test_get_store_from_url.py new file mode 100644 index 00000000..e1a072e0 --- /dev/null +++ b/tests/test_get_store_from_url.py @@ -0,0 +1,39 @@ +from typing import Callable + +import pytest + +from minimalkv._get_store import get_store +from minimalkv._get_store import get_store_from_url as get_store_from_url_new +from minimalkv._hstores import HDictStore +from minimalkv._key_value_store import KeyValueStore +from minimalkv._urls import url2dict +from minimalkv.memory import DictStore + + +# Monkey patch equality operator for testing purposes. +def _eq_dict_store(self: object, other: object) -> bool: + if isinstance(self, DictStore): + if isinstance(other, DictStore): + return self.d == other.d + raise NotImplementedError + + +DictStore.__eq__ = _eq_dict_store # type: ignore + + +def get_store_from_url_old(url: str) -> KeyValueStore: + return get_store(**url2dict(url)) + + +@pytest.fixture(params=[get_store_from_url_new, get_store_from_url_old]) +def get_store_from_url(request) -> Callable: + return request.param + + +@pytest.mark.parametrize( + "url, key_value_store", [("memory://", DictStore()), ("hmemory://", HDictStore())] +) +def test_get_store_from_url( + url: str, key_value_store: KeyValueStore, get_store_from_url: Callable +) -> None: + assert get_store_from_url(url) == key_value_store From 2479ffe6636a73e4f622fd8716e26d9ad978817f Mon Sep 17 00:00:00 2001 From: Jan-Benedikt Jagusch Date: Tue, 11 Jul 2023 15:35:25 +0200 Subject: [PATCH 2/4] Fail hard for unknown scheme. --- minimalkv/_get_store.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/minimalkv/_get_store.py b/minimalkv/_get_store.py index bea57fd8..ccaabea8 100644 --- a/minimalkv/_get_store.py +++ b/minimalkv/_get_store.py @@ -4,7 +4,6 @@ from uritools import SplitResult, urisplit from minimalkv._key_value_store import KeyValueStore -from minimalkv._urls import url2dict def get_store_from_url( @@ -73,8 +72,8 @@ def get_store_from_url( scheme = scheme_parts[0] if scheme not in scheme_to_store: - # If we can't find the scheme, we fall back to the old creation methods - return get_store(**url2dict(url)) + # If we can't find the scheme, we fail hard. + raise NotImplementedError(scheme) store_cls_from_url = scheme_to_store[scheme] if store_cls is not None and store_cls_from_url != store_cls: From 56c2f2da7103fce9f0016ec4b7f8105be16a2a3d Mon Sep 17 00:00:00 2001 From: Jan-Benedikt Jagusch Date: Tue, 11 Jul 2023 15:36:57 +0200 Subject: [PATCH 3/4] Add memory scheme to get_store_from_url. --- minimalkv/_get_store.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/minimalkv/_get_store.py b/minimalkv/_get_store.py index ccaabea8..07fba653 100644 --- a/minimalkv/_get_store.py +++ b/minimalkv/_get_store.py @@ -52,13 +52,16 @@ def get_store_from_url( See the respective store's :func:`_from_parsed_url` function for more details. """ - from minimalkv._hstores import HS3FSStore + from minimalkv._hstores import HDictStore, HS3FSStore + from minimalkv.memory import DictStore from minimalkv.net.s3fsstore import S3FSStore scheme_to_store: Dict[str, Type[KeyValueStore]] = { "s3": S3FSStore, "hs3": HS3FSStore, "boto": HS3FSStore, + "memory": DictStore, + "hmemory": HDictStore, } parsed_url = urisplit(url) From 4dd3342115af9b4d363528111cee743a9b48171c Mon Sep 17 00:00:00 2001 From: Jan-Benedikt Jagusch Date: Tue, 11 Jul 2023 15:38:54 +0200 Subject: [PATCH 4/4] Add changelog entry. --- docs/changes.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/changes.rst b/docs/changes.rst index 80def5c5..c171ba9a 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -1,6 +1,11 @@ Changelog ********* +1.8.0 +===== +* Implemented the ``_from_parsed_url`` for the following stores: + * ``DictStore`` and ``HDictStore`` + 1.7.0 ===== * Deprecated ``get_store``, ``url2dict``, and ``extract_params``.