mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-11-06 23:09:55 +01:00
summon: make schema more consistent
* Mark optional keywords * Allow extra entries * Include objects into the schema
This commit is contained in:
+22
-22
@@ -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: '<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:
|
||||
|
||||
Reference in New Issue
Block a user