|
9 | 9 | from dataclasses import asdict, dataclass, field, fields, replace |
10 | 10 | from functools import partial |
11 | 11 | from itertools import accumulate |
| 12 | +from pathlib import PurePosixPath |
12 | 13 | from typing import TYPE_CHECKING, Literal, TypeVar, assert_never, cast, overload |
13 | 14 |
|
14 | 15 | import numpy as np |
@@ -1424,6 +1425,33 @@ async def _members( |
1424 | 1425 | ): |
1425 | 1426 | yield member |
1426 | 1427 |
|
| 1428 | + # TODO: find a better name for this. create_tree could work. |
| 1429 | + # TODO: include an example in the docstring |
| 1430 | + async def create_hierarchy( |
| 1431 | + self, nodes: dict[str, ArrayV2Metadata | ArrayV3Metadata | GroupMetadata] |
| 1432 | + ) -> AsyncIterator[AsyncGroup | AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]]: |
| 1433 | + """ |
| 1434 | + Create a hierarchy of arrays or groups rooted at this group. |
| 1435 | +
|
| 1436 | + This method takes a dictionary where the keys are the names of the arrays or groups |
| 1437 | + to create and the values are the metadata or objects representing the arrays or groups. |
| 1438 | +
|
| 1439 | + The method returns an asynchronous iterator over the created nodes. |
| 1440 | +
|
| 1441 | + Parameters |
| 1442 | + ---------- |
| 1443 | + nodes : A dictionary representing the hierarchy to create |
| 1444 | +
|
| 1445 | + Returns |
| 1446 | + ------- |
| 1447 | + An asynchronous iterator over the created nodes. |
| 1448 | + """ |
| 1449 | + semaphore = asyncio.Semaphore(config.get("async.concurrency")) |
| 1450 | + async for node in create_hierarchy( |
| 1451 | + store_path=self.store_path, nodes=nodes, semaphore=semaphore |
| 1452 | + ): |
| 1453 | + yield node |
| 1454 | + |
1427 | 1455 | async def keys(self) -> AsyncGenerator[str, None]: |
1428 | 1456 | """Iterate over member names.""" |
1429 | 1457 | async for key, _ in self.members(): |
@@ -2046,6 +2074,32 @@ def members(self, max_depth: int | None = 0) -> tuple[tuple[str, Array | Group], |
2046 | 2074 |
|
2047 | 2075 | return tuple((kv[0], _parse_async_node(kv[1])) for kv in _members) |
2048 | 2076 |
|
| 2077 | + def create_hierarchy( |
| 2078 | + self, nodes: dict[str, ArrayV2Metadata | ArrayV3Metadata | GroupMetadata] |
| 2079 | + ) -> dict[str, AsyncGroup | AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]]: |
| 2080 | + """ |
| 2081 | + Create a hierarchy of arrays or groups rooted at this group. |
| 2082 | +
|
| 2083 | + This method takes a dictionary where the keys are the names of the arrays or groups |
| 2084 | + to create and the values are the metadata objects for the arrays or groups. |
| 2085 | +
|
| 2086 | + The method returns an asynchronous iterator over the created nodes. |
| 2087 | +
|
| 2088 | + Parameters |
| 2089 | + ---------- |
| 2090 | + nodes : A dictionary representing the hierarchy to create |
| 2091 | +
|
| 2092 | + Returns |
| 2093 | + ------- |
| 2094 | + A dict containing the created nodes.The keys are the same as th |
| 2095 | + """ |
| 2096 | + nodes_created = self._sync_iter(self._async_group.create_hierarchy(nodes)) |
| 2097 | + if self.path == "": |
| 2098 | + root = "/" |
| 2099 | + else: |
| 2100 | + root = self.path |
| 2101 | + return {str(PurePosixPath(n.name).relative_to(root)): n for n in nodes_created} |
| 2102 | + |
2049 | 2103 | def keys(self) -> Generator[str, None]: |
2050 | 2104 | """Return an iterator over group member names. |
2051 | 2105 |
|
|
0 commit comments