|
10 | 10 | from zarr.attrs import Attributes
|
11 | 11 | from zarr.core import Array
|
12 | 12 | from zarr.storage import (contains_array, contains_group, init_group,
|
13 |
| - DictStore, group_meta_key, attrs_key, listdir, rmdir) |
| 13 | + DictStore, group_meta_key, attrs_key, listdir, rename, rmdir) |
14 | 14 | from zarr.creation import (array, create, empty, zeros, ones, full,
|
15 | 15 | empty_like, zeros_like, ones_like, full_like,
|
16 | 16 | normalize_store_arg)
|
@@ -81,6 +81,7 @@ class Group(MutableMapping):
|
81 | 81 | ones_like
|
82 | 82 | full_like
|
83 | 83 | info
|
| 84 | + move |
84 | 85 |
|
85 | 86 | """
|
86 | 87 |
|
@@ -934,6 +935,37 @@ def _full_like_nosync(self, name, data, **kwargs):
|
934 | 935 | return full_like(data, store=self._store, path=path,
|
935 | 936 | chunk_store=self._chunk_store, **kwargs)
|
936 | 937 |
|
| 938 | + def _move_nosync(self, path, new_path): |
| 939 | + rename(self._store, path, new_path) |
| 940 | + if self._chunk_store is not None: |
| 941 | + rename(self._chunk_store, path, new_path) |
| 942 | + |
| 943 | + def move(self, source, dest): |
| 944 | + """Move contents from one path to another relative to the Group. |
| 945 | +
|
| 946 | + Parameters |
| 947 | + ---------- |
| 948 | + source : string |
| 949 | + Name or path to a Zarr object to move. |
| 950 | + dest : string |
| 951 | + New name or path of the Zarr object. |
| 952 | + """ |
| 953 | + |
| 954 | + source = self._item_path(source) |
| 955 | + dest = self._item_path(dest) |
| 956 | + |
| 957 | + # Check that source exists. |
| 958 | + if not (contains_array(self._store, source) or contains_group(self._store, source)): |
| 959 | + raise ValueError('The source, "%s", does not exist.' % source) |
| 960 | + if contains_array(self._store, dest) or contains_group(self._store, dest): |
| 961 | + raise ValueError('The dest, "%s", already exists.' % dest) |
| 962 | + |
| 963 | + # Ensure groups needed for `dest` exist. |
| 964 | + if "/" in dest: |
| 965 | + self.require_group("/" + dest.rsplit("/", 1)[0]) |
| 966 | + |
| 967 | + self._write_op(self._move_nosync, source, dest) |
| 968 | + |
937 | 969 |
|
938 | 970 | def _normalize_store_arg(store, clobber=False):
|
939 | 971 | return normalize_store_arg(store, clobber=clobber, default=DictStore)
|
|
0 commit comments