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

Add get_available_cats() method to tutorial.py #458

Merged
merged 11 commits into from
Aug 18, 2022
46 changes: 32 additions & 14 deletions intake_esm/tutorial.py
Expand Up @@ -3,35 +3,53 @@
* users learning intake-esm
* building tutorials in the documentation.
"""
from __future__ import annotations

tutorial_catalogs = {
DEFAULT_CATALOGS = {
'aws_cesm2_le': 'https://raw.githubusercontent.com/intake/intake-esm/main/tutorial-catalogs/AWS-CESM2-LENS.json',
'aws_cmip6': 'https://raw.githubusercontent.com/intake/intake-esm/main/tutorial-catalogs/AWS-CMIP6.json',
'google_cmip6': 'https://raw.githubusercontent.com/intake/intake-esm/main/tutorial-catalogs/GOOGLE-CMIP6.json',
}


def get_url(name, tutorial_catalogs=tutorial_catalogs):
def get_url(name: str) -> str:
"""
Get a small-catalog URL from the online repository (requires internet).
Get a small-catalog URL from the online repository (requires internet).
If a local copy is found then always use that to avoid network traffic.
Available catalog:
Available catalogs:

* ``"aws_cesm2_le"``
* ``"aws_cmip6"``
* ``"google_cmip6"''

Parameters
----------
name : str
Name of the catalog.
e.g. 'aws_cmip6'
tutorial_catalogs : dict
Catalog of tutorial keys mapping to URLs.
Name of the catalog. e.g. 'aws_cmip6'

Returns
-------
str
URL of the catalog.
"""

if name in DEFAULT_CATALOGS:
return DEFAULT_CATALOGS[name]

raise KeyError(
f'KeyError: {name} is an unknown key. Only small-catalogs in our `tutorial-catalogs`'
f'directory are supported with this method. Valid values include: {get_available_cats()} .'
)


def get_available_cats() -> list[str]:
"""
Get a list of all supported small-catalog key names that map to URL from the online repository.

Returns
-------
list[str]
List of all supported small-catalog key names.
"""

try:
return tutorial_catalogs[name]
except KeyError:
print(
f'{name} is an unknown key. Only small-catalogs in our `tutorial-catalogs` directory are supported with this method. The supported catalog key names are: "aws_cesm2_le", "aws_cmip6", "google_cmip6".'
)
return list(DEFAULT_CATALOGS.keys())
2 changes: 2 additions & 0 deletions setup.cfg
Expand Up @@ -20,3 +20,5 @@ skip=
[tool:pytest]
console_output_style = count
addopts = -n auto --cov=./ --cov-report=xml --verbose
markers =
network: tests requiring a network connection
22 changes: 8 additions & 14 deletions tests/test_tutorial.py
Expand Up @@ -2,21 +2,10 @@
import pytest

import intake_esm
from intake_esm.tutorial import DEFAULT_CATALOGS

tutorial_cats = [
(
'aws_cesm2_le',
'https://raw.githubusercontent.com/intake/intake-esm/main/tutorial-catalogs/AWS-CESM2-LENS.json',
),
(
'aws_cmip6',
'https://raw.githubusercontent.com/intake/intake-esm/main/tutorial-catalogs/AWS-CMIP6.json',
),
(
'google_cmip6',
'https://raw.githubusercontent.com/intake/intake-esm/main/tutorial-catalogs/GOOGLE-CMIP6.json',
),
pytest.param('bad_key', 'bad_url', marks=pytest.mark.xfail),
tutorial_cats = list(zip(DEFAULT_CATALOGS.keys(), DEFAULT_CATALOGS.values())) + [
pytest.param('bad_key', 'bad_url', marks=pytest.mark.xfail)
]


Expand All @@ -34,3 +23,8 @@ def test_open_from_url(name, url):
cat = intake.open_esm_datastore(cat_url)
assert isinstance(cat.esmcat, intake_esm.cat.ESMCatalogModel)
assert cat == intake.open_esm_datastore(url)


def test_get_available_cats():
cats = intake_esm.tutorial.get_available_cats()
assert cats == list(DEFAULT_CATALOGS.keys())