|
8 | 8 | from collections.abc import Sequence |
9 | 9 | from typing import TYPE_CHECKING |
10 | 10 | from typing import Any |
| 11 | +from typing import Callable |
| 12 | +from typing import Literal |
| 13 | +from typing import overload |
11 | 14 | from urllib.parse import SplitResult |
12 | 15 |
|
13 | 16 | from fsspec import AbstractFileSystem |
|
19 | 22 | from upath._protocol import compatible_protocol |
20 | 23 | from upath.core import UPath |
21 | 24 | from upath.core import _UPathMixin |
| 25 | +from upath.types import UNSET_DEFAULT |
22 | 26 | from upath.types import JoinablePathLike |
| 27 | +from upath.types import PathInfo |
| 28 | +from upath.types import ReadablePath |
| 29 | +from upath.types import ReadablePathLike |
| 30 | +from upath.types import SupportsPathLike |
| 31 | +from upath.types import WritablePath |
23 | 32 |
|
24 | 33 | if TYPE_CHECKING: |
| 34 | + from typing import IO |
| 35 | + from typing import BinaryIO |
| 36 | + from typing import TextIO |
| 37 | + from typing import TypeVar |
| 38 | + |
25 | 39 | if sys.version_info >= (3, 11): |
26 | 40 | from typing import Self |
27 | 41 | else: |
28 | 42 | from typing_extensions import Self |
29 | 43 |
|
| 44 | + _WT = TypeVar("_WT", bound="WritablePath") |
| 45 | + |
30 | 46 | __all__ = [ |
31 | 47 | "LocalPath", |
32 | 48 | "PosixUPath", |
@@ -66,6 +82,27 @@ def _warn_protocol_storage_options( |
66 | 82 | ) |
67 | 83 |
|
68 | 84 |
|
| 85 | +class _LocalPathInfo(PathInfo): |
| 86 | + """Backported PathInfo implementation for LocalPath. |
| 87 | + todo: currently not handling symlinks correctly. |
| 88 | + """ |
| 89 | + |
| 90 | + def __init__(self, path: LocalPath) -> None: |
| 91 | + self._path = path.path |
| 92 | + |
| 93 | + def exists(self, *, follow_symlinks: bool = True) -> bool: |
| 94 | + return os.path.exists(self._path) |
| 95 | + |
| 96 | + def is_dir(self, *, follow_symlinks: bool = True) -> bool: |
| 97 | + return os.path.isdir(self._path) |
| 98 | + |
| 99 | + def is_file(self, *, follow_symlinks: bool = True) -> bool: |
| 100 | + return os.path.isfile(self._path) |
| 101 | + |
| 102 | + def is_symlink(self) -> bool: |
| 103 | + return os.path.islink(self._path) |
| 104 | + |
| 105 | + |
69 | 106 | class LocalPath(_UPathMixin, pathlib.Path): |
70 | 107 | __slots__ = ( |
71 | 108 | "_chain", |
@@ -147,6 +184,27 @@ def _init(self, **kwargs: Any) -> None: |
147 | 184 | super()._init(**kwargs) # type: ignore[misc] |
148 | 185 | self._chain = Chain(ChainSegment(str(self), "", {})) |
149 | 186 |
|
| 187 | + def __vfspath__(self) -> str: |
| 188 | + return self.__fspath__() |
| 189 | + |
| 190 | + def __open_reader__(self) -> BinaryIO: |
| 191 | + return self.open("rb") |
| 192 | + |
| 193 | + if sys.version_info >= (3, 14): |
| 194 | + |
| 195 | + def __open_rb__(self, buffering: int = UNSET_DEFAULT) -> BinaryIO: |
| 196 | + return self.open("rb", buffering=buffering) |
| 197 | + |
| 198 | + def __open_writer__(self, mode: Literal["a", "w", "x"]) -> BinaryIO: |
| 199 | + if mode == "w": |
| 200 | + return self.open(mode="wb") |
| 201 | + elif mode == "a": |
| 202 | + return self.open(mode="ab") |
| 203 | + elif mode == "x": |
| 204 | + return self.open(mode="xb") |
| 205 | + else: |
| 206 | + raise ValueError(f"invalid mode: {mode}") |
| 207 | + |
150 | 208 | def with_segments(self, *pathsegments: str | os.PathLike[str]) -> Self: |
151 | 209 | return type(self)( |
152 | 210 | *pathsegments, |
@@ -190,6 +248,149 @@ def __rtruediv__(self, other) -> Self: |
190 | 248 | else other |
191 | 249 | ) |
192 | 250 |
|
| 251 | + @overload # type: ignore[override] |
| 252 | + def open( |
| 253 | + self, |
| 254 | + mode: Literal["r", "w", "a"] = "r", |
| 255 | + buffering: int = ..., |
| 256 | + encoding: str = ..., |
| 257 | + errors: str = ..., |
| 258 | + newline: str = ..., |
| 259 | + **fsspec_kwargs: Any, |
| 260 | + ) -> TextIO: ... |
| 261 | + |
| 262 | + @overload |
| 263 | + def open( |
| 264 | + self, |
| 265 | + mode: Literal["rb", "wb", "ab", "xb"], |
| 266 | + buffering: int = ..., |
| 267 | + encoding: str = ..., |
| 268 | + errors: str = ..., |
| 269 | + newline: str = ..., |
| 270 | + **fsspec_kwargs: Any, |
| 271 | + ) -> BinaryIO: ... |
| 272 | + |
| 273 | + @overload |
| 274 | + def open( |
| 275 | + self, |
| 276 | + mode: str, |
| 277 | + buffering: int = ..., |
| 278 | + encoding: str | None = ..., |
| 279 | + errors: str | None = ..., |
| 280 | + newline: str | None = ..., |
| 281 | + **fsspec_kwargs: Any, |
| 282 | + ) -> IO[Any]: ... |
| 283 | + |
| 284 | + def open( |
| 285 | + self, |
| 286 | + mode: str = "r", |
| 287 | + buffering: int = UNSET_DEFAULT, |
| 288 | + encoding: str | None = UNSET_DEFAULT, |
| 289 | + errors: str | None = UNSET_DEFAULT, |
| 290 | + newline: str | None = UNSET_DEFAULT, |
| 291 | + **fsspec_kwargs: Any, |
| 292 | + ) -> IO[Any]: |
| 293 | + if not fsspec_kwargs: |
| 294 | + kwargs: dict[str, str | int | None] = {} |
| 295 | + if buffering is not UNSET_DEFAULT: |
| 296 | + kwargs["buffering"] = buffering |
| 297 | + if encoding is not UNSET_DEFAULT: |
| 298 | + kwargs["encoding"] = encoding |
| 299 | + if errors is not UNSET_DEFAULT: |
| 300 | + kwargs["errors"] = errors |
| 301 | + if newline is not UNSET_DEFAULT: |
| 302 | + kwargs["newline"] = newline |
| 303 | + return super().open(mode, **kwargs) # type: ignore # noqa: E501 |
| 304 | + return UPath.open.__get__(self)( |
| 305 | + mode, |
| 306 | + buffering=buffering, |
| 307 | + encoding=encoding, |
| 308 | + errors=errors, |
| 309 | + newline=newline, |
| 310 | + **fsspec_kwargs, |
| 311 | + ) |
| 312 | + |
| 313 | + if sys.version_info < (3, 14): |
| 314 | + |
| 315 | + @overload |
| 316 | + def copy(self, target: _WT, **kwargs: Any) -> _WT: ... |
| 317 | + |
| 318 | + @overload |
| 319 | + def copy(self, target: SupportsPathLike | str, **kwargs: Any) -> Self: ... |
| 320 | + |
| 321 | + def copy( |
| 322 | + self, target: _WT | SupportsPathLike | str, **kwargs: Any |
| 323 | + ) -> _WT | Self: |
| 324 | + # hacky workaround for missing pathlib.Path.copy in python < 3.14 |
| 325 | + # todo: revisit |
| 326 | + _copy: Any = ReadablePath.copy.__get__(self) |
| 327 | + if not isinstance(target, UPath): |
| 328 | + return _copy(self.with_segments(str(target)), **kwargs) |
| 329 | + else: |
| 330 | + return _copy(target, **kwargs) |
| 331 | + |
| 332 | + @overload |
| 333 | + def copy_into(self, target_dir: _WT, **kwargs: Any) -> _WT: ... |
| 334 | + |
| 335 | + @overload |
| 336 | + def copy_into( |
| 337 | + self, target_dir: SupportsPathLike | str, **kwargs: Any |
| 338 | + ) -> Self: ... |
| 339 | + |
| 340 | + def copy_into( |
| 341 | + self, |
| 342 | + target_dir: _WT | SupportsPathLike | str, |
| 343 | + **kwargs: Any, |
| 344 | + ) -> _WT | Self: |
| 345 | + # hacky workaround for missing pathlib.Path.copy_into in python < 3.14 |
| 346 | + # todo: revisit |
| 347 | + _copy_into: Any = ReadablePath.copy_into.__get__(self) |
| 348 | + if not isinstance(target_dir, UPath): |
| 349 | + return _copy_into(self.with_segments(str(target_dir)), **kwargs) |
| 350 | + else: |
| 351 | + return _copy_into(target_dir, **kwargs) |
| 352 | + |
| 353 | + @property |
| 354 | + def info(self) -> PathInfo: |
| 355 | + return _LocalPathInfo(self) |
| 356 | + |
| 357 | + if sys.version_info < (3, 13): |
| 358 | + |
| 359 | + def full_match(self, pattern: str) -> bool: |
| 360 | + # hacky workaround for missing pathlib.Path.full_match in python < 3.13 |
| 361 | + # todo: revisit |
| 362 | + return self.match(pattern) |
| 363 | + |
| 364 | + if sys.version_info < (3, 12): |
| 365 | + |
| 366 | + def is_junction(self) -> bool: |
| 367 | + return False |
| 368 | + |
| 369 | + def walk( |
| 370 | + self, |
| 371 | + top_down: bool = True, |
| 372 | + on_error: Callable[[Exception], Any] | None = None, |
| 373 | + follow_symlinks: bool = False, |
| 374 | + ) -> Iterator[tuple[Self, list[str], list[str]]]: |
| 375 | + _walk = ReadablePath.walk.__get__(self) |
| 376 | + return _walk(top_down, on_error, follow_symlinks) |
| 377 | + |
| 378 | + if sys.version_info < (3, 10): |
| 379 | + |
| 380 | + def hardlink_to(self, target: ReadablePathLike) -> None: |
| 381 | + try: |
| 382 | + os.link(target, self) |
| 383 | + except AttributeError: |
| 384 | + raise NotImplementedError |
| 385 | + |
| 386 | + if not hasattr(pathlib.Path, "_copy_from"): |
| 387 | + |
| 388 | + def _copy_from( |
| 389 | + self, source: ReadablePath | LocalPath, follow_symlinks: bool = True |
| 390 | + ) -> None: |
| 391 | + _copy_from: Any = WritablePath._copy_from.__get__(self) |
| 392 | + _copy_from(source, follow_symlinks=follow_symlinks) |
| 393 | + |
193 | 394 |
|
194 | 395 | UPath.register(LocalPath) |
195 | 396 |
|
|
0 commit comments