|
| 1 | +"""Shared utilities for storage backends.""" |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from sqlspec.exceptions import MissingDependencyError |
| 6 | +from sqlspec.typing import PYARROW_INSTALLED |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from pathlib import Path |
| 10 | + |
| 11 | +__all__ = ("ensure_pyarrow", "resolve_storage_path") |
| 12 | + |
| 13 | + |
| 14 | +def ensure_pyarrow() -> None: |
| 15 | + """Ensure PyArrow is available for Arrow operations. |
| 16 | +
|
| 17 | + Raises: |
| 18 | + MissingDependencyError: If pyarrow is not installed. |
| 19 | + """ |
| 20 | + if not PYARROW_INSTALLED: |
| 21 | + raise MissingDependencyError(package="pyarrow", install_package="pyarrow") |
| 22 | + |
| 23 | + |
| 24 | +def resolve_storage_path( |
| 25 | + path: "str | Path", base_path: str = "", protocol: str = "file", strip_file_scheme: bool = True |
| 26 | +) -> str: |
| 27 | + """Resolve path relative to base_path with protocol-specific handling. |
| 28 | +
|
| 29 | + Args: |
| 30 | + path: Path to resolve (may include file:// scheme). |
| 31 | + base_path: Base path to prepend if path is relative. |
| 32 | + protocol: Storage protocol (file, s3, gs, etc.). |
| 33 | + strip_file_scheme: Whether to strip file:// prefix. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + Resolved path string suitable for the storage backend. |
| 37 | +
|
| 38 | + Examples: |
| 39 | + >>> resolve_storage_path("/data/file.txt", protocol="file") |
| 40 | + 'data/file.txt' |
| 41 | +
|
| 42 | + >>> resolve_storage_path( |
| 43 | + ... "file.txt", base_path="/base", protocol="file" |
| 44 | + ... ) |
| 45 | + 'base/file.txt' |
| 46 | +
|
| 47 | + >>> resolve_storage_path( |
| 48 | + ... "file:///data/file.txt", strip_file_scheme=True |
| 49 | + ... ) |
| 50 | + 'data/file.txt' |
| 51 | +
|
| 52 | + >>> resolve_storage_path( |
| 53 | + ... "/data/subdir/file.txt", |
| 54 | + ... base_path="/data", |
| 55 | + ... protocol="file", |
| 56 | + ... ) |
| 57 | + 'subdir/file.txt' |
| 58 | + """ |
| 59 | + from pathlib import Path as PathlibPath |
| 60 | + |
| 61 | + path_str = str(path) |
| 62 | + |
| 63 | + if strip_file_scheme and path_str.startswith("file://"): |
| 64 | + path_str = path_str.removeprefix("file://") |
| 65 | + |
| 66 | + # For local file protocol |
| 67 | + if protocol == "file": |
| 68 | + path_obj = PathlibPath(path_str) |
| 69 | + |
| 70 | + # Absolute path handling |
| 71 | + if path_obj.is_absolute(): |
| 72 | + if base_path: |
| 73 | + base_obj = PathlibPath(base_path) |
| 74 | + # Try to make path relative to base_path |
| 75 | + try: |
| 76 | + relative = path_obj.relative_to(base_obj) |
| 77 | + # Return joined path for FSSpec-style backends |
| 78 | + return f"{base_path.rstrip('/')}/{relative}" |
| 79 | + except ValueError: |
| 80 | + # Path is outside base_path |
| 81 | + return path_str.lstrip("/") |
| 82 | + # No base_path - strip leading / |
| 83 | + return path_str.lstrip("/") |
| 84 | + |
| 85 | + # Relative path with base_path - join them |
| 86 | + if base_path: |
| 87 | + return f"{base_path.rstrip('/')}/{path_str}" |
| 88 | + |
| 89 | + # Relative path without base_path |
| 90 | + return path_str |
| 91 | + |
| 92 | + # For cloud storage protocols (s3, gs, etc.), join with base_path |
| 93 | + if not base_path: |
| 94 | + return path_str |
| 95 | + |
| 96 | + clean_base = base_path.rstrip("/") |
| 97 | + clean_path = path_str.lstrip("/") |
| 98 | + return f"{clean_base}/{clean_path}" |
0 commit comments