Skip to content

Commit

Permalink
Replace print by logging where it makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
dalito committed Jul 13, 2023
1 parent b42d342 commit 41c7712
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 40 deletions.
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Example files showing typical conversions with voc4cat:
- A draft of a photocatalysis vocabulary: This has still the original structure and contents; only spelling and link errors were fixed.
- The hierarchy was created with voc4cat 0.30 by the `-r` / `--add-related` option from the extra column on the right in sheets Concepts & Collections. Note, this option was removed in 0.4.0 to foster using numeric IDs.
- `Photocatalysis_LIKAT_template043_textids.xlsx`
- Same contents as the first file but the extra column in the sheets Concepts & Collections was removed. The concepts were sorted by IRI (this was done to assign IDs in a reproducible way to domonstrate round-tripping, see below).
- Same contents as the first file but the extra column in the sheets Concepts & Collections was removed. The concepts were sorted by IRI (this was done to assign IDs in a reproducible way to demonstrate round-tripping, see below).
- `Photocatalysis_LIKAT_template043_final.xlsx`
- This file was generate from the previous one by replacing the textual IDs with numeric ones using the `--make-ids` option added in version 0.4.0.
- `Photocatalysis_LIKAT_template043_final.ttl`
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ max-complexity = 10
classmethod-decorators = ["classmethod", "pydantic.validator"]

[tool.codespell]
skip = "pyproject.toml,./vocabularies,./example,./tests/data,./tmp"
skip = "*.xlsx,pyproject.toml,./vocabularies,./example,./tests/data,./tmp"
# Note: words have to be lowercased for the ignore-words-list
ignore-words-list = "linke"
quiet-level = 3
1 change: 1 addition & 0 deletions src/voc4cat/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ def main(args=None):
if args.logfile:
# We import here to avoid a cyclic import when this is used via wrapper.main
from voc4cat.wrapper import setup_logging

setup_logging(logfile=args.logfile)
elif run_via_entrypoint:
setup_logging()
Expand Down
42 changes: 23 additions & 19 deletions src/voc4cat/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def make_ids(fpath, outfile, search_prefix, start_id):
If new_prefix is None the new IRI is a concatenation of VOC_BASE_IRI and ID.
If new_prefix is given the new IRI is the CURI new_prefix:ID.
"""
print(f"\nReplacing {search_prefix}:... IRIs.")
logger.info("\nReplacing {search_prefix}:... IRIs.")
# Load in data_only mode to get cell values not formulas.
wb = openpyxl.load_workbook(fpath, data_only=True)
is_supported_template(wb)
Expand Down Expand Up @@ -145,7 +145,8 @@ def make_ids(fpath, outfile, search_prefix, start_id):
iri_new = replaced_iris[iri]
else:
iri_new = voc_base_iri + f"{next(id_gen):07d}"
print(f"[{sheet}] Replaced CURI {iri} by {iri_new}")
msg = f"[{sheet}] Replaced CURI {iri} by {iri_new}"
logger.debug(msg)
replaced_iris[iri] = iri_new
row[0].value = iri_new

Expand All @@ -168,15 +169,16 @@ def make_ids(fpath, outfile, search_prefix, start_id):
)
if count_new_iris:
new_iris = [replaced_iris.get(iri, iri) for iri in iris]
print(
msg = (
f"[{sheet}] Replaced {count_new_iris} CURIs "
f"in cell {row[col].coordinate}"
)
logger.debug(msg)
row[col].value = ", ".join(new_iris)
# subsequent_empty_rows = 0

wb.save(outfile)
print(f"Saved updated file as {outfile}")
logger.info("Saved updated file as %s", outfile)
return 0


Expand All @@ -187,7 +189,7 @@ def hierarchy_from_indent(fpath, outfile, sep):
If separator character(s) are given they will be replaced with
Excel indent which is also the default if sep is None.
"""
print(f"\nReading concepts from file {fpath}")
logger.info("Reading concepts from file %s", fpath)
# Load in data_only mode to get cell values not formulas.
wb = openpyxl.load_workbook(fpath, data_only=True)
is_supported_template(wb)
Expand Down Expand Up @@ -267,7 +269,7 @@ def hierarchy_from_indent(fpath, outfile, sep):
row += 1

