Skip to content

Commit

Permalink
Remove magic for logdir; adapt merge_vocab for split SKOS
Browse files Browse the repository at this point in the history
  • Loading branch information
dalito committed Aug 9, 2023
1 parent 245cb87 commit fdabdbb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Change log

## Release 0.6.x (2023-08-dd)

New features:

- The `merge_vocab` script gained support for directories containing vocabularies created with `voc4cat transform --split`. This feature is used in gh-actions of [voc4cat-template](https://github.com/nfdi4cat/voc4cat-template)-based vocabularies.

Changes:

- The log file will now always be written to the given directory. Previously the log file directory depended on the presence of the `--outdir` option.

Bug fixes:

- Sub-command `voc4cat docs` failed if `--outdir` was not given.

## Release 0.6.0 (2023-08-09)

New features:
Expand Down
4 changes: 1 addition & 3 deletions src/voc4cat/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def process_common_options(args):
if logfile is None:
setup_logging(loglevel)
else:
if outdir is not None:
logfile = Path(outdir) / logfile
elif not logfile.parents[0].exists():
if not logfile.parents[0].exists():
logfile.parents[0].mkdir(exist_ok=True, parents=True)
setup_logging(loglevel, logfile)

Expand Down
18 changes: 11 additions & 7 deletions src/voc4cat/merge_vocab.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# This script is mainly useful for CI.
import argparse
import logging
import os
import shutil
import subprocess
import sys
Expand All @@ -19,12 +18,18 @@ def main(ttl_inbox: Path, vocab: Path) -> int:
New files are copied, existing files are synced by git merge-file.
"""
retcode = 0
for p in os.listdir(ttl_inbox):
new = ttl_inbox / Path(p)
if new.suffix != ".ttl" or new.is_dir():
logger.info('Skipping "%s"', new)
for new in ttl_inbox.iterdir():
if new.is_dir():
logger.debug('Entering directory "%s"', new)
(vocab / new.name).mkdir(exist_ok=True)
retcode = main(new, vocab / new.name)
if retcode != 0:
break
continue
if new.suffix != ".ttl":
logger.debug('Skipping "%s"', new)
continue
if os.path.exists(vocab / Path(new).name):
if (vocab / Path(new).name).exists():
exists = vocab / Path(new).name
cmd = ["git", "merge-file", "--theirs", str(exists), str(exists), str(new)]
logger.info("Running cmd: %s", " ".join(cmd))
Expand Down Expand Up @@ -64,7 +69,6 @@ def main_cli(args=None) -> int:
setup_logging()
else:
outbox.mkdir(exist_ok=True, parents=True)
logfile = Path(outbox) / logfile
setup_logging(logfile=logfile)

if not has_outbox or not vocab.exists():
Expand Down
4 changes: 2 additions & 2 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_run_vocexcel_outputdir(monkeypatch, datadir, tmp_path, outputdir, testf
shutil.copy(datadir / testfile, tmp_path)
monkeypatch.chdir(tmp_path)
# Check if log is placed in out folder.
log = "test-run.log"
log = Path(outputdir) / "test-run.log"
main_cli(
["convert", "--logfile", str(log)]
+ (["--outdir", str(outputdir)] if outputdir else [])
Expand All @@ -56,7 +56,7 @@ def test_run_vocexcel_outputdir(monkeypatch, datadir, tmp_path, outputdir, testf
assert (outdir / testfile).with_suffix(".ttl").exists()
else:
assert (outdir / testfile).with_suffix(".xlsx").exists()
assert (outdir / log).exists()
assert (outdir / log.name).exists()


def test_duplicates(datadir, tmp_path, caplog):
Expand Down
31 changes: 27 additions & 4 deletions tests/test_merge_vocab.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest import mock

import pytest
from test_cli import CS_CYCLES_TURTLE
from test_cli import CS_CYCLES_TURTLE, CS_SIMPLE_TURTLE
from voc4cat.merge_vocab import main_cli


Expand Down Expand Up @@ -36,17 +36,40 @@ def test_main_merge_dirs(datadir, tmp_path, caplog):
vocab.mkdir()
ttl_inbox = tmp_path / "ttl_inbox"
ttl_inbox.mkdir()
extra = ttl_inbox / "extra"
extra.mkdir()
extra = ttl_inbox / "extra.txt"
extra.touch()
splitvoc = ttl_inbox / "splitvoc"
splitvoc.mkdir()

shutil.copy(datadir / CS_CYCLES_TURTLE, ttl_inbox / CS_CYCLES_TURTLE)
shutil.copy(datadir / CS_SIMPLE_TURTLE, splitvoc)

with caplog.at_level(logging.INFO):
with caplog.at_level(logging.DEBUG):
exit_code = main_cli([str(ttl_inbox), str(vocab)])
assert f'Skipping "{extra}"' in caplog.text
assert (vocab / CS_CYCLES_TURTLE).exists()
assert (vocab / "splitvoc" / CS_SIMPLE_TURTLE).exists()
assert exit_code == 0


def test_main_merge_split_vocab_dir(datadir, tmp_path, caplog):
"""Check merge of dir with split vocab."""
vocab = tmp_path / "vocab"
(vocab / "splitvoc").mkdir(parents=True)
ttl_inbox = tmp_path / "ttl_inbox"
(ttl_inbox / "splitvoc").mkdir(parents=True)
shutil.copy(datadir / CS_SIMPLE_TURTLE, ttl_inbox / "splitvoc")
shutil.copy(datadir / CS_SIMPLE_TURTLE, vocab / "splitvoc")

with caplog.at_level(logging.DEBUG), mock.patch(
"voc4cat.merge_vocab.subprocess"
) as subprocess:
subprocess.Popen.return_value.returncode = 1
exit_code = main_cli([str(ttl_inbox), str(vocab)])
assert "Entering directory" in caplog.text
assert exit_code == 1


def test_main_merge_files(datadir, tmp_path, caplog):
"""Check merge that merges the content of files."""
vocab = tmp_path / "vocab"
Expand Down

0 comments on commit fdabdbb

Please sign in to comment.