|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from zarr.core.group import Group, GroupMetadata, _parse_async_node |
| 6 | +from zarr.core.group import create_hierarchy as create_hierarchy_async |
| 7 | +from zarr.core.group import create_nodes as create_nodes_async |
| 8 | +from zarr.core.group import create_rooted_hierarchy as create_rooted_hierarchy_async |
| 9 | +from zarr.core.group import get_node as get_node_async |
| 10 | +from zarr.core.sync import _collect_aiterator, sync |
| 11 | + |
| 12 | +if TYPE_CHECKING: |
| 13 | + from collections.abc import Iterator |
| 14 | + |
| 15 | + from zarr.abc.store import Store |
| 16 | + from zarr.core.array import Array |
| 17 | + from zarr.core.common import ZarrFormat |
| 18 | + from zarr.core.metadata import ArrayV2Metadata, ArrayV3Metadata |
| 19 | + |
| 20 | + |
| 21 | +def create_nodes( |
| 22 | + *, store: Store, nodes: dict[str, GroupMetadata | ArrayV2Metadata | ArrayV3Metadata] |
| 23 | +) -> Iterator[tuple[str, Group | Array]]: |
| 24 | + """Create a collection of arrays and / or groups concurrently. |
| 25 | +
|
| 26 | + Note: no attempt is made to validate that these arrays and / or groups collectively form a |
| 27 | + valid Zarr hierarchy. It is the responsibility of the caller of this function to ensure that |
| 28 | + the ``nodes`` parameter satisfies any correctness constraints. |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + store : Store |
| 33 | + The storage backend to use. |
| 34 | + nodes : dict[str, GroupMetadata | ArrayV3Metadata | ArrayV2Metadata] |
| 35 | + A dictionary defining the hierarchy. The keys are the paths of the nodes |
| 36 | + in the hierarchy, and the values are the metadata of the nodes. The |
| 37 | + metadata must be either an instance of GroupMetadata, ArrayV3Metadata |
| 38 | + or ArrayV2Metadata. |
| 39 | +
|
| 40 | + Yields |
| 41 | + ------ |
| 42 | + Group | Array |
| 43 | + The created nodes. |
| 44 | + """ |
| 45 | + coro = create_nodes_async(store=store, nodes=nodes) |
| 46 | + |
| 47 | + for key, value in sync(_collect_aiterator(coro)): |
| 48 | + yield key, _parse_async_node(value) |
| 49 | + |
| 50 | + |
| 51 | +def create_hierarchy( |
| 52 | + *, |
| 53 | + store: Store, |
| 54 | + nodes: dict[str, GroupMetadata | ArrayV2Metadata | ArrayV3Metadata], |
| 55 | + overwrite: bool = False, |
| 56 | +) -> Iterator[tuple[str, Group | Array]]: |
| 57 | + """ |
| 58 | + Create a complete zarr hierarchy from a collection of metadata objects. |
| 59 | +
|
| 60 | + Groups that are implicitly defined by the input will be created as needed. |
| 61 | +
|
| 62 | + This function takes a parsed hierarchy dictionary and creates all the nodes in the hierarchy |
| 63 | + concurrently. Arrays and Groups are yielded in the order they are created. |
| 64 | +
|
| 65 | + Parameters |
| 66 | + ---------- |
| 67 | + store : Store |
| 68 | + The storage backend to use. |
| 69 | + nodes : dict[str, GroupMetadata | ArrayV3Metadata | ArrayV2Metadata] |
| 70 | + A dictionary defining the hierarchy. The keys are the paths of the nodes |
| 71 | + in the hierarchy, and the values are the metadata of the nodes. The |
| 72 | + metadata must be either an instance of GroupMetadata, ArrayV3Metadata |
| 73 | + or ArrayV2Metadata. |
| 74 | +
|
| 75 | + Yields |
| 76 | + ------ |
| 77 | + Group | Array |
| 78 | + The created nodes in the order they are created. |
| 79 | + """ |
| 80 | + coro = create_hierarchy_async(store=store, nodes=nodes, overwrite=overwrite) |
| 81 | + |
| 82 | + for key, value in sync(_collect_aiterator(coro)): |
| 83 | + yield key, _parse_async_node(value) |
| 84 | + |
| 85 | + |
| 86 | +def create_rooted_hierarchy( |
| 87 | + *, |
| 88 | + store: Store, |
| 89 | + nodes: dict[str, GroupMetadata | ArrayV2Metadata | ArrayV3Metadata], |
| 90 | + overwrite: bool = False, |
| 91 | +) -> Group | Array: |
| 92 | + """ |
| 93 | + Create a Zarr hierarchy with a root, and return the root node, which could be a ``Group`` |
| 94 | + or ``Array`` instance. |
| 95 | +
|
| 96 | + Parameters |
| 97 | + ---------- |
| 98 | + store : Store |
| 99 | + The storage backend to use. |
| 100 | + nodes : dict[str, GroupMetadata | ArrayV3Metadata | ArrayV2Metadata] |
| 101 | + A dictionary defining the hierarchy. The keys are the paths of the nodes |
| 102 | + in the hierarchy, and the values are the metadata of the nodes. The |
| 103 | + metadata must be either an instance of GroupMetadata, ArrayV3Metadata |
| 104 | + or ArrayV2Metadata. |
| 105 | + overwrite : bool |
| 106 | + Whether to overwrite existing nodes. Default is ``False``. |
| 107 | +
|
| 108 | + Returns |
| 109 | + ------- |
| 110 | + Group | Array |
| 111 | + """ |
| 112 | + async_node = sync(create_rooted_hierarchy_async(store=store, nodes=nodes, overwrite=overwrite)) |
| 113 | + return _parse_async_node(async_node) |
| 114 | + |
| 115 | + |
| 116 | +def get_node(store: Store, path: str, zarr_format: ZarrFormat) -> Array | Group: |
| 117 | + """ |
| 118 | + Get an Array or Group from a path in a Store. |
| 119 | +
|
| 120 | + Parameters |
| 121 | + ---------- |
| 122 | + store : Store |
| 123 | + The store-like object to read from. |
| 124 | + path : str |
| 125 | + The path to the node to read. |
| 126 | + zarr_format : {2, 3} |
| 127 | + The zarr format of the node to read. |
| 128 | +
|
| 129 | + Returns |
| 130 | + ------- |
| 131 | + Array | Group |
| 132 | + """ |
| 133 | + |
| 134 | + return _parse_async_node(sync(get_node_async(store=store, path=path, zarr_format=zarr_format))) |
0 commit comments