Skip to content

Commit

Permalink
Fix edge case of calling check sub-cmd without args
Browse files Browse the repository at this point in the history
  • Loading branch information
dalito committed Aug 10, 2023
1 parent 2f95e4a commit 4be1442
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
38 changes: 25 additions & 13 deletions src/voc4cat/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ def check_xlsx(fpath: Path, outfile: Path) -> int:
logger.info("-> Extended xlsx checks passed successfully.")


def ci_post(args):
prev_vocab_dir, vocab_new = args.ci_post, args.VOCAB
for vocfile in glob.glob(str(vocab_new.resolve() / "*.ttl")):
new = Path(vocfile)
vocfile_name = Path(vocfile).name
prev = prev_vocab_dir / vocfile_name
if not prev.exists():
logger.debug(
'-> previous version of vocabulary "%s" does not exist.',
vocfile_name,
)
continue
check_for_removed_iris(prev, new)
logger.info("-> Check ci-post passed.")


# ===== check command & helpers to validate cmd options =====


Expand All @@ -109,6 +125,14 @@ def check(args):

_check_ci_args(args)

if not args.VOCAB and not args.listprofiles:
msg = (
"Argument VOCAB is required for this sub-command (except for "
"option --listprofiles)."
)
logger.error(msg)
return

if args.listprofiles:
s = "\nProfiles\nToken\tIRI\n-----\t-----\n"
for k, v in profiles.PROFILES.items():
Expand All @@ -124,19 +148,7 @@ def check(args):
return

if args.ci_post:
prev_vocab_dir, vocab_new = args.ci_post, args.VOCAB
for vocfile in glob.glob(str(vocab_new.resolve() / "*.ttl")):
new = Path(vocfile)
vocfile_name = Path(vocfile).name
prev = prev_vocab_dir / vocfile_name
if not prev.exists():
logger.debug(
'-> previous version of vocabulary "%s" does not exist.',
vocfile_name,
)
continue
check_for_removed_iris(prev, new)
logger.info("-> Check ci-post passed.")
ci_post(args)
return

files = [args.VOCAB] if args.VOCAB.is_file() else [*Path(args.VOCAB).iterdir()]
Expand Down
34 changes: 19 additions & 15 deletions src/voc4cat/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,24 @@ def check_names_not_empty(cls, values):
ID_RANGES_BY_ACTOR = defaultdict(list)


def _id_ranges_by_actor(new_conf):
# create look-up map for ID ranges of all "actors"
id_ranges_by_actor = defaultdict(list)
for name in new_conf["IDRANGES"].vocabs:
voc = new_conf["IDRANGES"].vocabs.get(name)
for idr in voc.id_range:
rng = (idr.first_id, idr.last_id)
if idr.orcid:
id_ranges_by_actor[str(idr.orcid)].append(rng)
no_url_orcid = str(idr.orcid).split("orcid.org/")[-1]
id_ranges_by_actor[no_url_orcid].append(rng)
if idr.gh_name:
id_ranges_by_actor[str(idr.gh_name)].append(rng)
if idr.ror_id:
id_ranges_by_actor[str(idr.ror_id)].append(rng)
return id_ranges_by_actor


def load_config(config_file: Path | None = None, config: IDrangeConfig | None = None):
new_conf = {}
new_conf["ID_PATTERNS"] = {}
Expand All @@ -135,21 +153,7 @@ def load_config(config_file: Path | None = None, config: IDrangeConfig | None =
id_patterns[name] = re.compile(r"(?P<identifier>[0-9]{%i})$" % voc.id_length)
new_conf["ID_PATTERNS"] = id_patterns

# create look-up map for ID ranges of all "actors"
id_ranges_by_actor = defaultdict(list)
for name in new_conf["IDRANGES"].vocabs:
voc = new_conf["IDRANGES"].vocabs.get(name)
for idr in voc.id_range:
rng = (idr.first_id, idr.last_id)
if idr.orcid:
id_ranges_by_actor[str(idr.orcid)].append(rng)
no_url_orcid = str(idr.orcid).split("orcid.org/")[-1]
id_ranges_by_actor[no_url_orcid].append(rng)
if idr.gh_name:
id_ranges_by_actor[str(idr.gh_name)].append(rng)
if idr.ror_id:
id_ranges_by_actor[str(idr.ror_id)].append(rng)
new_conf["ID_RANGES_BY_ACTOR"] = id_ranges_by_actor
new_conf["ID_RANGES_BY_ACTOR"] = _id_ranges_by_actor(new_conf)

for name, value in new_conf.items():
globals()[name] = value
6 changes: 6 additions & 0 deletions tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def test_check_list_profiles(capsys):
assert "Profiles" in captured.out


def test_check_missing_vocab(caplog):
with caplog.at_level(logging.ERROR):
main_cli(["check"])
assert "Argument VOCAB is required for this sub-command" in caplog.text


def test_check_overwrite_warning(monkeypatch, datadir, tmp_path, caplog):
shutil.copy(datadir / CS_SIMPLE, tmp_path / CS_SIMPLE)
monkeypatch.chdir(tmp_path)
Expand Down

0 comments on commit 4be1442

Please sign in to comment.