|
| 1 | +""" |
| 2 | +Abstract base class and data model for URL pipeline adapters. |
| 3 | +
|
| 4 | +A URL pipeline is a `|`-separated chain of sub-URLs, read outer-to-inner, |
| 5 | +as specified by https://github.com/jbms/url-pipeline. The first sub-URL (the |
| 6 | +*root*) locates a resource using a conventional URL, and each subsequent |
| 7 | +sub-URL names an *adapter* that reinterprets everything to its left: |
| 8 | +
|
| 9 | + s3://bucket/data.zip|zip:path/inside|zarr3: |
| 10 | +
|
| 11 | +Third-party packages provide adapters by subclassing |
| 12 | +[`URLPipelineAdapter`][zarr.abc.url_pipeline.URLPipelineAdapter] and |
| 13 | +registering the class under the `zarr.url_adapters` entry-point group, |
| 14 | +using the URL scheme as the entry-point name. |
| 15 | +""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +from abc import ABC, abstractmethod |
| 20 | +from dataclasses import dataclass, field |
| 21 | +from typing import TYPE_CHECKING, Any |
| 22 | + |
| 23 | +if TYPE_CHECKING: |
| 24 | + from collections.abc import Awaitable, Callable |
| 25 | + |
| 26 | + from zarr.abc.store import Store |
| 27 | + from zarr.core.common import AccessModeLiteral |
| 28 | + |
| 29 | +__all__ = [ |
| 30 | + "AdapterResolution", |
| 31 | + "PipelineContext", |
| 32 | + "PipelineSegment", |
| 33 | + "URLPipelineAdapter", |
| 34 | +] |
| 35 | + |
| 36 | + |
| 37 | +@dataclass(frozen=True) |
| 38 | +class PipelineSegment: |
| 39 | + """ |
| 40 | + One `|`-delimited sub-URL of a URL pipeline. |
| 41 | +
|
| 42 | + Attributes |
| 43 | + ---------- |
| 44 | + scheme : str |
| 45 | + The lowercased URL scheme. Empty string only for a schemeless root |
| 46 | + (a bare local path). |
| 47 | + body : str |
| 48 | + The text after `scheme:` and before any `?`. Interpretation is |
| 49 | + scheme-defined; it is **not** URL-normalized, so case-significant |
| 50 | + content (e.g. icechunk snapshot IDs) is preserved. |
| 51 | + query : str | None |
| 52 | + The raw query string after `?`, or None. Interpretation is |
| 53 | + scheme-defined. |
| 54 | + raw : str |
| 55 | + The exact original sub-URL text, preserved for lossless |
| 56 | + reconstruction of the pipeline. |
| 57 | + """ |
| 58 | + |
| 59 | + scheme: str |
| 60 | + body: str |
| 61 | + query: str | None |
| 62 | + raw: str |
| 63 | + |
| 64 | + def __str__(self) -> str: |
| 65 | + return self.raw |
| 66 | + |
| 67 | + |
| 68 | +@dataclass(frozen=True) |
| 69 | +class AdapterResolution: |
| 70 | + """ |
| 71 | + The result of resolving a URL pipeline (or a prefix of one). |
| 72 | +
|
| 73 | + Attributes |
| 74 | + ---------- |
| 75 | + store : Store |
| 76 | + The resolved store. |
| 77 | + path : str |
| 78 | + Residual path *within* the store that the pipeline addresses |
| 79 | + (e.g. `"path/to/node"` for `...|icechunk://tag.v1/path/to/node`). |
| 80 | + Empty string when the pipeline addresses the store root. |
| 81 | + """ |
| 82 | + |
| 83 | + store: Store |
| 84 | + path: str = "" |
| 85 | + |
| 86 | + |
| 87 | +@dataclass(frozen=True) |
| 88 | +class PipelineContext: |
| 89 | + """ |
| 90 | + Context handed to a [`URLPipelineAdapter`][zarr.abc.url_pipeline.URLPipelineAdapter] |
| 91 | + describing the pipeline to the left of its segment. |
| 92 | +
|
| 93 | + Attributes |
| 94 | + ---------- |
| 95 | + preceding : tuple[PipelineSegment, ...] |
| 96 | + The parsed sub-URLs to the left of the adapter's segment, outer to |
| 97 | + inner. Empty when the adapter's segment is the pipeline root. |
| 98 | + mode : AccessModeLiteral | None |
| 99 | + The access mode requested by the caller (e.g. `zarr.open(mode=...)`), |
| 100 | + or None when unspecified. Adapters for read-only resources should |
| 101 | + raise for unambiguous write modes (`"w"`, `"w-"`, `"r+"`) and |
| 102 | + open read-only otherwise. `"a"` (the `zarr.open` default) means |
| 103 | + open-or-create: read-only adapters serve the "open" half, and any |
| 104 | + subsequent write fails at the store level. |
| 105 | + read_only : bool |
| 106 | + True when the caller requires a read-only store (`mode == "r"`). |
| 107 | + Adapters must construct their store read-only when this is set; |
| 108 | + when it is False, they may construct a writable store if the |
| 109 | + underlying resource supports writing. |
| 110 | + storage_options : dict[str, Any] | None |
| 111 | + Options passed by the caller. By convention these configure the |
| 112 | + *root* sub-URL (e.g. fsspec options), but adapters may consume |
| 113 | + adapter-specific keys. |
| 114 | + """ |
| 115 | + |
| 116 | + preceding: tuple[PipelineSegment, ...] |
| 117 | + mode: AccessModeLiteral | None |
| 118 | + read_only: bool |
| 119 | + storage_options: dict[str, Any] | None |
| 120 | + _resolver: Callable[[tuple[PipelineSegment, ...]], Awaitable[AdapterResolution]] = field( |
| 121 | + repr=False |
| 122 | + ) |
| 123 | + |
| 124 | + @property |
| 125 | + def preceding_url(self) -> str: |
| 126 | + """ |
| 127 | + The pipeline to the left of this segment, reconstructed exactly. |
| 128 | +
|
| 129 | + An adapter that consumes this string instead of calling |
| 130 | + [`resolve_preceding`][zarr.abc.url_pipeline.PipelineContext.resolve_preceding] |
| 131 | + takes ownership of the *entire* preceding pipeline: it must |
| 132 | + validate every preceding segment itself and raise |
| 133 | + [`URLPipelineError`][zarr.errors.URLPipelineError] for segments it |
| 134 | + does not understand, so that no segment is ever silently ignored. |
| 135 | + """ |
| 136 | + return "|".join(segment.raw for segment in self.preceding) |
| 137 | + |
| 138 | + async def resolve_preceding(self) -> AdapterResolution: |
| 139 | + """ |
| 140 | + Resolve the preceding pipeline into a store. |
| 141 | +
|
| 142 | + This is the entry point for *wrapper* adapters (e.g. `zip:`) that |
| 143 | + operate on the resource produced by the segments to their left. It |
| 144 | + composes with any preceding adapters, because each segment is |
| 145 | + resolved by its own adapter. Adapters backed by their own I/O |
| 146 | + machinery (e.g. `icechunk:`) may instead consume |
| 147 | + [`preceding_url`][zarr.abc.url_pipeline.PipelineContext.preceding_url] |
| 148 | + and never materialize the intermediate store — subject to the |
| 149 | + ownership contract documented there. |
| 150 | + """ |
| 151 | + return await self._resolver(self.preceding) |
| 152 | + |
| 153 | + |
| 154 | +class URLPipelineAdapter(ABC): |
| 155 | + """ |
| 156 | + Handler for one URL pipeline scheme. |
| 157 | +
|
| 158 | + Subclasses implement a single classmethod, |
| 159 | + [`open_pipeline_segment`][zarr.abc.url_pipeline.URLPipelineAdapter.open_pipeline_segment], |
| 160 | + and are registered under the `zarr.url_adapters` entry-point group with |
| 161 | + the URL scheme as the entry-point name: |
| 162 | +
|
| 163 | + [project.entry-points."zarr.url_adapters"] |
| 164 | + myscheme = "mypackage.zarr_adapter:MyAdapter" |
| 165 | +
|
| 166 | + An adapter is used in two positions: |
| 167 | +
|
| 168 | + - as an *adapter segment*: `s3://bucket/repo|icechunk://tag.v1` — the |
| 169 | + context carries the preceding sub-URLs; |
| 170 | + - as a *root scheme*: `gh://org/repo` — `context.preceding` is empty. |
| 171 | + """ |
| 172 | + |
| 173 | + @classmethod |
| 174 | + @abstractmethod |
| 175 | + async def open_pipeline_segment( |
| 176 | + cls, segment: PipelineSegment, context: PipelineContext |
| 177 | + ) -> AdapterResolution: |
| 178 | + """ |
| 179 | + Resolve `segment` (in the context of the pipeline to its left) |
| 180 | + into a store and an optional residual path within that store. |
| 181 | +
|
| 182 | + The returned store must already be open and must honor |
| 183 | + `context.read_only`. |
| 184 | + """ |
| 185 | + ... |
0 commit comments