Skip to content

HTTP Store #373

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 39 additions & 0 deletions zarr/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
# POSIX rename() is always atomic
from os import rename as replace

# Import the proper urljoin function
if sys.version_info.major == 3:
from urllib.parse import urljoin
else:
from urlparse import urljoin

def _path_to_prefix(path):
# assume path already normalized
Expand Down Expand Up @@ -1954,3 +1959,37 @@ def getsize(self, path):

def listdir(self, path):
return listdir(self.meta_store, path)


class HTTPStore(MutableMapping):

def __init__(self, url_base):
import requests
self.url_base = url_base
self.session = requests.Session()

def __getitem__(self, key):
r = self.session.get(urljoin(self.url_base, key))
r.raise_for_status()
# is it correct to always return bytes?
return r.content

def __contains__(self, key):
h = self.session.head(urljoin(self.url_base, key))
return h.status_code == 200

def _not_implemented_error(self):
raise NotImplementedError('HTTPStore is designed to be used only with '
'consolidated metadata')

def __setitem__(self, key, value):
self._not_implemented_error()

def __delitem__(self, key):
self._not_implemented_error()

def __iter__(self):
self._not_implemented_error()

def __len__(self):
self._not_implemented_error()