Skip to content

Commit

Permalink
APIv2 internal cleanups (#4721)
Browse files Browse the repository at this point in the history
* Stop importing api.py in apiv2.py

* Cleanup backend_kwargs / kwargs

* Fix a bug in _get_mtime when filename_or_obj is a pathlib.Path
  • Loading branch information
alexamici authored Dec 22, 2020
1 parent 32f51e5 commit fc29824
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
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]

0 comments on commit fc29824

Please sign in to comment.