Skip to content

paths

Utility functions for handling paths.

uri_to_path(uri)

Convert URI to pathlib.Path.

Support both Windows and Posix path types.

Information

urllib.parse.urlparse() leaves an initial slash in front of the drive letter when parsing a file URL for an absolute path on Windows.

Example: urlparse("file:///C:/Windows").path -> "/C:/Windows"

To solve this, the initial forward slash is removed prior to casting to pathlib.Path.

Parameters:

Name Type Description Default
uri Union[str, AnyUrl, ParseResult]

The URI/IRI/URL. Either as a string or a parsed URL.

required

Returns:

Type Description
Path

A properly converted URI/IRI/URL to pathlib.Path.

Source code in oteapi/utils/paths.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def uri_to_path(uri: "Union[str, AnyUrl, ParseResult]") -> Path:
    """Convert URI to pathlib.Path.

    Support both Windows and Posix path types.

    Information:
        `urllib.parse.urlparse()` leaves an initial slash in front of the drive letter
        when parsing a file URL for an absolute path on Windows.

        Example: `urlparse("file:///C:/Windows").path` -> `"/C:/Windows"`

        To solve this, the initial forward slash is removed prior to casting to
        `pathlib.Path`.

    Parameters:
        uri: The URI/IRI/URL. Either as a string or a parsed URL.

    Returns:
        A properly converted URI/IRI/URL to `pathlib.Path`.

    """
    if not isinstance(uri, ParseResult):
        uri = urlparse(str(uri))

    uri_path = (uri.netloc + uri.path) if uri.scheme == "file" else uri.path

    if uri.scheme != "file":
        warnings.warn(
            "A 'file'-scheme was expected for the 'uri' in 'uri_to_path()', instead a "
            f"{uri.scheme!r} was received. Still converting to `pathlib.Path` using "
            "the 'path' of the URI."
        )

    path = Path(uri_path)
    if isinstance(path, PureWindowsPath):
        path = Path(uri_path.lstrip("/"))
    return path