Skip to content

Refactor out _tofile/_fromfile from DirectoryStore #503

New issue

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

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

Already on GitHub? # to your account

Merged
merged 7 commits into from
Nov 12, 2019
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
3 changes: 3 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ Upcoming Release
* Use ``math.ceil`` for scalars.
By :user:`John Kirkham <jakirkham>`; :issue:`500`.

* Refactor out ``_tofile``/``_fromfile`` from ``DirectoryStore``.
By :user:`John Kirkham <jakirkham>`; :issue:`503`.

.. _release_2.3.2:

2.3.2
Expand Down
41 changes: 37 additions & 4 deletions zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,12 +746,46 @@ def __init__(self, path, normalize_keys=False):
def _normalize_key(self, key):
return key.lower() if self.normalize_keys else key

def _fromfile(self, fn):
""" Read data from a file

Parameters
----------
fn: str
Filepath to open and read from.

Notes
-----
Subclasses should overload this method to specify any custom
file reading logic.
"""
with open(fn, 'rb') as f:
return f.read()

def _tofile(self, a, fn):
""" Write data to a file

Parameters
----------
a: array-like
Data to write into the file.

fn: str
Filepath to open and write to.

Notes
-----
Subclasses should overload this method to specify any custom
file writing logic.
"""
with open(fn, mode='wb') as f:
f.write(a)

def __getitem__(self, key):
key = self._normalize_key(key)
filepath = os.path.join(self.path, key)
if os.path.isfile(filepath):
with open(filepath, 'rb') as f:
return f.read()
return self._fromfile(filepath)
else:
raise KeyError(key)

Expand Down Expand Up @@ -784,8 +818,7 @@ def __setitem__(self, key, value):
temp_name = file_name + '.' + uuid.uuid4().hex + '.partial'
temp_path = os.path.join(dir_path, temp_name)
try:
with open(temp_path, mode='wb') as f:
f.write(value)
self._tofile(value, temp_path)

# move temporary file into place
replace(temp_path, file_path)
Expand Down