mirror of
https://github.com/19JVJeffery/tiddl-gui.git
synced 2026-10-22 02:42:16 -05:00
Agent-Logs-Url: https://github.com/19JVJeffery/tiddl-gui/sessions/ee218a96-15f4-4124-8eea-902561b3cc4e Co-authored-by: 19JVJeffery <94115228+19JVJeffery@users.noreply.github.com>
39 lines
997 B
Python
39 lines
997 B
Python
from pathlib import Path
|
|
|
|
|
|
def resolve_existing_path_case(base_path: Path, relative_path: Path) -> Path:
|
|
"""
|
|
Return base_path / relative_path, reusing existing path component casing.
|
|
"""
|
|
|
|
if relative_path.is_absolute():
|
|
raise ValueError("relative_path must not be absolute")
|
|
|
|
resolved_path = base_path
|
|
|
|
for part in relative_path.parts:
|
|
if part in ("", "."):
|
|
continue
|
|
|
|
existing_part = find_existing_child_case(resolved_path, part)
|
|
resolved_path = resolved_path / (existing_part or part)
|
|
|
|
return resolved_path
|
|
|
|
|
|
def find_existing_child_case(parent: Path, name: str) -> str | None:
|
|
if not parent.is_dir():
|
|
return None
|
|
|
|
casefolded_name = name.casefold()
|
|
fallback: str | None = None
|
|
|
|
for child in parent.iterdir():
|
|
if child.name == name:
|
|
return child.name
|
|
|
|
if fallback is None and child.name.casefold() == casefolded_name:
|
|
fallback = child.name
|
|
|
|
return fallback
|