From d45f07cbecf357575e8a0eedb89afab203f8e684 Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Tue, 24 Dec 2019 13:16:27 -0600 Subject: [PATCH] summon: make schema more consistent * Mark optional keywords * Allow extra entries * Include objects into the schema --- dvc/api.py | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/dvc/api.py b/dvc/api.py index d5909dea5..f85141470 100644 --- a/dvc/api.py +++ b/dvc/api.py @@ -10,13 +10,32 @@ except ImportError: from contextlib import GeneratorContextManager as GCM import ruamel.yaml -from voluptuous import Schema, Invalid +from voluptuous import Schema, Optional, Invalid, ALLOW_EXTRA from dvc.utils.compat import urlparse, builtin_str, FileNotFoundError from dvc.repo import Repo from dvc.external_repo import external_repo +summon_schema = Schema( + { + "objects": [ + { + "name": str, + "summon": { + "type": "python", + "call": str, + Optional("args"): dict, + Optional("deps"): [str], + }, + } + ] + }, + required=True, + extra=ALLOW_EXTRA +) + + def get_url(path, repo=None, rev=None, remote=None): """Returns an url of a resource specified by path in repo""" with _make_repo(repo, rev=rev) as _repo: @@ -109,34 +128,15 @@ def _get_object_from_summoners_file(name, path): Given a summonable object's name, search for it on the given file and bring it to life. """ - schema = Schema( - [ - { - "name": str, - "description": str, - "paper": str, - "metrics": dict, - "summon": { - "type": "python", - "call": str, - "args": dict, # XXX: Optional - "deps": [str], # XXX: Optional - }, - } - ] - ) - with open(path, "r") as fobj: try: - objects = ruamel.yaml.safe_load(fobj.read())["objects"] - objects = schema(objects) + content = summon_schema(ruamel.yaml.safe_load(fobj.read())) + objects = content["objects"] return next(x for x in objects if x["name"] == name) except FileNotFoundError: pass # XXX: No such YAML file with path: '' except ruamel.yaml.ScannerError: pass # XXX: Failed to parse YAML correctly - except KeyError: - pass # XXX: YAML file doesn't include the "objects" keyword except Invalid: pass # XXX: YAML file dosen't match with the schema except StopIteration: