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

Check dependencies before dispatch. #438

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
28 changes: 25 additions & 3 deletions tiled/client/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ..utils import UNCHANGED, OneShotCachedMap, Sentinel, node_repr
from .base import BaseClient
from .cache import Revalidate, verify_cache
from .utils import ClientError, client_for_item, export_util
from .utils import ClientError, client_for_item, export_util, modules_available


class Node(BaseClient, collections.abc.Mapping, IndexersMixin):
Expand Down Expand Up @@ -884,11 +884,14 @@ def __index__(self):

class _LazyLoad:
# This exists because lambdas and closures cannot be pickled.
def __init__(self, import_module_args, attr_name):
def __init__(self, import_module_args, attr_name, check=None):
self.import_module_args = import_module_args
self.attr_name = attr_name
self.check = check

def __call__(self):
if self.check is not None:
self.check()
return getattr(
importlib.import_module(*self.import_module_args), self.attr_name
)
Expand All @@ -903,14 +906,33 @@ def __call__(self):
return self.obj


class MissingExtraDependency(ImportError):
pass


def _check_xarray():
if not modules_avaialble("xarray"):
raise MissingExtraDepenency(
"""The structure can be read into an xarray.Dataset, but xarray is not installed.

You can install xarray with pip:

pip install xarray

or conda:

conda install -c conda-forge xarray
""")


DEFAULT_STRUCTURE_CLIENT_DISPATCH = {
"numpy": OneShotCachedMap(
{
"node": _Wrap(Node),
"array": _LazyLoad(("..array", Node.__module__), "ArrayClient"),
"dataframe": _LazyLoad(("..dataframe", Node.__module__), "DataFrameClient"),
"sparse": _LazyLoad(("..sparse", Node.__module__), "SparseClient"),
"xarray_dataset": _LazyLoad(("..xarray", Node.__module__), "DatasetClient"),
"xarray_dataset": _LazyLoad(("..xarray", Node.__module__), "DatasetClient", _check_xarray),
}
),
"dask": OneShotCachedMap(
Expand Down