Skip to content

Add full copy of zarr-python v2.18.7 under src/zarr/v2 #3075

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
70 changes: 70 additions & 0 deletions src/zarr/v2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# flake8: noqa
from zarr.codecs import *
from zarr.convenience import (

Check warning on line 3 in src/zarr/v2/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/__init__.py#L2-L3

Added lines #L2 - L3 were not covered by tests
consolidate_metadata,
copy,
copy_all,
copy_store,
load,
open,
open_consolidated,
save,
save_array,
save_group,
tree,
)
from zarr.core import Array
from zarr.creation import (

Check warning on line 17 in src/zarr/v2/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/__init__.py#L16-L17

Added lines #L16 - L17 were not covered by tests
array,
create,
empty,
empty_like,
full,
full_like,
ones,
ones_like,
open_array,
open_like,
zeros,
zeros_like,
)
from zarr.errors import CopyError, MetadataError
from zarr.hierarchy import Group, group, open_group
from zarr.n5 import N5Store, N5FSStore
from zarr._storage.store import v3_api_available
from zarr.storage import (

Check warning on line 35 in src/zarr/v2/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/__init__.py#L31-L35

Added lines #L31 - L35 were not covered by tests
ABSStore,
DBMStore,
DictStore,
DirectoryStore,
KVStore,
LMDBStore,
LRUStoreCache,
MemoryStore,
MongoDBStore,
NestedDirectoryStore,
RedisStore,
SQLiteStore,
TempStore,
ZipStore,
)
from zarr.sync import ProcessSynchronizer, ThreadSynchronizer
from zarr.version import version as __version__

Check warning on line 52 in src/zarr/v2/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/__init__.py#L51-L52

Added lines #L51 - L52 were not covered by tests

# in case setuptools scm screw up and find version to be 0.0.0
assert not __version__.startswith("0.0.0")

Check warning on line 55 in src/zarr/v2/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/__init__.py#L55

Added line #L55 was not covered by tests

if v3_api_available:
from zarr._storage.v3 import (

Check warning on line 58 in src/zarr/v2/__init__.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/__init__.py#L57-L58

Added lines #L57 - L58 were not covered by tests
ABSStoreV3,
DBMStoreV3,
KVStoreV3,
DirectoryStoreV3,
LMDBStoreV3,
LRUStoreCacheV3,
MemoryStoreV3,
MongoDBStoreV3,
RedisStoreV3,
SQLiteStoreV3,
ZipStoreV3,
)
Empty file.
293 changes: 293 additions & 0 deletions src/zarr/v2/_storage/absstore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
"""This module contains storage classes related to Azure Blob Storage (ABS)"""

from typing import Optional
import warnings

Check warning on line 4 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L3-L4

Added lines #L3 - L4 were not covered by tests

from numcodecs.compat import ensure_bytes
from zarr.util import normalize_storage_path
from zarr._storage.store import (

Check warning on line 8 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L6-L8

Added lines #L6 - L8 were not covered by tests
_get_metadata_suffix,
data_root,
meta_root,
Store,
StoreV3,
V3_DEPRECATION_MESSAGE,
)
from zarr.types import DIMENSION_SEPARATOR

Check warning on line 16 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L16

Added line #L16 was not covered by tests

__doctest_requires__ = {

Check warning on line 18 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L18

Added line #L18 was not covered by tests
("ABSStore", "ABSStore.*"): ["azure.storage.blob"],
}


class ABSStore(Store):

Check warning on line 23 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L23

Added line #L23 was not covered by tests
"""Storage class using Azure Blob Storage (ABS).

Parameters
----------
container : string
The name of the ABS container to use.

.. deprecated::
Use ``client`` instead.

prefix : string
Location of the "directory" to use as the root of the storage hierarchy
within the container.

account_name : string
The Azure blob storage account name.

.. deprecated:: 2.8.3
Use ``client`` instead.

account_key : string
The Azure blob storage account access key.

.. deprecated:: 2.8.3
Use ``client`` instead.

blob_service_kwargs : dictionary
Extra arguments to be passed into the azure blob client, for e.g. when
using the emulator, pass in blob_service_kwargs={'is_emulated': True}.

.. deprecated:: 2.8.3
Use ``client`` instead.

dimension_separator : {'.', '/'}, optional
Separator placed between the dimensions of a chunk.

client : azure.storage.blob.ContainerClient, optional
And ``azure.storage.blob.ContainerClient`` to connect with. See
`here <https://docs.microsoft.com/en-us/python/api/azure-storage-blob/azure.storage.blob.containerclient?view=azure-python>`_ # noqa
for more.

.. versionadded:: 2.8.3

Notes
-----
In order to use this store, you must install the Microsoft Azure Storage SDK for Python,
``azure-storage-blob>=12.5.0``.
""" # noqa: E501

