From 97ff07f8f63b519839a99cd01fc3815994ae905a Mon Sep 17 00:00:00 2001 From: Jared Lewis Date: Sun, 17 Nov 2024 17:19:37 +1100 Subject: [PATCH] feat: Support being able to use in-memory ESMCatalogModel instances --- intake_esm/core.py | 9 ++++++--- tests/test_core.py | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/intake_esm/core.py b/intake_esm/core.py index 0c9c8c16..6061679c 100644 --- a/intake_esm/core.py +++ b/intake_esm/core.py @@ -31,7 +31,8 @@ class esm_datastore(Catalog): Parameters ---------- - obj : str, dict + obj : str, dict, ESMCatalogModel + The ESM Catalog to use, or a path to a JSON file containing the catalog. If string, this must be a path or URL to an ESM catalog JSON file. If dict, this must be a dict representation of an ESM catalog. This dict must have two keys: 'esmcat' and 'df'. The 'esmcat' key must be a @@ -76,7 +77,7 @@ class esm_datastore(Catalog): def __init__( self, - obj: pydantic.FilePath | pydantic.AnyUrl | dict[str, typing.Any], + obj: pydantic.FilePath | pydantic.AnyUrl | dict[str, typing.Any] | ESMCatalogModel, *, progressbar: bool = True, sep: str = '.', @@ -101,7 +102,9 @@ def __init__( self.read_csv_kwargs = read_csv_kwargs self.progressbar = progressbar self.sep = sep - if isinstance(obj, dict): + if isinstance(obj, ESMCatalogModel): + self.esmcat = obj + elif isinstance(obj, dict): self.esmcat = ESMCatalogModel.from_dict(obj) else: self.esmcat = ESMCatalogModel.load( diff --git a/tests/test_core.py b/tests/test_core.py index 38055ed2..15fb4b47 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -63,6 +63,7 @@ def func_multivar(ds): (multi_variable_cat, '*', {'converters': {'variable': ast.literal_eval}}, None), (multi_variable_cat, '*', None, ['variable']), ({'esmcat': sample_esmcat_data, 'df': sample_df}, '.', None, None), + (intake_esm.cat.ESMCatalogModel.load(cdf_cat_sample_cmip6), '.', None, None), ], ) def test_catalog_init(capsys, obj, sep, read_csv_kwargs, columns_with_iterables):