diff --git a/docs/release.rst b/docs/release.rst index 0aad67ef8a..42297edc3e 100644 --- a/docs/release.rst +++ b/docs/release.rst @@ -40,6 +40,9 @@ Upcoming Release * Use ``math.ceil`` for scalars. By :user:`John Kirkham `; :issue:`500`. +* Refactor out ``_tofile``/``_fromfile`` from ``DirectoryStore``. + By :user:`John Kirkham `; :issue:`503`. + .. _release_2.3.2: 2.3.2 diff --git a/zarr/storage.py b/zarr/storage.py index b2ed796580..5a33940ad5 100644 --- a/zarr/storage.py +++ b/zarr/storage.py @@ -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) @@ -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)