Skip to content
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

APIv2 internal cleanups #4721

Merged
merged 3 commits into from
Dec 22, 2020
Merged
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
2 changes: 1 addition & 1 deletion xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def open_dataset(
open_mfdataset
"""
if os.environ.get("XARRAY_BACKEND_API", "v1") == "v2":
kwargs = locals()
kwargs = {k: v for k, v in locals().items() if v is not None}
from . import apiv2

return apiv2.open_dataset(**kwargs)
Expand Down
38 changes: 25 additions & 13 deletions xarray/backends/apiv2.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
import os
import warnings

from ..core import indexing
from ..core.dataset import _get_chunk, _maybe_chunk
from ..core.utils import is_remote_uri
from . import plugins
from .api import _get_backend_cls, _protect_dataset_variables_inplace


def _protect_dataset_variables_inplace(dataset, cache):
for name, variable in dataset.variables.items():
if name not in variable.dims:
# no need to protect IndexVariable objects
data = indexing.CopyOnWriteArray(variable._data)
if cache:
data = indexing.MemoryCachedArray(data)
variable.data = data


def _get_mtime(filename_or_obj):
# if passed an actual file path, augment the token with
# the file modification time
if isinstance(filename_or_obj, str) and not is_remote_uri(filename_or_obj):
mtime = None

try:
path = os.fspath(filename_or_obj)
except TypeError:
path = None

if path and not is_remote_uri(path):
mtime = os.path.getmtime(filename_or_obj)
else:
mtime = None

return mtime


Expand Down Expand Up @@ -237,14 +253,13 @@ def open_dataset(
if cache is None:
cache = chunks is None

if backend_kwargs is None:
backend_kwargs = {}
if backend_kwargs is not None:
kwargs.update(backend_kwargs)

if engine is None:
engine = plugins.guess_engine(filename_or_obj)

engines = plugins.list_engines()
backend = _get_backend_cls(engine, engines=engines)
backend = plugins.get_backend(engine)

decoders = _resolve_decoders_kwargs(
decode_cf,
Expand All @@ -257,14 +272,12 @@ def open_dataset(
decode_coords=decode_coords,
)

backend_kwargs = backend_kwargs.copy()
overwrite_encoded_chunks = backend_kwargs.pop("overwrite_encoded_chunks", None)
overwrite_encoded_chunks = kwargs.pop("overwrite_encoded_chunks", None)
backend_ds = backend.open_dataset(
filename_or_obj,
drop_variables=drop_variables,
**decoders,
**backend_kwargs,
**{k: v for k, v in kwargs.items() if v is not None},
**kwargs,
)
ds = _dataset_from_backend_dataset(
backend_ds,
Expand All @@ -275,7 +288,6 @@ def open_dataset(
overwrite_encoded_chunks,
drop_variables=drop_variables,
**decoders,
**backend_kwargs,
**kwargs,
)

Expand Down
10 changes: 10 additions & 0 deletions xarray/backends/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,13 @@ def guess_engine(store_spec):
logging.exception(f"{engine!r} fails while guessing")

raise ValueError("cannot guess the engine, try passing one explicitly")


def get_backend(engine):
"""Select open_dataset method based on current engine"""
engines = list_engines()
if engine not in engines:
raise ValueError(
f"unrecognized engine {engine} must be one of: {list(engines)}"
)
return engines[engine]