Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions databusclient/filehandling/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def get_converted_filename(original_filename: str, convert_format: str) -> str:
break

# strip existing format extension (longest first)
for old_ext in sorted(FORMAT_TO_EXTENSION.values(), key=len, reverse=True):
for old_ext in sorted(EXTENSION_TO_FORMAT.keys(), key=len, reverse=True):
if name.lower().endswith(old_ext):
name = name[: -len(old_ext)]
break
Expand Down Expand Up @@ -524,7 +524,15 @@ def convert_file(
if input_format == convert_format:
# Input and target format are identical.
# Copy input to output path so the caller always receives an output file.
if input_file != output_file:
same_file = (
os.path.exists(input_file)
and os.path.exists(output_file)
and os.path.samefile(input_file, output_file)
)
if not same_file:
parent = os.path.dirname(output_file)
if parent:
os.makedirs(parent, exist_ok=True)
shutil.copy2(input_file, output_file)
print(
f"Input and target format are both '{input_format}'. "
Expand Down
30 changes: 29 additions & 1 deletion tests/test_format_round_trips.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,32 @@ def test_mapping_quads_to_csv_and_back():
assert csv_graphs == original_graph_uris, (
f"Graph URIs in CSV do not match original. "
f"Expected: {original_graph_uris}, got: {csv_graphs}"
)
)



def test_get_converted_filename_strips_all_recognized_extensions():
"""Ensure .owl, .xml, and .json extensions are stripped just like primary ones."""
from databusclient.filehandling.format import get_converted_filename

assert get_converted_filename("ontology.owl", "turtle") == "ontology.ttl"
assert get_converted_filename("data.xml.gz", "ntriples") == "data.nt"
assert get_converted_filename("graph.json", "nquads") == "graph.nq"
assert get_converted_filename("graph.jsonld.bz2", "trig") == "graph.trig"


def test_convert_file_same_format_creates_parent_dir_and_handles_same_path(
tmp_path, monkeypatch
):
"""Same-format copy creates missing parent dirs and skips equivalent paths."""
from databusclient.filehandling.format import convert_file

source = resource("sample.ttl")
nested_out = tmp_path / "nested" / "subdir" / "copy.ttl"
convert_file(source, str(nested_out), "turtle")
assert nested_out.exists()

# Equivalent path via relative reference should not raise SameFileError
monkeypatch.chdir(nested_out.parent)
convert_file("copy.ttl", os.path.join(".", "copy.ttl"), "ttl")
assert nested_out.exists()