def __init__(

Check warning on line 73 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L73

Added line #L73 was not covered by tests
self,
container=None,
prefix="",
account_name=None,
account_key=None,
blob_service_kwargs=None,
dimension_separator: Optional[DIMENSION_SEPARATOR] = None,
client=None,
):
warnings.warn(

Check warning on line 83 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L83

Added line #L83 was not covered by tests
V3_DEPRECATION_MESSAGE.format(store=self.__class__.__name__),
FutureWarning,
stacklevel=3,
)

self._dimension_separator = dimension_separator
self.prefix = normalize_storage_path(prefix)
if client is None:

Check warning on line 91 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L89-L91

Added lines #L89 - L91 were not covered by tests
# deprecated option, try to construct the client for them
msg = (

Check warning on line 93 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L93

Added line #L93 was not covered by tests
"Providing 'container', 'account_name', 'account_key', and 'blob_service_kwargs'"
"is deprecated. Provide and instance of 'azure.storage.blob.ContainerClient' "
"'client' instead."
)
warnings.warn(msg, FutureWarning, stacklevel=2)
from azure.storage.blob import ContainerClient

Check warning on line 99 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L98-L99

Added lines #L98 - L99 were not covered by tests

blob_service_kwargs = blob_service_kwargs or {}
client = ContainerClient(

Check warning on line 102 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L101-L102

Added lines #L101 - L102 were not covered by tests
f"https://{account_name}.blob.core.windows.net/",
container,
credential=account_key,
**blob_service_kwargs,
)

self.client = client
self._container = container
self._account_name = account_name
self._account_key = account_key

Check warning on line 112 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L109-L112

Added lines #L109 - L112 were not covered by tests

@staticmethod
def _warn_deprecated(property_):
msg = (

Check warning on line 116 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L114-L116

Added lines #L114 - L116 were not covered by tests
"The {} property is deprecated and will be removed in a future "
"version. Get the property from 'ABSStore.client' instead."
)
warnings.warn(msg.format(property_), FutureWarning, stacklevel=3)

Check warning on line 120 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L120

Added line #L120 was not covered by tests

@property
def container(self):
self._warn_deprecated("container")
return self._container

Check warning on line 125 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L122-L125

Added lines #L122 - L125 were not covered by tests

@property
def account_name(self):
self._warn_deprecated("account_name")
return self._account_name

Check warning on line 130 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L127-L130

Added lines #L127 - L130 were not covered by tests

@property
def account_key(self):
self._warn_deprecated("account_key")
return self._account_key

Check warning on line 135 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L132-L135

Added lines #L132 - L135 were not covered by tests

def _append_path_to_prefix(self, path):
if self.prefix == "":
return normalize_storage_path(path)

Check warning on line 139 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L137-L139

Added lines #L137 - L139 were not covered by tests
else:
return "/".join([self.prefix, normalize_storage_path(path)])

Check warning on line 141 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L141

Added line #L141 was not covered by tests

@staticmethod
def _strip_prefix_from_path(path, prefix):

Check warning on line 144 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L143-L144

Added lines #L143 - L144 were not covered by tests
# normalized things will not have any leading or trailing slashes
path_norm = normalize_storage_path(path)
prefix_norm = normalize_storage_path(prefix)
if prefix:
return path_norm[(len(prefix_norm) + 1) :]

Check warning on line 149 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L146-L149

Added lines #L146 - L149 were not covered by tests
else:
return path_norm

Check warning on line 151 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L151

Added line #L151 was not covered by tests

def __getitem__(self, key):
from azure.core.exceptions import ResourceNotFoundError

Check warning on line 154 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L153-L154

Added lines #L153 - L154 were not covered by tests

blob_name = self._append_path_to_prefix(key)
try:
return self.client.download_blob(blob_name).readall()
except ResourceNotFoundError as e:
raise KeyError(f"Blob {blob_name} not found") from e

Check warning on line 160 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L156-L160

Added lines #L156 - L160 were not covered by tests

def __setitem__(self, key, value):
value = ensure_bytes(value)
blob_name = self._append_path_to_prefix(key)
self.client.upload_blob(blob_name, value, overwrite=True)

Check warning on line 165 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L162-L165

Added lines #L162 - L165 were not covered by tests

def __delitem__(self, key):
from azure.core.exceptions import ResourceNotFoundError

Check warning on line 168 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L167-L168

Added lines #L167 - L168 were not covered by tests

try:
self.client.delete_blob(self._append_path_to_prefix(key))
except ResourceNotFoundError as e:
raise KeyError(f"Blob {key} not found") from e

Check warning on line 173 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L170-L173

Added lines #L170 - L173 were not covered by tests

def __eq__(self, other):
return (

Check warning on line 176 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L175-L176

Added lines #L175 - L176 were not covered by tests
isinstance(other, ABSStore)
and self.client == other.client
and self.prefix == other.prefix
)

def keys(self):
return list(self.__iter__())

Check warning on line 183 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L182-L183

Added lines #L182 - L183 were not covered by tests

def __iter__(self):
if self.prefix:
list_blobs_prefix = self.prefix + "/"

Check warning on line 187 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L185-L187

