From a21e14d9c9adaa04390dc33691a2c0735652c164 Mon Sep 17 00:00:00 2001 From: Aakarsh2007 <183278592+Aakarsh2007@users.noreply.github.com> Date: Tue, 1 Dec 2026 22:57:09 +0530 Subject: [PATCH] fix: derive download filenames from URL paths --- CHANGELOG.md | 3 +++ databusclient/api/download.py | 2 +- databusclient/api/utils.py | 5 ++++- pyproject.toml | 2 +- tests/test_api_utils.py | 15 +++++++++++++++ tests/test_download.py | 13 +++++++++++++ 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 80baae66..c52a3794 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ All notable changes to this project will be documented in this file. ### Changed - File downloads now identify as `databus-python-client/ (https://github.com/dbpedia/databus-python-client)` instead of the Requests library default. +### Fixed +- Databus resource parsing and local download names use the URL path, so query strings and fragments do not change the resource type or filename. + ## [0.15] - 2025-12-31 ### Added diff --git a/databusclient/api/download.py b/databusclient/api/download.py index f2aead78..42bedafa 100644 --- a/databusclient/api/download.py +++ b/databusclient/api/download.py @@ -471,7 +471,7 @@ def _download_file( if not local_dir_was_given: print(f"local directory not given, using {os.path.relpath(localDir)}") - file = url.split("/")[-1] + file = urlparse(url).path.rsplit("/", 1)[-1] filename = os.path.join(localDir, file) print(f"Download file: {url}") dirpath = os.path.dirname(filename) diff --git a/databusclient/api/utils.py b/databusclient/api/utils.py index ba6dfe5a..1c53448e 100644 --- a/databusclient/api/utils.py +++ b/databusclient/api/utils.py @@ -5,6 +5,7 @@ """ from typing import Optional, Tuple +from urllib.parse import urlsplit import hashlib import requests @@ -29,7 +30,9 @@ def get_databus_id_parts_from_file_url( Each element is a string or None if not present. """ - uri = uri.removeprefix("https://").removeprefix("http://") + if uri.startswith(("https://", "http://")): + parsed = urlsplit(uri) + uri = parsed.netloc + parsed.path parts = uri.strip("/").split("/") parts += [None] * (6 - len(parts)) # pad with None if less than 6 parts return tuple(parts[:6]) # return only the first 6 parts diff --git a/pyproject.toml b/pyproject.toml index d44c5483..c9d07c08 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "databusclient" -version = "1.3.2" +version = "1.3.3" description = "A simple client for submitting, downloading, and deleting data on the DBpedia Databus" authors = ["DBpedia Association"] license = "Apache-2.0 License" diff --git a/tests/test_api_utils.py b/tests/test_api_utils.py index fe9ec053..cfbe649a 100644 --- a/tests/test_api_utils.py +++ b/tests/test_api_utils.py @@ -36,6 +36,21 @@ def test_split_short_url_fills_none(): ) +def test_split_databus_version_ignores_query_and_fragment(): + result = get_databus_id_parts_from_file_url( + "https://databus.dbpedia.org/account/group/artifact/2026" + "?format=text/turtle#details" + ) + assert result == ( + "databus.dbpedia.org", + "account", + "group", + "artifact", + "2026", + None, + ) + + def test_split_http_same_as_https(): result = get_databus_id_parts_from_file_url( "http://databus.dbpedia.org/dbpedia/mappings/geo-coordinates/2022.12.01/file.ttl" diff --git a/tests/test_download.py b/tests/test_download.py index b1746d38..e834b25c 100644 --- a/tests/test_download.py +++ b/tests/test_download.py @@ -59,6 +59,19 @@ def test_with_query(): api_download("tmp", DEFAULT_ENDPOINT, [TEST_QUERY]) +@pytest.mark.parametrize("suffix", ["?download=true", "?format=text/turtle#details"]) +def test_cli_download_uses_url_path_for_local_filename(monkeypatch, tmp_path, suffix): + url = f"https://example.org/account/group/artifact/2026/file.ttl{suffix}" + data = b"

.\n" + _mock_download(monkeypatch, {url: data}) + + result = CliRunner().invoke(app, ["download", url, "--localdir", str(tmp_path)]) + + assert result.exit_code == 0, result.output or repr(result.exception) + destination = tmp_path / "account" / "group" / "artifact" / "2026" / "file.ttl" + assert destination.read_bytes() == data + + @pytest.mark.skip( reason="Live collection download is long-running and flakes on network timeouts" )