Skip to content

Commit 50913cb

Browse files
author
Shikhar Goenka
committed
fixed bug
1 parent 121e9ec commit 50913cb

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

zarr/storage.py

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,7 +1964,7 @@ class ABSStore(MutableMapping):
19641964
In order to use this store, you must install the Microsoft Azure Storage SDK for Python.
19651965
"""
19661966

1967-
def __init__(self, container, prefix, account_name=None, account_key=None,
1967+
def __init__(self, container, prefix=None, account_name=None, account_key=None,
19681968
blob_service_kwargs=None):
19691969
from azure.storage.blob import BlockBlobService
19701970
self.container = container
@@ -1990,21 +1990,26 @@ def __setstate__(self, state):
19901990
self.client = BlockBlobService(self.account_name, self.account_key,
19911991
**self.blob_service_kwargs)
19921992

1993-
@staticmethod
1994-
def _append_path_to_prefix(path, prefix):
1995-
return '/'.join([normalize_storage_path(prefix),
1996-
normalize_storage_path(path)])
1993+
def _append_path_to_prefix(self, path):
1994+
if self.prefix == '':
1995+
return normalize_storage_path(path)
1996+
else:
1997+
return '/'.join([normalize_storage_path(self.prefix),
1998+
normalize_storage_path(path)])
19971999

19982000
@staticmethod
19992001
def _strip_prefix_from_path(path, prefix):
20002002
# normalized things will not have any leading or trailing slashes
20012003
path_norm = normalize_storage_path(path)
20022004
prefix_norm = normalize_storage_path(prefix)
2003-
return path_norm[(len(prefix_norm)+1):]
2005+
if prefix:
2006+
return path_norm[(len(prefix_norm)+1):]
2007+
else:
2008+
return path_norm
20042009

20052010
def __getitem__(self, key):
20062011
from azure.common import AzureMissingResourceHttpError
2007-
blob_name = '/'.join([self.prefix, key])
2012+
blob_name = self._append_path_to_prefix(key)
20082013
try:
20092014
blob = self.client.get_blob_to_bytes(self.container, blob_name)
20102015
return blob.content
@@ -2013,13 +2018,13 @@ def __getitem__(self, key):
20132018

20142019
def __setitem__(self, key, value):
20152020
value = ensure_bytes(value)
2016-
blob_name = '/'.join([self.prefix, key])
2021+
blob_name = self._append_path_to_prefix(key)
20172022
self.client.create_blob_from_bytes(self.container, blob_name, value)
20182023

20192024
def __delitem__(self, key):
20202025
from azure.common import AzureMissingResourceHttpError
20212026
try:
2022-
self.client.delete_blob(self.container, '/'.join([self.prefix, key]))
2027+
self.client.delete_blob(self.container, self._append_path_to_prefix(key))
20232028
except AzureMissingResourceHttpError:
20242029
raise KeyError('Blob %s not found' % key)
20252030

@@ -2034,26 +2039,27 @@ def keys(self):
20342039
return list(self.__iter__())
20352040

20362041
def __iter__(self):
2037-
for blob in self.client.list_blobs(self.container, self.prefix + '/'):
2042+
if self.prefix:
2043+
list_blobs_prefix = self.prefix + '/'
2044+
else:
2045+
list_blobs_prefix = None
2046+
for blob in self.client.list_blobs(self.container, list_blobs_prefix):
20382047
yield self._strip_prefix_from_path(blob.name, self.prefix)
20392048

20402049
def __len__(self):
20412050
return len(self.keys())
20422051

20432052
def __contains__(self, key):
2044-
blob_name = '/'.join([self.prefix, key])
2053+
blob_name = self._append_path_to_prefix(key)
20452054
if self.client.exists(self.container, blob_name):
20462055
return True
20472056
else:
20482057
return False
20492058

20502059
def listdir(self, path=None):
2051-
store_path = normalize_storage_path(path)
2052-
# prefix is normalized to not have a trailing slash
2053-
dir_path = self.prefix
2054-
if store_path:
2055-
dir_path = dir_path + '/' + store_path
2056-
dir_path += '/'
2060+
dir_path = normalize_storage_path(self._append_path_to_prefix(path))
2061+
if dir_path:
2062+
dir_path += '/'
20572063
items = list()
20582064
for blob in self.client.list_blobs(self.container, prefix=dir_path, delimiter='/'):
20592065
if '/' in blob.name[len(dir_path):]:
@@ -2064,23 +2070,30 @@ def listdir(self, path=None):
20642070
return items
20652071

20662072
def rmdir(self, path=None):
2067-
dir_path = normalize_storage_path(self._append_path_to_prefix(path, self.prefix)) + '/'
2073+
dir_path = normalize_storage_path(self._append_path_to_prefix(path))
2074+
if dir_path:
2075+
dir_path += '/'
20682076
for blob in self.client.list_blobs(self.container, prefix=dir_path):
20692077
self.client.delete_blob(self.container, blob.name)
20702078

20712079
def getsize(self, path=None):
2080+
from azure.storage.blob.models import Blob
20722081
store_path = normalize_storage_path(path)
20732082
fs_path = self.prefix
20742083
if store_path:
2075-
fs_path = self._append_path_to_prefix(store_path, self.prefix)
2084+
fs_path = self._append_path_to_prefix(store_path)
20762085
if self.client.exists(self.container, fs_path):
20772086
return self.client.get_blob_properties(self.container,
20782087
fs_path).properties.content_length
20792088
else:
20802089
size = 0
2081-
for blob in self.client.list_blobs(self.container, prefix=fs_path + '/',
2090+
if fs_path == '':
2091+
fs_path = None
2092+
else:
2093+
fs_path += '/'
2094+
for blob in self.client.list_blobs(self.container, prefix=fs_path,
20822095
delimiter='/'):
2083-
if '/' not in blob.name[len(fs_path + '/'):]:
2096+
if type(blob) == Blob:
20842097
size += blob.properties.content_length
20852098
return size
20862099

zarr/tests/test_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,8 +1404,8 @@ def absstore():
14041404
blob_client = asb.BlockBlobService(is_emulated=True)
14051405
blob_client.delete_container('test')
14061406
blob_client.create_container('test')
1407-
store = ABSStore(container='test', prefix='zarrtesting/', account_name='foo',
1408-
account_key='bar', blob_service_kwargs={'is_emulated': True})
1407+
store = ABSStore(container='test', account_name='foo', account_key='bar',
1408+
blob_service_kwargs={'is_emulated': True})
14091409
store.rmdir()
14101410
return store
14111411

zarr/tests/test_hierarchy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,8 @@ def create_store():
894894
blob_client = asb.BlockBlobService(is_emulated=True)
895895
blob_client.delete_container('test')
896896
blob_client.create_container('test')
897-
store = ABSStore(container='test', prefix='zarrtesting/', account_name='foo',
898-
account_key='bar', blob_service_kwargs={'is_emulated': True})
897+
store = ABSStore(container='test', account_name='foo', account_key='bar',
898+
blob_service_kwargs={'is_emulated': True})
899899
store.rmdir()
900900
return store, None
901901

zarr/tests/test_storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,8 +1478,8 @@ def create_store(self):
14781478
blob_client = asb.BlockBlobService(is_emulated=True)
14791479
blob_client.delete_container('test')
14801480
blob_client.create_container('test')
1481-
store = ABSStore(container='test', prefix='zarrtesting/', account_name='foo',
1482-
account_key='bar', blob_service_kwargs={'is_emulated': True})
1481+
store = ABSStore(container='test', account_name='foo', account_key='bar',
1482+
blob_service_kwargs={'is_emulated': True})
14831483
store.rmdir()
14841484
return store
14851485

0 commit comments

Comments
 (0)