Skip to content

Commit

Permalink
Merge branch 'issue17' into 'main'
Browse files Browse the repository at this point in the history
Add tests for wrapper & merge_vocab

Closes #17

See merge request nfdi4cat/ta1-ontologies/voc4cat-tool!43
  • Loading branch information
dalito committed Dec 5, 2022
2 parents d416cf7 + 7e6a48b commit c9227fb
Show file tree
Hide file tree
Showing 15 changed files with 774 additions and 119 deletions.
4 changes: 2 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ before_script:
tests:
stage: test
script:
- pip install -U pytest
- pip install .
# install project inlcuding optional test dependencies
- pip install -U .[tests]
- pytest

# https://docs.gitlab.com/ee/ci/testing/test_coverage_visualization.html#python-example
Expand Down
18 changes: 12 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ dependencies = [
# We install vocexcel directly from a specific branch form github (or gitea in LIKAT)
"vocexcel @ git+https://github.com/nfdi4cat/VocExcel.git@nfdi4cat-dev",
#vocexcel @ git+https://gogs.catalysis.de/LIKAT_RDM/VocExcel.git@nfdi4cat-dev
"django>=4.0.0",
"networkx>=2.8",
"openpyxl>=3.0.9",
"pillow>=9.1.0",
"ontospy[Full] >= 1.9.9.4",
"django >= 4.1.3",
"networkx >= 2.8",
"openpyxl >= 3.0.9",
"pillow >= 9.1.0",
"ontospy >= 2.1.0",
]

#dynamic = ["version", "readme"]
Expand All @@ -58,7 +58,12 @@ tests = [
lint = [
"black",
"isort",
"wemake-python-styleguide",
]
dev = [
# Recursively including the project's own optional dependencies requires pip>=21.2
"voc4cat[tests,lint]",
"ruff",
"wemake-python-styleguide",
]

[project.scripts]
Expand Down Expand Up @@ -155,6 +160,7 @@ exclude_lines = [
"raise AssertionError",
"raise NotImplementedError",
"return NotImplemented",
"if __name__ == .__main__.:",
]

[tool.coverage.html]
Expand Down
11 changes: 11 additions & 0 deletions src/voc4cat/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
from importlib.metadata import PackageNotFoundError, version

try:
__version__ = version("voc4cat")
except PackageNotFoundError:
# package is not installed
try:
from ._version import version as __version__
except ImportError:
__version__ = "0.0.0"
43 changes: 23 additions & 20 deletions src/voc4cat/merge_vocab.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
# -*- coding: utf-8 -*-
# This script is mainly useful for CI.
import os
import shutil
import subprocess
import sys
from pathlib import Path

VOCAB_DIR = Path("vocabularies")
OUTBOX = Path("outbox")


def main(outbox, vocab):
def main(ttl_inbox, vocab):
"""
Sync ttl-files in outbox and vocab folder
Sync ttl-files from ttl_inbox into vocab folder
New files are copied, existing files are synced by git merge-file.
"""
retcode = 0
for p in os.listdir(outbox):
new = outbox / Path(p)
for p in os.listdir(ttl_inbox):
new = ttl_inbox / Path(p)
if not new.suffix == ".ttl" or new.is_dir():
print(f'skipping "{new}"')
print(f'Skipping "{new}"')
continue
if os.path.exists(vocab / Path(new).name):
exists = vocab / Path(new).name
cmd = ["git", "merge-file", "--theirs", str(exists), str(exists), str(new)]
print(" ".join(cmd))
print("Running cmd: {0}".format(" ".join(cmd)))
outp = subprocess.run(cmd, capture_output=True)
print(outp.stdout)
if retcode := outp.returncode != 0:
break
else:
print(f'copying "{new}" to "{vocab}"')
print(f'Copying "{new}" to "{vocab}"')
shutil.copy(new, vocab)
return retcode


def run():
if len(sys.argv) != 3:
print('Usage: "python merge_vocab.py outbox_dir vocab_dir')
sys.exit(1)
outbox, vocab = sys.argv[1:3]
def main_cli(args=None):

if args is None: # script run via entrypoint
args = sys.argv[1:]

if len(args) != 2:
print("Usage: python merge_vocab.py <outbox_dir> <vocab_dir>")
return 1
outbox, vocab = args
if os.path.exists(outbox) and os.path.exists(vocab):
retcode = main(Path(outbox), Path(vocab))
sys.exit(retcode)
else:
print(f'This script requires both folders to exist: "{outbox}" and "{vocab}"')
sys.exit(1)
return retcode

print(f'This script requires both folders to exist: "{outbox}" and "{vocab}"')
return 1


if __name__ == "__main__":
run()
err = main_cli(sys.argv[1:])
sys.exit(err)
6 changes: 3 additions & 3 deletions src/voc4cat/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def dag_to_node_levels(termgraph, baselevel=0):
for sgc in nx.connected_components(termgraph.to_undirected())
]
tg_copy = termgraph.copy()
# We must find & eliminate cycles no matter which direction for the tree representation.
# We must find & eliminate cycles no matter if directed or undirected.
cycles = nx.cycle_basis(tg_copy.to_undirected())
broken_edges = []
while cycles:
Expand All @@ -125,8 +125,8 @@ def dag_to_node_levels(termgraph, baselevel=0):
tg_copy.remove_edge(*edge_to_break)
# nx.cycle_basis does only report cycles >= 3 members.
cycles = nx.cycle_basis(tg_copy.to_undirected())
# It is not clearly documented in networkx if the direction of edges is preserved
# when converting to an undirected graph. So we better check...
# It is not clearly documented in networkx if the direction of edges is
# preserved when converting to an undirected graph. So we better check...
if termgraph.has_edge(*edge_to_break):
broken_edges.append(edge_to_break)
else: # pragma: no cover
Expand Down
Loading

0 comments on commit c9227fb

Please sign in to comment.