add route combination

This commit is contained in:
Florian Förster 2025-03-14 12:21:00 +01:00
parent 2f8ffe4feb
commit 65afc249bc
2 changed files with 55 additions and 8 deletions

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import pickle import pickle
import re
import shutil import shutil
from collections.abc import Sequence from collections.abc import Sequence
from pathlib import Path from pathlib import Path
@ -95,6 +96,16 @@ def prepare_path(
return (pth_parent / filename).with_suffix(suffix) return (pth_parent / filename).with_suffix(suffix)
def _strip_url_components(string: str) -> str:
return re.sub(r"^[ /]+|[ /]+$", "", string)
def combine_route(base_url: str, route: str) -> str:
base_url = _strip_url_components(base_url)
route = _strip_url_components(route)
return "/".join((base_url, route))
def search_cwd( def search_cwd(
glob_pattern: str, glob_pattern: str,
) -> Path | None: ) -> Path | None:

View File

@ -70,7 +70,7 @@ def test_create_folder(tmp_path, delete_existing):
assert target_dir.is_dir() assert target_dir.is_dir()
def test_prepare_save_path_SuccessWithCreate(tmp_path): def test_prepare_path_SuccessWithCreate(tmp_path):
base_folder = tmp_path base_folder = tmp_path
dirs = ("target", "dir") dirs = ("target", "dir")
filename = None filename = None
@ -81,7 +81,7 @@ def test_prepare_save_path_SuccessWithCreate(tmp_path):
assert res_pth == target_pth assert res_pth == target_pth
def test_prepare_save_path_SuccessWithCreateTimestamp(tmp_path): def test_prepare_path_SuccessWithCreateTimestamp(tmp_path):
base_folder = tmp_path base_folder = tmp_path
dirs = ("target", "dir") dirs = ("target", "dir")
filename = "test" filename = "test"
@ -92,7 +92,7 @@ def test_prepare_save_path_SuccessWithCreateTimestamp(tmp_path):
assert res_pth.parent.exists() assert res_pth.parent.exists()
def test_prepare_save_path_SuccessWithoutCreate(tmp_path): def test_prepare_path_SuccessWithoutCreate(tmp_path):
base_folder = tmp_path base_folder = tmp_path
dirs = ("target", "dir") dirs = ("target", "dir")
filename = None filename = None
@ -103,7 +103,7 @@ def test_prepare_save_path_SuccessWithoutCreate(tmp_path):
assert res_pth == target_pth assert res_pth == target_pth
def test_prepare_save_path_FailNoTargets(tmp_path): def test_prepare_path_FailNoTargets(tmp_path):
base_folder = tmp_path base_folder = tmp_path
dirs = None dirs = None
filename = None filename = None
@ -118,7 +118,7 @@ def test_prepare_save_path_FailNoTargets(tmp_path):
) )
def test_prepare_save_path_FailNoFilenameSuffix(tmp_path): def test_prepare_path_FailNoFilenameSuffix(tmp_path):
base_folder = tmp_path base_folder = tmp_path
dirs = None dirs = None
filename = None filename = None
@ -144,7 +144,7 @@ def test_prepare_save_path_FailNoFilenameSuffix(tmp_path):
) )
def test_prepare_save_path_FailTimestampWithoutFilename(tmp_path): def test_prepare_path_FailTimestampWithoutFilename(tmp_path):
base_folder = tmp_path base_folder = tmp_path
dirs = ["test"] dirs = ["test"]
filename = None filename = None
@ -160,7 +160,7 @@ def test_prepare_save_path_FailTimestampWithoutFilename(tmp_path):
) )
def test_prepare_save_path_FailBadSuffix(tmp_path): def test_prepare_path_FailBadSuffix(tmp_path):
base_folder = tmp_path base_folder = tmp_path
dirs = None dirs = None
filename = "test" filename = "test"
@ -176,7 +176,7 @@ def test_prepare_save_path_FailBadSuffix(tmp_path):
) )
def test_prepare_save_path_SuccessSuffixAddDot(tmp_path): def test_prepare_path_SuccessSuffixAddDot(tmp_path):
base_folder = tmp_path base_folder = tmp_path
dirs = None dirs = None
filename = "test" filename = "test"
@ -193,6 +193,42 @@ def test_prepare_save_path_SuccessSuffixAddDot(tmp_path):
assert ret_path == target_path assert ret_path == target_path
@pytest.mark.parametrize(
["case", "expect"],
[
("http://test.com/ ", "http://test.com"),
("http://test.com/", "http://test.com"),
("http://test.com ", "http://test.com"),
("http://test.com// ", "http://test.com"),
("http://test.com", "http://test.com"),
(" /http://test.com", "http://test.com"),
(" //http://test.com", "http://test.com"),
("//http://test.com", "http://test.com"),
],
)
def test_strip_url_components(case, expect):
res = io._strip_url_components(case)
assert res == expect
@pytest.mark.parametrize(
["base", "route", "expect"],
[
("http://test.com/ ", "ping", "http://test.com/ping"),
("http://test.com/", "ping ", "http://test.com/ping"),
("http://test.com ", "ping/", "http://test.com/ping"),
("http://test.com// ", "ping", "http://test.com/ping"),
("http://test.com", "/ping ", "http://test.com/ping"),
(" /http://test.com", "/ ping/ ", "http://test.com/ping"),
(" //http://test.com", "ping", "http://test.com/ping"),
("//http://test.com", "// ping// ", "http://test.com/ping"),
],
)
def test_combine_route(base, route, expect):
res = io.combine_route(base, route)
assert res == expect
def test_search_cwd(monkeypatch, base_folder, target_file_pth): def test_search_cwd(monkeypatch, base_folder, target_file_pth):
monkeypatch.setattr(Path, "cwd", lambda: base_folder) monkeypatch.setattr(Path, "cwd", lambda: base_folder)
assert Path.cwd() == base_folder assert Path.cwd() == base_folder