From d8575a8c497f856aa1e00965e169863fdc7f02de Mon Sep 17 00:00:00 2001 From: Rohan-Dedhia Date: Thu, 26 Nov 2026 18:37:56 +0530 Subject: [PATCH 1/2] fix(format): strip all recognized extensions and create parent dir on same-format copy --- databusclient/filehandling/format.py | 7 +++++-- tests/test_format_round_trips.py | 30 +++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/databusclient/filehandling/format.py b/databusclient/filehandling/format.py index c1cd4ec1..be559e95 100644 --- a/databusclient/filehandling/format.py +++ b/databusclient/filehandling/format.py @@ -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 @@ -524,7 +524,10 @@ 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: + if os.path.abspath(input_file) != os.path.abspath(output_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}'. " diff --git a/tests/test_format_round_trips.py b/tests/test_format_round_trips.py index b828ddb8..6f15a778 100644 --- a/tests/test_format_round_trips.py +++ b/tests/test_format_round_trips.py @@ -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}" - ) \ No newline at end of file + ) + + + +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() From b88f5d722da874edc7bbc23b7e06e4f4f37132d3 Mon Sep 17 00:00:00 2001 From: Rohan-Dedhia Date: Thu, 26 Nov 2026 19:03:54 +0530 Subject: [PATCH 2/2] fix(format): use os.path.samefile for same-format copy check --- databusclient/filehandling/format.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/databusclient/filehandling/format.py b/databusclient/filehandling/format.py index be559e95..20f6e5fa 100644 --- a/databusclient/filehandling/format.py +++ b/databusclient/filehandling/format.py @@ -524,7 +524,12 @@ 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 os.path.abspath(input_file) != os.path.abspath(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)