Skip to content

Commit 88e1ee4

Browse files
committed
Extract ABSStore to zarr._storage.absstore
First step towards being able to assign CODEOWNERS to simplify maintenance. This does not yet attempt the rename to zarr.storage.absstore in order lower conflicts with open PRs. Imports should remain un- changed for the moment. see: zarr-developers#764
1 parent efd94fa commit 88e1ee4

File tree

1 file changed

+2
-191
lines changed

1 file changed

+2
-191
lines changed

zarr/storage.py

+2-191
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@
5656
normalize_dtype, normalize_fill_value, normalize_order,
5757
normalize_shape, normalize_storage_path, retry_call)
5858

59+
from zarr._storage.abstore import ABSStore
60+
5961
__doctest_requires__ = {
6062
('RedisStore', 'RedisStore.*'): ['redis'],
6163
('MongoDBStore', 'MongoDBStore.*'): ['pymongo'],
62-
('ABSStore', 'ABSStore.*'): ['azure.storage.blob'],
6364
('LRUStoreCache', 'LRUStoreCache.*'): ['s3fs'],
6465
}
6566

@@ -2208,196 +2209,6 @@ def __delitem__(self, key):
22082209
self._invalidate_value(key)
22092210

22102211

2211-
class ABSStore(MutableMapping):
2212-
"""Storage class using Azure Blob Storage (ABS).
2213-
2214-
Parameters
2215-
----------
2216-
container : string
2217-
The name of the ABS container to use.
2218-
.. deprecated::
2219-
Use ``client`` instead.
2220-
prefix : string
2221-
Location of the "directory" to use as the root of the storage hierarchy
2222-
within the container.
2223-
account_name : string
2224-
The Azure blob storage account name.
2225-
.. deprecated:: 2.8.3
2226-
Use ``client`` instead.
2227-
account_key : string
2228-
The Azure blob storage account access key.
2229-
.. deprecated:: 2.8.3
2230-
Use ``client`` instead.
2231-
blob_service_kwargs : dictionary
2232-
Extra arguments to be passed into the azure blob client, for e.g. when
2233-
using the emulator, pass in blob_service_kwargs={'is_emulated': True}.
2234-
.. deprecated:: 2.8.3
2235-
Use ``client`` instead.
2236-
dimension_separator : {'.', '/'}, optional
2237-
Separator placed between the dimensions of a chunk.
2238-
client : azure.storage.blob.ContainerClient, optional
2239-
And ``azure.storage.blob.ContainerClient`` to connect with. See
2240-
`here <https://docs.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.containerclient?view=azure-python>`_ # noqa
2241-
for more.
2242-
2243-
.. versionadded:: 2.8.3
2244-
2245-
Notes
2246-
-----
2247-
In order to use this store, you must install the Microsoft Azure Storage SDK for Python,
2248-
``azure-storage-blob>=12.5.0``.
2249-
"""
2250-
2251-
def __init__(self, container=None, prefix='', account_name=None, account_key=None,
2252-
blob_service_kwargs=None, dimension_separator=None,
2253-
client=None,
2254-
):
2255-
self._dimension_separator = dimension_separator
2256-
self.prefix = normalize_storage_path(prefix)
2257-
if client is None:
2258-
# deprecated option, try to construct the client for them
2259-
msg = (
2260-
"Providing 'container', 'account_name', 'account_key', and 'blob_service_kwargs'"
2261-
"is deprecated. Provide and instance of 'azure.storage.blob.ContainerClient' "
2262-
"'client' instead."
2263-
)
2264-
warnings.warn(msg, FutureWarning, stacklevel=2)
2265-
from azure.storage.blob import ContainerClient
2266-
blob_service_kwargs = blob_service_kwargs or {}
2267-
client = ContainerClient(
2268-
"https://{}.blob.core.windows.net/".format(account_name), container,
2269-
credential=account_key, **blob_service_kwargs
2270-
)
2271-
2272-
self.client = client
2273-
self._container = container
2274-
self._account_name = account_name
2275-
self._account_key = account_key
2276-
2277-
def _warn_deprecated(self, property_):
2278-
msg = ("The {} property is deprecated and will be removed in a future "
2279-
"version. Get the property from 'ABSStore.client' instead.")
2280-
warnings.warn(msg.format(property_), FutureWarning, stacklevel=3)
2281-
2282-
@property
2283-
def container(self):
2284-
self._warn_deprecated("container")
2285-
return self._container
2286-
2287-
@property
2288-
def account_name(self):
2289-
self._warn_deprecated("account_name")
2290-
return self._account_name
2291-
2292-
@property
2293-
def account_key(self):
2294-
self._warn_deprecated("account_key")
2295-
return self._account_key
2296-
2297-
def _append_path_to_prefix(self, path):
2298-
if self.prefix == '':
2299-
return normalize_storage_path(path)
2300-
else:
2301-
return '/'.join([self.prefix, normalize_storage_path(path)])
2302-
2303-
@staticmethod
2304-
def _strip_prefix_from_path(path, prefix):
2305-
# normalized things will not have any leading or trailing slashes
2306-
path_norm = normalize_storage_path(path)
2307-
prefix_norm = normalize_storage_path(prefix)
2308-
if prefix:
2309-
return path_norm[(len(prefix_norm)+1):]
2310-
else:
2311-
return path_norm
2312-
2313-
def __getitem__(self, key):
2314-
from azure.core.exceptions import ResourceNotFoundError
2315-
blob_name = self._append_path_to_prefix(key)
2316-
try:
2317-
return self.client.download_blob(blob_name).readall()
2318-
except ResourceNotFoundError:
2319-
raise KeyError('Blob %s not found' % blob_name)
2320-
2321-
def __setitem__(self, key, value):
2322-
value = ensure_bytes(value)
2323-
blob_name = self._append_path_to_prefix(key)
2324-
self.client.upload_blob(blob_name, value, overwrite=True)
2325-
2326-
def __delitem__(self, key):
2327-
from azure.core.exceptions import ResourceNotFoundError
2328-
try:
2329-
self.client.delete_blob(self._append_path_to_prefix(key))
2330-
except ResourceNotFoundError:
2331-
raise KeyError('Blob %s not found' % key)
2332-
2333-
def __eq__(self, other):
2334-
return (
2335-
isinstance(other, ABSStore) and
2336-
self.client == other.client and
2337-
self.prefix == other.prefix
2338-
)
2339-
2340-
def keys(self):
2341-
return list(self.__iter__())
2342-
2343-
def __iter__(self):
2344-
if self.prefix:
2345-
list_blobs_prefix = self.prefix + '/'
2346-
else:
2347-
list_blobs_prefix = None
2348-
for blob in self.client.list_blobs(list_blobs_prefix):
2349-
yield self._strip_prefix_from_path(blob.name, self.prefix)
2350-
2351-
def __len__(self):
2352-
return len(self.keys())
2353-
2354-
def __contains__(self, key):
2355-
blob_name = self._append_path_to_prefix(key)
2356-
return self.client.get_blob_client(blob_name).exists()
2357-
2358-
def listdir(self, path=None):
2359-
dir_path = normalize_storage_path(self._append_path_to_prefix(path))
2360-
if dir_path:
2361-
dir_path += '/'
2362-
items = [
2363-
self._strip_prefix_from_path(blob.name, dir_path)
2364-
for blob in self.client.walk_blobs(name_starts_with=dir_path, delimiter='/')
2365-
]
2366-
return items
2367-
2368-
def rmdir(self, path=None):
2369-
dir_path = normalize_storage_path(self._append_path_to_prefix(path))
2370-
if dir_path:
2371-
dir_path += '/'
2372-
for blob in self.client.list_blobs(name_starts_with=dir_path):
2373-
self.client.delete_blob(blob)
2374-
2375-
def getsize(self, path=None):
2376-
store_path = normalize_storage_path(path)
2377-
fs_path = self._append_path_to_prefix(store_path)
2378-
if fs_path:
2379-
blob_client = self.client.get_blob_client(fs_path)
2380-
else:
2381-
blob_client = None
2382-
2383-
if blob_client and blob_client.exists():
2384-
return blob_client.get_blob_properties().size
2385-
else:
2386-
size = 0
2387-
if fs_path == '':
2388-
fs_path = None
2389-
elif not fs_path.endswith('/'):
2390-
fs_path += '/'
2391-
for blob in self.client.walk_blobs(name_starts_with=fs_path, delimiter='/'):
2392-
blob_client = self.client.get_blob_client(blob)
2393-
if blob_client.exists():
2394-
size += blob_client.get_blob_properties().size
2395-
return size
2396-
2397-
def clear(self):
2398-
self.rmdir()
2399-
2400-
24012212
class SQLiteStore(MutableMapping):
24022213
"""Storage class using SQLite.
24032214

0 commit comments

Comments
 (0)