Skip to content

Commit 9a6de6e

Browse files
authored
Merge pull request #68 from alimanfoo/issue_59
implement TempStore; resolves #59
2 parents ebaddd2 + 97f6671 commit 9a6de6e

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-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
.. automethod:: close

docs/release.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
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>`_)
47
* Fixed performance issues with ``ZipStore`` class
58
(`#66 <https://github.com/alimanfoo/zarr/issues/27>`_)
69
* The Blosc extension has been modified to return bytes instead of array

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
@@ -717,6 +718,23 @@ def getsize(self, path=None):
717718
err_path_not_found(path)
718719

719720

721+
def _atexit_rmtree(path,
722+
isdir=os.path.isdir,
723+
rmtree=shutil.rmtree): # pragma: no cover
724+
"""Ensure directory removal at interpreter exit."""
725+
if isdir(path):
726+
rmtree(path)
727+
728+
729+
class TempStore(DirectoryStore):
730+
"""Directory store using a temporary directory for storage."""
731+
732+
def __init__(self, suffix='', prefix='zarr', dir=None):
733+
path = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=dir)
734+
atexit.register(_atexit_rmtree, path)
735+
super(TempStore, self).__init__(path)
736+
737+
720738
# noinspection PyPep8Naming
721739
class ZipStore(MutableMapping):
722740
"""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
@@ -619,6 +620,16 @@ def test_setdel(self):
619620
setdel_hierarchy_checks(store)
620621

621622

623+
class TestTempStore(StoreTests, unittest.TestCase):
624+
625+
def create_store(self):
626+
return TempStore()
627+
628+
def test_setdel(self):
629+
store = self.create_store()
630+
setdel_hierarchy_checks(store)
631+
632+
622633
class TestZipStore(StoreTests, unittest.TestCase):
623634

624635
def create_store(self):

0 commit comments

Comments
 (0)