wb.save(outfile)
print(f"Saved updated file as {outfile}")
logger.info("Saved updated file as %s", outfile)
return 0


Expand All @@ -278,7 +280,7 @@ def hierarchy_to_indent(fpath, outfile, sep):
If separator character(s) are given they will be used.
If sep is None, Excel indent will be used.
"""
print(f"\nReading concepts from file {fpath}")
logger.info("Reading concepts from file %s", fpath)
wb = openpyxl.load_workbook(fpath)
is_supported_template(wb)
ws = wb["Concepts"]
Expand Down Expand Up @@ -363,7 +365,7 @@ def hierarchy_to_indent(fpath, outfile, sep):
iri_written.append((iri, lang))

wb.save(outfile)
print(f"Saved updated file as {outfile}")
logger.info("Saved updated file as %s", outfile)
return 0


Expand All @@ -378,7 +380,7 @@ def run_pylode(file_path, output_path):
return 1

for turtle_file in turtle_files:
print(f"\nBuilding pyLODE documentation for {turtle_file}")
logger.info("Building pyLODE documentation for %s", turtle_file)
filename = Path(turtle_file) # .resolve())
outdir = output_path / filename.with_suffix("").name
outdir.mkdir(exist_ok=True)
Expand All @@ -400,7 +402,7 @@ def run_pylode(file_path, output_path):
)
with open(outfile, "w") as html_file:
html_file.write(content)
print(f"Done!\n=> {outfile.resolve().as_uri()}")
logger.info("Done!\n=> %s", outfile.resolve().as_uri())
return 0


Expand All @@ -419,7 +421,7 @@ def run_ontospy(file_path, output_path):
for turtle_file in turtle_files:
title = config.VOCAB_TITLE

print(f"\nBuilding ontospy documentation for {turtle_file}")
logger.info("Building ontospy documentation for %s", turtle_file)
specific_output_path = (Path(output_path) / Path(turtle_file).stem).resolve()

g = ontospy.Ontospy(Path(turtle_file).resolve().as_uri())
Expand All @@ -439,12 +441,12 @@ def find_files_to_document(file_path):
if Path(file_path).is_dir():
turtle_files = glob.glob(f"{file_path}/*.ttl")
if not turtle_files:
print(f"No turtle file(s) found to document in {file_path}")
logger.warning("No turtle file(s) found to document in %s", file_path)
turtle_files = []
elif Path(file_path).exists():
turtle_files = [file_path]
else:
print(f"File/dir not found (for docs): {file_path}")
logger.warning("File/dir not found (for docs): %s", file_path)
turtle_files = []
return turtle_files

Expand All @@ -458,7 +460,7 @@ def build_docs(file_path, output_path, doc_builder):
elif doc_builder == "pylode":
errcode = run_pylode(file_path, output_path)
else:
print(f"Unsupported document builder '{doc_builder}'.")
logger.error("Unsupported document builder '%s'.", doc_builder)
errcode = 1
return errcode

Expand All @@ -471,7 +473,7 @@ def check(fpath, outfile):
- The "Concept IRI" must be unique; this is the case when no language
is used more than once per concept.
"""
print(f"\nRunning check of Concepts sheet for file {fpath}")
logger.info("Running check of Concepts sheet for file %s", fpath)
wb = openpyxl.load_workbook(fpath)
is_supported_template(wb)
ws = wb["Concepts"]
Expand All @@ -489,10 +491,11 @@ def check(fpath, outfile):
new_concept_iri = f'"{concept_iri}"@{lang.lower()}'
if new_concept_iri in seen_concept_iris:
failed_check = True
print(
msg = (
f'ERROR: Same Concept IRI "{concept_iri}" used more than once for '
f'language "{lang}"'
)
logger.error(msg)
# colorize problematic cells
row[0].fill = color
row[2].fill = color
Expand All @@ -513,14 +516,15 @@ def check(fpath, outfile):

if failed_check:
wb.save(outfile)
print(f"Saved file with highlighted errors as {outfile}")
logger.info("Saved file with highlighted errors as %s", outfile)
return 1

print("All checks passed successfully.")
logger.info("All checks passed successfully.")

if fpath != outfile: # Save to new directory (important for --forward option)
wb.save(outfile)
print(f"Saved checked file as {outfile}")
msg = f"Saved checked file as {outfile}"
logger.info(msg)

return 0

Expand Down
40 changes: 21 additions & 19 deletions tests/test_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import os
import shutil
from pathlib import Path
Expand Down Expand Up @@ -442,30 +443,31 @@ def test_build_docs_ontospy(datadir, tmp_path, test_file):
"doc_builder",
["pylode", "ontospy"],
)
def test_build_docs(tmp_path, capsys, doc_builder):
def test_build_docs(tmp_path, caplog, doc_builder):
"""Check handling of missing dir/file on documentation build."""
exit_code = build_docs(tmp_path, tmp_path, doc_builder)
captured = capsys.readouterr()
with caplog.at_level(logging.INFO):
exit_code = build_docs(tmp_path, tmp_path, doc_builder)
assert exit_code == 1
assert f"No turtle file(s) found to document in {tmp_path}" in captured.out
exit_code = build_docs(tmp_path / CS_CYCLES_TURTLE, tmp_path, doc_builder)
captured = capsys.readouterr()
assert f"No turtle file(s) found to document in {tmp_path}" in caplog.text

