Skip to content

Commit

Permalink
chore: tryin to fix osx / linux
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Nov 30, 2024
1 parent 5fe800d commit 34e1183
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
49 changes: 43 additions & 6 deletions src/llmling/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from datetime import datetime
import os
import re
from typing import TYPE_CHECKING, Any, ClassVar, TypeVar, cast, overload
import urllib.parse

import logfire
import upath

from llmling.config.models import BaseResource
from llmling.core import exceptions
Expand Down Expand Up @@ -97,6 +101,8 @@ class ResourceLoader[TResource](ABC):
context_class: type[TResource]
uri_scheme: ClassVar[str]
supported_mime_types: ClassVar[list[str]] = ["text/plain"]
# Invalid path characters (Windows + Unix)
invalid_chars_pattern = re.compile(r'[\x00-\x1F<>:"|?*\\]')

def __init__(self, context: LoaderContext[TResource] | None = None) -> None:
"""Initialize loader with optional context."""
Expand Down Expand Up @@ -132,18 +138,49 @@ def get_name_from_uri(cls, uri: str) -> str:
"""
try:
if not cls.supports_uri(uri):
msg = f"Unsupported URI scheme: {uri}"
msg = f"Unsupported URI: {uri}"
raise exceptions.LoaderError(msg) # noqa: TRY301

# Split "scheme://name" and get name part
_, name = uri.split("://", 1)
path = upath.UPath(uri)

# Normalize path separators
return name.replace("\\", "/").lstrip("/")
# Get parts excluding protocol info
parts = [
urllib.parse.unquote(str(part)) # URL decode each part
for part in path.parts
if not cls._is_ignorable_part(str(part))
]

if not parts:
msg = "Empty path after normalization"
raise exceptions.LoaderError(msg) # noqa: TRY301

# Validate path components
for part in parts:
if cls.invalid_chars_pattern.search(part):
msg = f"Invalid characters in path component: {part}"
raise exceptions.LoaderError(msg) # noqa: TRY301

# Join with forward slashes and normalize path
joined = "/".join(parts)
# Normalize path (resolve .. and .)
normalized = os.path.normpath(joined).replace("\\", "/")
except Exception as exc:
msg = f"Invalid URI format: {uri}"
if isinstance(exc, exceptions.LoaderError):
raise
msg = f"Invalid URI: {uri}"
raise exceptions.LoaderError(msg) from exc
else:
return normalized

@staticmethod
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 {"/", "\\"}
)

@classmethod
def create_uri(cls, *, name: str) -> str:
Expand Down
12 changes: 0 additions & 12 deletions src/llmling/resources/loaders/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class PathResourceLoader(ResourceLoader[PathResource]):
"text/markdown",
"text/yaml",
]
# Invalid path characters (Windows + Unix)
invalid_chars_pattern = re.compile(r'[\x00-\x1F<>:"|?*\\]')

@classmethod
def supports_uri(cls, uri: str) -> bool:
Expand Down Expand Up @@ -75,16 +73,6 @@ def get_name_from_uri(cls, uri: str) -> str:
msg = f"Invalid URI: {uri}"
raise exceptions.LoaderError(msg) from exc

@staticmethod
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 {"/", "\\"}
)

@classmethod
def create_uri(cls, *, name: str) -> str:
"""Create a URI from a path or URL."""
Expand Down

0 comments on commit 34e1183

Please sign in to comment.