|
| 1 | +####################################################################### |
| 2 | +# Copyright (c) 2019-present, Blosc Development Team <blosc@blosc.org> |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: BSD-3-Clause |
| 6 | +####################################################################### |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +from dataclasses import dataclass |
| 11 | +from typing import Any |
| 12 | + |
| 13 | + |
| 14 | +@dataclass(frozen=True, slots=True) |
| 15 | +class Ref: |
| 16 | + """A durable reference to a Blosc2 object. |
| 17 | +
|
| 18 | + ``Ref`` can describe: |
| 19 | +
|
| 20 | + - a persistent local Blosc2 object reopenable from ``urlpath`` |
| 21 | + - a member inside a :class:`blosc2.DictStore` |
| 22 | + - a remote :class:`blosc2.C2Array` |
| 23 | +
|
| 24 | + Instances can be created directly, from dictionaries via :meth:`from_dict`, |
| 25 | + or from supported objects via :meth:`from_object`. Use :meth:`open` to |
| 26 | + resolve the reference back into a live Blosc2 object. |
| 27 | + """ |
| 28 | + |
| 29 | + kind: str |
| 30 | + urlpath: str | None = None |
| 31 | + key: str | None = None |
| 32 | + path: str | None = None |
| 33 | + urlbase: str | None = None |
| 34 | + |
| 35 | + def __post_init__(self) -> None: |
| 36 | + if self.kind == "urlpath": |
| 37 | + if not isinstance(self.urlpath, str): |
| 38 | + raise TypeError("Ref(kind='urlpath') requires a string 'urlpath'") |
| 39 | + if self.key is not None or self.path is not None or self.urlbase is not None: |
| 40 | + raise ValueError("Ref(kind='urlpath') only supports the 'urlpath' field") |
| 41 | + return |
| 42 | + if self.kind == "dictstore_key": |
| 43 | + if not isinstance(self.urlpath, str): |
| 44 | + raise TypeError("Ref(kind='dictstore_key') requires a string 'urlpath'") |
| 45 | + if not isinstance(self.key, str): |
| 46 | + raise TypeError("Ref(kind='dictstore_key') requires a string 'key'") |
| 47 | + if self.path is not None or self.urlbase is not None: |
| 48 | + raise ValueError("Ref(kind='dictstore_key') only supports 'urlpath' and 'key'") |
| 49 | + return |
| 50 | + if self.kind == "c2array": |
| 51 | + if not isinstance(self.path, str): |
| 52 | + raise TypeError("Ref(kind='c2array') requires a string 'path'") |
| 53 | + if self.urlbase is not None and not isinstance(self.urlbase, str): |
| 54 | + raise TypeError("Ref(kind='c2array') requires 'urlbase' to be a string or None") |
| 55 | + if self.urlpath is not None or self.key is not None: |
| 56 | + raise ValueError("Ref(kind='c2array') only supports 'path' and 'urlbase'") |
| 57 | + return |
| 58 | + raise ValueError(f"Unsupported Ref kind: {self.kind!r}") |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def urlpath_ref(cls, urlpath: str) -> Ref: |
| 62 | + return cls(kind="urlpath", urlpath=urlpath) |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def dictstore_key(cls, urlpath: str, key: str) -> Ref: |
| 66 | + return cls(kind="dictstore_key", urlpath=urlpath, key=key) |
| 67 | + |
| 68 | + @classmethod |
| 69 | + def c2array_ref(cls, path: str, urlbase: str | None = None) -> Ref: |
| 70 | + return cls(kind="c2array", path=path, urlbase=urlbase) |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def from_dict(cls, payload: dict[str, Any]) -> Ref: |
| 74 | + if not isinstance(payload, dict): |
| 75 | + raise TypeError("Ref payload must be a mapping") |
| 76 | + version = payload.get("version") |
| 77 | + if version != 1: |
| 78 | + raise ValueError(f"Unsupported Ref payload version: {version!r}") |
| 79 | + return cls( |
| 80 | + kind=payload.get("kind"), |
| 81 | + urlpath=payload.get("urlpath"), |
| 82 | + key=payload.get("key"), |
| 83 | + path=payload.get("path"), |
| 84 | + urlbase=payload.get("urlbase"), |
| 85 | + ) |
| 86 | + |
| 87 | + @classmethod |
| 88 | + def from_object(cls, obj: Any) -> Ref: |
| 89 | + import blosc2 |
| 90 | + |
| 91 | + if isinstance(obj, blosc2.C2Array): |
| 92 | + return cls.c2array_ref(obj.path, obj.urlbase) |
| 93 | + if isinstance(obj, blosc2.Proxy): |
| 94 | + obj = obj._cache |
| 95 | + ref = getattr(obj, "_blosc2_ref", None) |
| 96 | + if isinstance(ref, cls): |
| 97 | + return ref |
| 98 | + if hasattr(obj, "schunk"): |
| 99 | + urlpath = obj.schunk.urlpath |
| 100 | + if urlpath is None: |
| 101 | + raise ValueError("Durable Blosc2 references require operands to be stored on disk/network") |
| 102 | + return cls.urlpath_ref(urlpath) |
| 103 | + raise TypeError("Durable Blosc2 references require NDArray, C2Array, or Proxy operands") |
| 104 | + |
| 105 | + def to_dict(self) -> dict[str, Any]: |
| 106 | + payload = {"kind": self.kind, "version": 1} |
| 107 | + if self.kind == "urlpath": |
| 108 | + payload["urlpath"] = self.urlpath |
| 109 | + elif self.kind == "dictstore_key": |
| 110 | + payload["urlpath"] = self.urlpath |
| 111 | + payload["key"] = self.key |
| 112 | + elif self.kind == "c2array": |
| 113 | + payload["path"] = self.path |
| 114 | + payload["urlbase"] = self.urlbase |
| 115 | + return payload |
| 116 | + |
| 117 | + def open(self): |
| 118 | + import blosc2 |
| 119 | + |
| 120 | + if self.kind == "urlpath": |
| 121 | + return blosc2.open(self.urlpath, mode="r") |
| 122 | + if self.kind == "dictstore_key": |
| 123 | + return blosc2.DictStore(self.urlpath, mode="r")[self.key] |
| 124 | + if self.kind == "c2array": |
| 125 | + return blosc2.C2Array(self.path, urlbase=self.urlbase) |
| 126 | + raise ValueError(f"Unsupported Ref kind: {self.kind!r}") |
0 commit comments