diff --git a/packages/zoo/src/pyearthtools/zoo/__init__.py b/packages/zoo/src/pyearthtools/zoo/__init__.py index fde55ed6..f7e659a5 100644 --- a/packages/zoo/src/pyearthtools/zoo/__init__.py +++ b/packages/zoo/src/pyearthtools/zoo/__init__.py @@ -87,7 +87,7 @@ class MODEL(): Models = utils.AvailableModels() -from pyearthtools.zoo.commands import commands # pylint: disable=C0413 # noqa: E402 +from pyearthtools.zoo import commands # pylint: disable=C0413 # noqa: E402 from pyearthtools.zoo.predict import ( # pylint: disable=C0413 # noqa: E402 data, interactive, diff --git a/packages/zoo/src/pyearthtools/zoo/commands/__init__.py b/packages/zoo/src/pyearthtools/zoo/commands/__init__.py index 1724490e..11aa5285 100644 --- a/packages/zoo/src/pyearthtools/zoo/commands/__init__.py +++ b/packages/zoo/src/pyearthtools/zoo/commands/__init__.py @@ -16,7 +16,7 @@ `pyearthtools.zoo` commands. """ -from pyearthtools.zoo.commands import utils, commands -from pyearthtools.zoo.commands.commands import entry_point +# CLI entry point for 'pet' as defined in pyproject.toml for zoo +from ._commands import entry_point -__all__ = ["utils", "commands", "entry_point"] +__all__ = ["entry_point"] diff --git a/packages/zoo/src/pyearthtools/zoo/commands/utils.py b/packages/zoo/src/pyearthtools/zoo/commands/_cmd_utils.py similarity index 100% rename from packages/zoo/src/pyearthtools/zoo/commands/utils.py rename to packages/zoo/src/pyearthtools/zoo/commands/_cmd_utils.py diff --git a/packages/zoo/src/pyearthtools/zoo/commands/commands.py b/packages/zoo/src/pyearthtools/zoo/commands/_commands.py similarity index 95% rename from packages/zoo/src/pyearthtools/zoo/commands/commands.py rename to packages/zoo/src/pyearthtools/zoo/commands/_commands.py index edfe7431..47fc171f 100644 --- a/packages/zoo/src/pyearthtools/zoo/commands/commands.py +++ b/packages/zoo/src/pyearthtools/zoo/commands/_commands.py @@ -34,7 +34,8 @@ import click import pyearthtools.zoo -from pyearthtools.zoo.commands import utils as command_utils + +from . import _cmd_utils as command_utils from pyearthtools.zoo.utils import AvailableModels available_models = AvailableModels() @@ -62,6 +63,10 @@ def entry_point(debug, info): @entry_point.command("models", help="Print available models.") def models(): + list_models() + + +def list_models(): from pyearthtools.zoo.register import dynamic_import dynamic_import() @@ -73,6 +78,7 @@ def models(): print(_models) print("(Specify category with a '/' seperation.)") + sys.exit(0) @@ -116,7 +122,11 @@ def models(): default=None, help="Override for config path", ) -def run_predict( +def run_predict(): + cmd_run_predict() + + +def cmd_run_predict( ctx, model: str, time: str, @@ -127,9 +137,6 @@ def run_predict( ): ctx_kwargs = command_utils.get_keyword_from_ctx(ctx) - if "data" in ctx_kwargs: - raise ValueError("data has been deprecated as an argument for `predict`, use `data`.") - predictions = pyearthtools.zoo.predict( model, time, @@ -200,7 +207,11 @@ def run_predict( default=None, help="Override for config path", ) -def interactive( +def interactive(): + cmd_interactive() + + +def cmd_interactive( ctx, model: str, time: str, @@ -277,6 +288,10 @@ def interactive( help="Override for config path", ) def data(ctx, model, time, pipeline, data_cache, config_path: str | Path): + cmd_data(ctx, model, time, pipeline, data_cache, config_path) + + +def cmd_data(ctx, model, time, pipeline, data_cache, config_path: str | Path): ctx_kwargs = command_utils.get_keyword_from_ctx(ctx) _ = pyearthtools.zoo.data(model, time, pipeline, data_cache, config_path=config_path, **ctx_kwargs) diff --git a/packages/zoo/src/pyearthtools/zoo/predict.py b/packages/zoo/src/pyearthtools/zoo/predict.py index 50326ba7..5d14b5e8 100644 --- a/packages/zoo/src/pyearthtools/zoo/predict.py +++ b/packages/zoo/src/pyearthtools/zoo/predict.py @@ -27,7 +27,7 @@ import warnings import pyearthtools.zoo -from pyearthtools.zoo.commands import utils as command_utils +from pyearthtools.zoo.commands import _cmd_utils as command_utils from pyearthtools.zoo import utils, exceptions from pyearthtools.zoo.register import dynamic_import diff --git a/packages/zoo/tests/models/utils/test_args_to_dict.py b/packages/zoo/tests/commands/test_args_to_dict.py similarity index 83% rename from packages/zoo/tests/models/utils/test_args_to_dict.py rename to packages/zoo/tests/commands/test_args_to_dict.py index 257415c7..ac8489b5 100644 --- a/packages/zoo/tests/models/utils/test_args_to_dict.py +++ b/packages/zoo/tests/commands/test_args_to_dict.py @@ -14,6 +14,8 @@ import pytest +from pyearthtools.zoo import commands + tests = [ (("--test", "value"), {"test": "value"}), # Single argument ( @@ -37,28 +39,24 @@ @pytest.mark.parametrize("args, result", tests) def test_parse_args_to_dict(args, result): - from pyearthtools.zoo.commands import utils - - assert utils.parse_args_to_dict(*args) == result + assert commands._cmd_utils.parse_args_to_dict(*args) == result @pytest.mark.parametrize("args, result", tests) def test_get_keyword_from_ctx(args, result): - from pyearthtools.zoo.commands import utils import click class FakeContext(click.Context): def __init__(self, args): self.args = args - assert utils.get_keyword_from_ctx(FakeContext(args)) == result + assert commands._cmd_utils.get_keyword_from_ctx(FakeContext(args)) == result @pytest.mark.parametrize("args, result", tests) def test_parse_str_to_dict(args, result): - from pyearthtools.zoo.commands import utils - assert utils.parse_str_to_dict(" ".join(args)) == result + assert commands._cmd_utils.parse_str_to_dict(" ".join(args)) == result @pytest.mark.parametrize( @@ -72,7 +70,6 @@ def test_parse_str_to_dict(args, result): ], ) def test_parse_args_to_dict_fail(args): - from pyearthtools.zoo.commands import utils with pytest.raises(KeyError): - utils.parse_args_to_dict(*args) + commands._cmd_utils.parse_args_to_dict(*args) diff --git a/packages/zoo/tests/commands/test_commands.py b/packages/zoo/tests/commands/test_commands.py new file mode 100644 index 00000000..6c87fbc1 --- /dev/null +++ b/packages/zoo/tests/commands/test_commands.py @@ -0,0 +1,60 @@ +from collections import namedtuple +import pytest +from pyearthtools.zoo.commands import _commands as cmd +from unittest.mock import patch + +from pyearthtools.zoo.exceptions import ModelException + +# def test_entry_point(): + +# with pytest.raises(SystemExit): +# ep = cmd.entry_point(None, None) + + +def test_models(): + + with pytest.raises(SystemExit): + _m = cmd.list_models() + + +def test_run_predict(): + + ctx = namedtuple("Any", ["args", "kwargs"])([], []) + model = "nonexistent" + time = "2020T010100" + output = "tbc" + pipeline_name = "tbc" + data_cache = "tbc" + config_path = "tbc" + + with pytest.raises(ModelException): + cmd.cmd_run_predict(ctx, model, time, output, pipeline_name, data_cache, config_path) + + +def test_interactive(): + + ctx = namedtuple("Any", ["args", "kwargs"])([], []) + model = "nonexistent" + time = "2020T010100" + output = "tbc" + pipeline_name = "tbc" + data_cache = "tbc" + config_path = "tbc" + + with pytest.raises(AttributeError): + with patch("pyearthtools.zoo.available_models", return_value="fake_model"): + cmd.cmd_interactive(ctx, model, time, pipeline_name, output, data_cache, config_path) + + +def test_data(): + + ctx = namedtuple("Any", ["args", "kwargs"])([], []) + model = "nonexistent" + time = "2020T010100" + _output = "tbc" + pipeline_name = "tbc" + data_cache = "tbc" + config_path = "tbc" + + with pytest.raises(ModelException): + cmd.cmd_data(ctx, model, time, pipeline_name, data_cache, config_path) diff --git a/packages/zoo/tests/test_zoo.py b/packages/zoo/tests/test_zoo.py new file mode 100644 index 00000000..8dbb67df --- /dev/null +++ b/packages/zoo/tests/test_zoo.py @@ -0,0 +1,5 @@ +from pyearthtools import zoo + + +def test_available_models(): + _models = zoo.available_models() # smoke test for now