Skip to content

FSStore: key_separator fix #699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,8 @@ class FSStore(MutableMapping):
storage_options : passed to the fsspec implementation
"""

_META_KEYS = (attrs_key, group_meta_key, array_meta_key)

def __init__(self, url, normalize_keys=True, key_separator='.',
mode='w',
exceptions=(KeyError, PermissionError, IOError),
Expand All @@ -1039,7 +1041,11 @@ def _normalize_key(self, key):
key = normalize_storage_path(key).lstrip('/')
if key:
*bits, end = key.split('/')
key = '/'.join(bits + [end.replace('.', self.key_separator)])

if end not in FSStore._META_KEYS:
end = end.replace('.', self.key_separator)
key = '/'.join(bits + [end])

return key.lower() if self.normalize_keys else key

def getitems(self, keys, **kwargs):
Expand Down
37 changes: 33 additions & 4 deletions zarr/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,12 +863,27 @@ def mock_walker_no_slash(_path):
@pytest.mark.skipif(have_fsspec is False, reason="needs fsspec")
class TestFSStore(StoreTests, unittest.TestCase):

def create_store(self, normalize_keys=False):
def create_store(self, normalize_keys=False, key_separator="."):
path = tempfile.mkdtemp()
atexit.register(atexit_rmtree, path)
store = FSStore(path, normalize_keys=normalize_keys)
store = FSStore(
path,
normalize_keys=normalize_keys,
key_separator=key_separator)
return store

def test_key_separator(self):
for x in (".", "/"):
store = self.create_store(key_separator=x)
norm = store._normalize_key
assert ".zarray" == norm(".zarray")
assert ".zarray" == norm("/.zarray")
assert ".zgroup" == norm("/.zgroup")
assert "group/.zarray" == norm("group/.zarray")
assert "group/.zgroup" == norm("group/.zgroup")
assert "group/.zarray" == norm("/group/.zarray")
assert "group/.zgroup" == norm("/group/.zgroup")

def test_complex(self):
path1 = tempfile.mkdtemp()
path2 = tempfile.mkdtemp()
Expand Down Expand Up @@ -1183,13 +1198,27 @@ def test_filters(self):
@pytest.mark.skipif(have_fsspec is False, reason="needs fsspec")
class TestNestedFSStore(TestNestedDirectoryStore):

def create_store(self, normalize_keys=False):
path = tempfile.mkdtemp()
def create_store(self, normalize_keys=False, path=None):
if path is None:
path = tempfile.mkdtemp()
atexit.register(atexit_rmtree, path)
store = FSStore(path, normalize_keys=normalize_keys,
key_separator='/', auto_mkdir=True)
return store

def test_numbered_groups(self):
import zarr

# Create an array
store = self.create_store()
group = zarr.group(store=store)
arr = group.create_dataset('0', shape=(10, 10))
arr[1] = 1

# Read it back
store = self.create_store(path=store.path)
zarr.open_group(store.path)["0"]


class TestTempStore(StoreTests, unittest.TestCase):

Expand Down