Skip to content

paths

Class info

🛈 DocStrings

Path handling utilities for URI and filesystem operations.

guess_mime_type

guess_mime_type(path: JoinablePathLike) -> str

Guess MIME type from file path using stdlib.

Parameters:

Name Type Description Default
path JoinablePathLike

Path to get MIME type for

required

Returns:

Type Description
str

MIME type string, defaults to "application/octet-stream" if unknown

Source code in src/llmling/utils/paths.py
def guess_mime_type(path: JoinablePathLike) -> str:
    """Guess MIME type from file path using stdlib.

    Args:
        path: Path to get MIME type for

    Returns:
        MIME type string, defaults to "application/octet-stream" if unknown
    """
    mime_type, _ = mimetypes.guess_type(str(path))
    return mime_type or "application/octet-stream"

is_ignorable_part

is_ignorable_part(part: str) -> bool

Check if a path component should be ignored.

Source code in src/llmling/utils/paths.py
def is_ignorable_part(part: str) -> bool:
    """Check if a path component should be ignored."""
    return (
        not part
        or part in {".", ".."}
        or (len(part) == 2 and part[1] == ":")  # Drive letter  # noqa: PLR2004
        or part in {"/", "\\"}
    )

is_ignorable_path_component

is_ignorable_path_component(part: str) -> bool

Check if a path component should be ignored.

Source code in src/llmling/utils/paths.py
def is_ignorable_path_component(part: str) -> bool:
    """Check if a path component should be ignored."""
    return (
        not part
        or part in {".", ".."}
        or (len(part) == 2 and part[1] == ":")  # Drive letter  # noqa: PLR2004
        or part in {"/", "\\"}
    )

normalize_path_components

normalize_path_components(parts: Sequence[str]) -> list[str]

Normalize path components, resolving . and .. entries.

Source code in src/llmling/utils/paths.py
def normalize_path_components(parts: Sequence[str]) -> list[str]:
    """Normalize path components, resolving . and .. entries."""
    result: list[str] = []
    for part in parts:
        match part:
            case "." | "":
                continue
            case "..":
                if result:
                    result.pop()
            case _:
                if not is_ignorable_path_component(part):
                    result.append(part)
    return result

path_to_uri

path_to_uri(path: str) -> str

Convert a path to a file URI with proper encoding.

Source code in src/llmling/utils/paths.py
def path_to_uri(path: str) -> str:
    """Convert a path to a file URI with proper encoding."""
    # Normalize path separators and split
    parts = path.replace("\\", "/").split("/")

    # Normalize and encode components
    normalized = normalize_path_components(parts)
    if not normalized:
        msg = "Empty path"
        raise ValueError(msg)

    encoded = [urllib.parse.quote(part) for part in normalized]
    return f"file:///{'/'.join(encoded)}"

uri_to_path

uri_to_path(uri: str) -> str

Convert a file URI to a normalized path.

Source code in src/llmling/utils/paths.py
def uri_to_path(uri: str) -> str:
    """Convert a file URI to a normalized path."""
    if not uri.startswith("file:///"):
        msg = f"Invalid file URI format: {uri}"
        raise ValueError(msg)

    # Split into components and decode, including empty parts
    parts = [urllib.parse.unquote(part) for part in uri[8:].split("/")]

    # Normalize components
    return "/".join(normalize_path_components(parts))