with caplog.at_level(logging.INFO):
exit_code = build_docs(tmp_path / CS_CYCLES_TURTLE, tmp_path, doc_builder)
assert exit_code == 1
assert f"File/dir not found (for docs): {tmp_path/CS_CYCLES_TURTLE}" in captured.out
assert f"File/dir not found (for docs): {tmp_path/CS_CYCLES_TURTLE}" in caplog.text

exit_code = build_docs(tmp_path / CS_CYCLES_TURTLE, tmp_path, "ontospy")
captured = capsys.readouterr()
with caplog.at_level(logging.INFO):
exit_code = build_docs(tmp_path / CS_CYCLES_TURTLE, tmp_path, "ontospy")
assert exit_code == 1
assert f"File/dir not found (for docs): {tmp_path/CS_CYCLES_TURTLE}" in captured.out
assert f"File/dir not found (for docs): {tmp_path/CS_CYCLES_TURTLE}" in caplog.text


def test_build_docs_unknown_builder(tmp_path, capsys):
def test_build_docs_unknown_builder(tmp_path, caplog):
"""Check handling of unknown documentation builder."""
unknown_doc_builder = "123doc"
exit_code = build_docs(tmp_path, tmp_path, unknown_doc_builder)
captured = capsys.readouterr()
with caplog.at_level(logging.INFO):
exit_code = build_docs(tmp_path, tmp_path, unknown_doc_builder)
assert exit_code == 1
assert f"Unsupported document builder '{unknown_doc_builder}'." in captured.out
assert f"Unsupported document builder '{unknown_doc_builder}'." in caplog.text


@pytest.mark.parametrize(
Expand All @@ -481,14 +483,14 @@ def test_build_docs_unknown_builder(tmp_path, capsys):
],
ids=["no error", "with error"],
)
def test_check(datadir, tmp_path, capsys, test_file, err, msg): # noqa: PLR0913
def test_check(datadir, tmp_path, caplog, test_file, err, msg): # noqa: PLR0913
dst = tmp_path / test_file
shutil.copy(datadir / test_file, dst)
exit_code = main_cli(["--check", "--no-warn", str(dst)])
captured = capsys.readouterr()
# TODO check that erroneous cells get colored.
with caplog.at_level(logging.INFO):
exit_code = main_cli(["--check", "--no-warn", str(dst)])
assert exit_code == err
assert msg in captured.out
assert msg in caplog.text
# TODO check that erroneous cells get colored.


def test_unsupported_filetype(datadir, capsys):
Expand Down

0 comments on commit 41c7712

Please sign in to comment.