From c799f2a4c776cc86f04075b4018abbd0cac65f6c Mon Sep 17 00:00:00 2001 From: Lukas Heinrich Date: Mon, 19 Jul 2021 15:41:16 +0200 Subject: [PATCH] commit --- setup.py | 1 + src/recastatlas/config.py | 48 +++++++++++++++++------- src/recastatlas/subcommands/catalogue.py | 7 ++-- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/setup.py b/setup.py index 35a0bc3..d1c8a1d 100644 --- a/setup.py +++ b/setup.py @@ -20,6 +20,7 @@ include_package_data = True, install_requires = [ 'click', + 'jsonschema', 'pyyaml', 'yadage-schemas==0.10.6', ], diff --git a/src/recastatlas/config.py b/src/recastatlas/config.py index d3fe910..cc6182d 100644 --- a/src/recastatlas/config.py +++ b/src/recastatlas/config.py @@ -3,6 +3,7 @@ import pkg_resources import glob import logging +import jsonschema log = logging.getLogger(__name__) @@ -76,23 +77,42 @@ def catalogue(self): files = list(set(files)) log.debug(files) for f in files: - d = yaml.safe_load(open(f)) - if not validate_catalogue_entry(d): - continue log.debug(f'loading catalogue file {f}') - name = d.pop("name") - if not "toplevel" in d["spec"]: - d["spec"]["toplevel"] = os.path.realpath( - os.path.join(os.path.dirname(f), "specs") - ) - cfg[name] = d + entry = yaml.safe_load(open(f)) + process_entry(cfg,f,entry) return cfg - config = Config() -def validate_catalogue_entry(entry): - for x in ['name','metadata','spec']: - if not x in entry: - return False +def process_entry(cfg,fname,entry,tags = None): + if (entry is not None) and is_entry_file(entry): + name = entry.pop("name") + if not "toplevel" in entry["spec"]: + entry["spec"]["toplevel"] = os.path.realpath( + os.path.join(os.path.dirname(fname), "specs") + ) + entry['metadata'].setdefault('tags',[]).extend(tags or []) + cfg[name] = entry + elif (entry is not None) and is_index_file(entry): + for index_f in entry['recast_catalogue_entries']: + index_f = os.path.join(os.path.dirname(fname),index_f) + log.debug(f'loading catalogue file {index_f}') + index_entry = yaml.safe_load(open(index_f)) + process_entry(cfg,index_f,index_entry,entry['tags']) + +def is_entry_file(entry): + schema = jsonschema.Draft7Validator({'required': ['name','metadata','spec']}) + try: + schema.validate(entry) + except jsonschema.exceptions.ValidationError: + return False + return True + +def is_index_file(entry): + schema = jsonschema.Draft7Validator({'required': ['name','tags','recast_catalogue_entries']}) + try: + schema.validate(entry) + except jsonschema.exceptions.ValidationError: + return False return True + diff --git a/src/recastatlas/subcommands/catalogue.py b/src/recastatlas/subcommands/catalogue.py index aafb407..45b080a 100644 --- a/src/recastatlas/subcommands/catalogue.py +++ b/src/recastatlas/subcommands/catalogue.py @@ -13,7 +13,7 @@ log = logging.getLogger(__name__) -default_meta = {"author": "unknown", "short_description": "no description"} +default_meta = {"author": "unknown", "short_description": "no description", "tags": []} @click.group(help="The RECAST Analysis Catalogue") @@ -87,8 +87,8 @@ def rm(path): @catalogue.command() def ls(): - fmt = "{0:35}{1:60}{2:20}" - click.secho(fmt.format("NAME", "DESCRIPTION", "EXAMPLES")) + fmt = "{0:35}{1:60}{2:20}{3:20}" + click.secho(fmt.format("NAME", "DESCRIPTION", "EXAMPLES", "TAGS")) for k, v in sorted(config.catalogue.items(), key=lambda x: x[0]): click.secho( @@ -96,6 +96,7 @@ def ls(): k, v.get("metadata", default_meta)["short_description"], ",".join(list(v.get("example_inputs", {}).keys())), + ",".join(v.get("metadata", default_meta).get('tags',[])) ) )