Skip to content

Commit

Permalink
Merge pull request #39 from recast-hep/index_files
Browse files Browse the repository at this point in the history
index file support
  • Loading branch information
lukasheinrich authored Sep 17, 2021
2 parents e9fe58b + c799f2a commit 3a5f2e3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
include_package_data = True,
install_requires = [
'click',
'jsonschema',
'pyyaml',
'yadage-schemas==0.10.6',
],
Expand Down
48 changes: 34 additions & 14 deletions src/recastatlas/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pkg_resources
import glob
import logging
import jsonschema

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -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

7 changes: 4 additions & 3 deletions src/recastatlas/subcommands/catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -87,15 +87,16 @@ 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(
fmt.format(
k,
v.get("metadata", default_meta)["short_description"],
",".join(list(v.get("example_inputs", {}).keys())),
",".join(v.get("metadata", default_meta).get('tags',[]))
)
)

Expand Down

0 comments on commit 3a5f2e3

Please sign in to comment.