1"""``pyshacl.rdfutil.load``: source loading and ``file:`` URI conversion."""
2
3import os
4from pathlib import Path, PurePath
5from urllib.parse import unquote_to_bytes
6
7from rdflib import URIRef
8
9from pyshacl_turbo.rdf import load_from_source
10
11
12def path_from_uri(uri: str | URIRef, relative_to: PurePath | None = None) -> Path:
13 """Return the path of a ``file:`` URI, with pySHACL's DOS and authority handling."""
14 if not uri.startswith("file:"):
15 raise ValueError(f"File URI does not start with 'file:': {uri!r}")
16 path = str(uri)[5:]
17 if path[:3] == "///":
18 path = path[2:]
19 elif path[:12] == "//localhost/":
20 path = path[11:]
21 if path[:3] == "///" or (path[:1] == "/" and path[2:3] in ":|"):
22 path = path[1:]
23 if path[1:2] == "|":
24 path = path[:1] + ":" + path[2:]
25 result = Path(os.fsdecode(unquote_to_bytes(path)))
26 return result if relative_to is None else Path(relative_to).joinpath(result)
27
28
29__all__ = ["load_from_source", "path_from_uri"]