Skip to content

Commit badc7d8

Browse files
committed
implement TempStore; resolves #59
1 parent 96c34bb commit badc7d8

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
lines changed

docs/api/storage.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ can be used as a Zarr array store.
1111

1212
.. autoclass:: DictStore
1313
.. autoclass:: DirectoryStore
14+
.. autoclass:: TempStore
1415
.. autoclass:: ZipStore
1516

1617
.. autofunction:: migrate_1to2

docs/release.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Release notes
22
=============
33

4+
* Added :class:`zarr.storage.TempStore` class for convenience to provide
5+
storage via a temporary directory
6+
(`#59 <https://github.com/alimanfoo/zarr/issues/59>`_)
7+
48
.. _release_2.0.1:
59

610
2.0.1

zarr/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from zarr.core import Array
77
from zarr.creation import empty, zeros, ones, full, array, empty_like, \
88
zeros_like, ones_like, full_like, open, open_array, open_like, create
9-
from zarr.storage import DictStore, DirectoryStore, ZipStore
9+
from zarr.storage import DictStore, DirectoryStore, ZipStore, TempStore
1010
from zarr.hierarchy import group, open_group, Group
1111
from zarr.sync import ThreadSynchronizer, ProcessSynchronizer
1212
from zarr.codecs import *

zarr/storage.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import zipfile
88
import shutil
9+
import atexit
910

1011

1112
import numpy as np
@@ -676,6 +677,23 @@ def getsize(self, path=None):
676677
raise ValueError('path not found: %r' % path)
677678

678679

680+
def _atexit_rmtree(path,
681+
isdir=os.path.isdir,
682+
rmtree=shutil.rmtree): # pragma: no cover
683+
"""Ensure directory removal at interpreter exit."""
684+
if isdir(path):
685+
rmtree(path)
686+
687+
688+
class TempStore(DirectoryStore):
689+
"""Directory store using a temporary directory for storage."""
690+
691+
def __init__(self, suffix='', prefix='zarr', dir=None):
692+
path = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=dir)
693+
atexit.register(_atexit_rmtree, path)
694+
super(TempStore, self).__init__(path)
695+
696+
679697
# noinspection PyPep8Naming
680698
class ZipStore(MutableMapping):
681699
"""Mutable Mapping interface to a Zip file. Keys must be strings,

zarr/tests/test_storage.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616

1717
from zarr.storage import init_array, array_meta_key, attrs_key, DictStore, \
18-
DirectoryStore, ZipStore, init_group, group_meta_key, getsize, migrate_1to2
18+
DirectoryStore, ZipStore, init_group, group_meta_key, getsize, \
19+
migrate_1to2, TempStore
1920
from zarr.meta import decode_array_metadata, encode_array_metadata, \
2021
ZARR_FORMAT, decode_group_metadata, encode_group_metadata
2122
from zarr.compat import text_type
@@ -627,6 +628,16 @@ def test_setdel(self):
627628
setdel_hierarchy_checks(store)
628629

629630

631+
class TestTempStore(StoreTests, unittest.TestCase):
632+
633+
def create_store(self):
634+
return TempStore()
635+
636+
def test_setdel(self):
637+
store = self.create_store()
638+
setdel_hierarchy_checks(store)
639+
640+
630641
class TestZipStore(StoreTests, unittest.TestCase):
631642

632643
def create_store(self):

0 commit comments

Comments
 (0)