Added lines #L185 - L187 were not covered by tests
else:
list_blobs_prefix = None
for blob in self.client.list_blobs(list_blobs_prefix):
yield self._strip_prefix_from_path(blob.name, self.prefix)

Check warning on line 191 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L189-L191

Added lines #L189 - L191 were not covered by tests

def __len__(self):
return len(self.keys())

Check warning on line 194 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L193-L194

Added lines #L193 - L194 were not covered by tests

def __contains__(self, key):
blob_name = self._append_path_to_prefix(key)
return self.client.get_blob_client(blob_name).exists()

Check warning on line 198 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L196-L198

Added lines #L196 - L198 were not covered by tests

def listdir(self, path=None):
dir_path = normalize_storage_path(self._append_path_to_prefix(path))
if dir_path:
dir_path += "/"
items = [

Check warning on line 204 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L200-L204

Added lines #L200 - L204 were not covered by tests
self._strip_prefix_from_path(blob.name, dir_path)
for blob in self.client.walk_blobs(name_starts_with=dir_path, delimiter="/")
]
return items

Check warning on line 208 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L208

Added line #L208 was not covered by tests

def rmdir(self, path=None):
dir_path = normalize_storage_path(self._append_path_to_prefix(path))
if dir_path:
dir_path += "/"
for blob in self.client.list_blobs(name_starts_with=dir_path):
self.client.delete_blob(blob)

Check warning on line 215 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L210-L215

Added lines #L210 - L215 were not covered by tests

def getsize(self, path=None):
store_path = normalize_storage_path(path)
fs_path = self._append_path_to_prefix(store_path)
if fs_path:
blob_client = self.client.get_blob_client(fs_path)

Check warning on line 221 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L217-L221

Added lines #L217 - L221 were not covered by tests
else:
blob_client = None

Check warning on line 223 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L223

Added line #L223 was not covered by tests

if blob_client and blob_client.exists():
return blob_client.get_blob_properties().size

Check warning on line 226 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L225-L226

Added lines #L225 - L226 were not covered by tests
else:
size = 0
if fs_path == "":
fs_path = None
elif not fs_path.endswith("/"):
fs_path += "/"
for blob in self.client.walk_blobs(name_starts_with=fs_path, delimiter="/"):
blob_client = self.client.get_blob_client(blob.name)
if blob_client.exists():
size += blob_client.get_blob_properties().size
return size

Check warning on line 237 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L228-L237

Added lines #L228 - L237 were not covered by tests

def clear(self):
self.rmdir()

Check warning on line 240 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L239-L240

Added lines #L239 - L240 were not covered by tests


class ABSStoreV3(ABSStore, StoreV3):
def list(self):
return list(self.keys())

Check warning on line 245 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L243-L245

Added lines #L243 - L245 were not covered by tests

def __eq__(self, other):
return (

Check warning on line 248 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L247-L248

Added lines #L247 - L248 were not covered by tests
isinstance(other, ABSStoreV3)
and self.client == other.client
and self.prefix == other.prefix
)

def __setitem__(self, key, value):
self._validate_key(key)
super().__setitem__(key, value)

Check warning on line 256 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L254-L256

Added lines #L254 - L256 were not covered by tests

def rmdir(self, path=None):
if not path:

Check warning on line 259 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L258-L259

Added lines #L258 - L259 were not covered by tests
# Currently allowing clear to delete everything as in v2

# If we disallow an empty path then we will need to modify
# TestABSStoreV3 to have the create_store method use a prefix.
ABSStore.rmdir(self, "")
return

Check warning on line 265 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L264-L265

Added lines #L264 - L265 were not covered by tests

meta_dir = meta_root + path
meta_dir = meta_dir.rstrip("/")
ABSStore.rmdir(self, meta_dir)

Check warning on line 269 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L267-L269

Added lines #L267 - L269 were not covered by tests

# remove data folder
data_dir = data_root + path
data_dir = data_dir.rstrip("/")
ABSStore.rmdir(self, data_dir)

Check warning on line 274 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L272-L274

Added lines #L272 - L274 were not covered by tests

# remove metadata files
sfx = _get_metadata_suffix(self)
array_meta_file = meta_dir + ".array" + sfx
if array_meta_file in self:
del self[array_meta_file]
group_meta_file = meta_dir + ".group" + sfx
if group_meta_file in self:
del self[group_meta_file]

Check warning on line 283 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L277-L283

Added lines #L277 - L283 were not covered by tests

# TODO: adapt the v2 getsize method to work for v3
# For now, calling the generic keys-based _getsize
def getsize(self, path=None):
from zarr.storage import _getsize # avoid circular import

Check warning on line 288 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L287-L288

Added lines #L287 - L288 were not covered by tests

return _getsize(self, path)

Check warning on line 290 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L290

Added line #L290 was not covered by tests


ABSStoreV3.__doc__ = ABSStore.__doc__

Check warning on line 293 in src/zarr/v2/_storage/absstore.py

View check run for this annotation

Codecov / codecov/patch

src/zarr/v2/_storage/absstore.py#L293

Added line #L293 was not covered by tests
Loading
Loading