Skip to content

Commit

Permalink
Merge pull request #68 from alimanfoo/issue_59
Browse files Browse the repository at this point in the history
implement TempStore; resolves #59
  • Loading branch information
alimanfoo committed Sep 9, 2016
2 parents ebaddd2 + 97f6671 commit 9a6de6e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/api/storage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ can be used as a Zarr array store.

.. autoclass:: DictStore
.. autoclass:: DirectoryStore
.. autoclass:: TempStore
.. autoclass:: ZipStore

.. automethod:: close
Expand Down
3 changes: 3 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Release notes
=============

* Added :class:`zarr.storage.TempStore` class for convenience to provide
storage via a temporary directory
(`#59 <https://github.com/alimanfoo/zarr/issues/59>`_)
* Fixed performance issues with ``ZipStore`` class
(`#66 <https://github.com/alimanfoo/zarr/issues/27>`_)
* The Blosc extension has been modified to return bytes instead of array
Expand Down
2 changes: 1 addition & 1 deletion zarr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from zarr.core import Array
from zarr.creation import empty, zeros, ones, full, array, empty_like, \
zeros_like, ones_like, full_like, open, open_array, open_like, create
from zarr.storage import DictStore, DirectoryStore, ZipStore
from zarr.storage import DictStore, DirectoryStore, ZipStore, TempStore
from zarr.hierarchy import group, open_group, Group
from zarr.sync import ThreadSynchronizer, ProcessSynchronizer
from zarr.codecs import *
Expand Down
18 changes: 18 additions & 0 deletions zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
import zipfile
import shutil
import atexit


import numpy as np
Expand Down Expand Up @@ -717,6 +718,23 @@ def getsize(self, path=None):
err_path_not_found(path)


def _atexit_rmtree(path,
isdir=os.path.isdir,
rmtree=shutil.rmtree): # pragma: no cover
"""Ensure directory removal at interpreter exit."""
if isdir(path):
rmtree(path)


class TempStore(DirectoryStore):
"""Directory store using a temporary directory for storage."""

def __init__(self, suffix='', prefix='zarr', dir=None):
path = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=dir)
atexit.register(_atexit_rmtree, path)
super(TempStore, self).__init__(path)


# noinspection PyPep8Naming
class ZipStore(MutableMapping):
"""Mutable Mapping interface to a Zip file. Keys must be strings,
Expand Down
13 changes: 12 additions & 1 deletion zarr/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@


from zarr.storage import init_array, array_meta_key, attrs_key, DictStore, \
DirectoryStore, ZipStore, init_group, group_meta_key, getsize, migrate_1to2
DirectoryStore, ZipStore, init_group, group_meta_key, getsize, \
migrate_1to2, TempStore
from zarr.meta import decode_array_metadata, encode_array_metadata, \
ZARR_FORMAT, decode_group_metadata, encode_group_metadata
from zarr.compat import text_type
Expand Down Expand Up @@ -619,6 +620,16 @@ def test_setdel(self):
setdel_hierarchy_checks(store)


class TestTempStore(StoreTests, unittest.TestCase):

def create_store(self):
return TempStore()

def test_setdel(self):
store = self.create_store()
setdel_hierarchy_checks(store)


class TestZipStore(StoreTests, unittest.TestCase):

def create_store(self):
Expand Down

0 comments on commit 9a6de6e

Please sign in to comment.