diff --git a/dvc/api.py b/dvc/api.py index ac64b3dca..ffb26cd90 100644 --- a/dvc/api.py +++ b/dvc/api.py @@ -75,34 +75,25 @@ def _make_repo(repo_url, rev=None): def summon(name, repo=None, rev=None): - # 1. Read artifacts.yaml - # 2. Pull dependencies + # 1. Read grimoire.yaml + # 2. Pull dependencies (TODO) # 3. Get the call and parameters # 4. Invoke the call with the given parameters # 5. Return the result with _make_repo(repo, rev=rev) as _repo: - artifacts_path = os.path.join(_repo.root_dir, "artifacts.yaml") + grimoire_path = os.path.join(_repo.root_dir, "grimoire.yaml") - with _repo.open(artifacts_path, mode="r") as fd: - artifacts = ruamel.yaml.load(fd.read()).get("artifacts") - - artifact = next(x for x in artifacts if x.get("name") == name) - - spec = importlib.util.spec_from_file_location( - artifact.get("call"), - os.path.join(_repo.root_dir, artifact.get("file")), - ) + with open(grimoire_path, "r") as fobj: + spells = ruamel.yaml.load(fobj.read()).get("spells") + spell = next(spell for spell in spells if spell.get("name") == name) + spell_path = os.path.join(_repo.root_dir, spell.get("file")) + spec = importlib.util.spec_from_file_location(name, spell_path) module = importlib.util.module_from_spec(spec) - # ugly af, don't use exec / global, pls - call = 'global result; result = {method}({params})'.format( - method=artifact.get("call"), - params=", ".join(k + "=" + v for k, v in artifact.get("params").items()) - ) - spec.loader.exec_module(module) - exec(call) - global result - return result + + method = getattr(module, spell.get("method")) + + return method(**spell.get("params")) diff --git a/tests/func/test_api.py b/tests/func/test_api.py index 7be749e02..b4cdcb264 100644 --- a/tests/func/test_api.py +++ b/tests/func/test_api.py @@ -130,22 +130,26 @@ def test_open_not_cached(dvc): def test_summon(tmp_dir, erepo_dir, dvc): - artifacts_yaml = ruamel.yaml.dump({ - "artifacts": [{ - "name": "sum", - "description": "The sum of 1 + 2", - "call": "module.sum", - "file": "module.py", - "params": {"x": "1", "y": "2"}, - "deps": ["foo"], - }] - }) + grimoire_yaml = ruamel.yaml.dump( + { + "spells": [ + { + "name": "three", + "description": "The sum of 1 + 2", + "file": "calculator.py", + "method": "sum", + "params": {"x": 1, "y": 2}, + "deps": ["foo"], + } + ] + } + ) - erepo_dir.gen("module.py", "def sum(x, y): return x + y") - erepo_dir.gen("artifacts.yaml", artifacts_yaml) - erepo_dir.scm.add(["module.py", "artifacts.yaml"]) - erepo_dir.scm.commit("Add module.py and artifacts.yaml") + erepo_dir.gen("calculator.py", "def sum(x, y): return x + y") + erepo_dir.gen("grimoire.yaml", grimoire_yaml) + erepo_dir.scm.add(["calculator.py", "grimoire.yaml"]) + erepo_dir.scm.commit("Add calculator.py and grimoire.yaml") repo_url = "file://{}".format(erepo_dir) - assert api.summon("sum", repo=repo_url) == 3 + assert api.summon("three", repo=repo_url) == 3