Coverage for src/mlopus/utils/urls.py: 100%
16 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-07-13 14:49 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-07-13 14:49 +0000
1from pathlib import Path
3import urllib3.util
5Url = urllib3.util.Url
7UrlLike = Url | str | Path
10def parse_url(url: UrlLike, default_scheme: str = "file", resolve_if_local: bool = False) -> Url:
11 """Parse URL with default scheme."""
12 if not isinstance(url, Url):
13 url = urllib3.util.parse_url(str(url))
14 if not url.scheme:
15 url = urllib3.util.parse_url(f"{default_scheme}://{url}")
16 if resolve_if_local and is_local(url):
17 url = parse_url(Path((url.host or "") + (url.path or "")).expanduser().resolve())
18 return url
21def is_local(url: UrlLike) -> bool:
22 """Tell if URL points to local file system."""
23 return parse_url(url).scheme == "file"
26def urljoin(*parts: UrlLike) -> Url:
27 """Join URL parts while trimming slashes."""
28 return parse_url("/".join(trimmed for x in parts if (trimmed := str(x).strip("/ "))))