panpath 0.3.0__tar.gz → 0.4.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {panpath-0.3.0 → panpath-0.4.1}/PKG-INFO +2 -2
- {panpath-0.3.0 → panpath-0.4.1}/README.md +1 -1
- {panpath-0.3.0 → panpath-0.4.1}/panpath/__init__.py +1 -1
- {panpath-0.3.0 → panpath-0.4.1}/panpath/azure_async_client.py +22 -30
- {panpath-0.3.0 → panpath-0.4.1}/panpath/azure_client.py +15 -20
- {panpath-0.3.0 → panpath-0.4.1}/panpath/base.py +37 -19
- {panpath-0.3.0 → panpath-0.4.1}/panpath/clients.py +28 -6
- {panpath-0.3.0 → panpath-0.4.1}/panpath/cloud.py +17 -14
- {panpath-0.3.0 → panpath-0.4.1}/panpath/gs_async_client.py +18 -19
- {panpath-0.3.0 → panpath-0.4.1}/panpath/gs_client.py +13 -13
- {panpath-0.3.0 → panpath-0.4.1}/panpath/local_path.py +213 -31
- {panpath-0.3.0 → panpath-0.4.1}/panpath/s3_async_client.py +17 -15
- {panpath-0.3.0 → panpath-0.4.1}/panpath/s3_client.py +11 -17
- {panpath-0.3.0 → panpath-0.4.1}/pyproject.toml +1 -1
- {panpath-0.3.0 → panpath-0.4.1}/LICENSE +0 -0
- {panpath-0.3.0 → panpath-0.4.1}/panpath/azure_path.py +0 -0
- {panpath-0.3.0 → panpath-0.4.1}/panpath/exceptions.py +0 -0
- {panpath-0.3.0 → panpath-0.4.1}/panpath/gs_path.py +0 -0
- {panpath-0.3.0 → panpath-0.4.1}/panpath/py.typed +0 -0
- {panpath-0.3.0 → panpath-0.4.1}/panpath/registry.py +0 -0
- {panpath-0.3.0 → panpath-0.4.1}/panpath/s3_path.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: panpath
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary: Universal sync/async local/cloud path library with pathlib-compatible interface
|
|
5
5
|
License: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -117,7 +117,7 @@ content = await gs_path.a_read_text()
|
|
|
117
117
|
# Async local files
|
|
118
118
|
local_path = PanPath("/path/to/file.txt")
|
|
119
119
|
async with local_path.a_open("r") as f:
|
|
120
|
-
content = await f.
|
|
120
|
+
content = await f.read()
|
|
121
121
|
```
|
|
122
122
|
|
|
123
123
|
### Path Operations
|
|
@@ -11,6 +11,7 @@ from panpath.clients import AsyncClient, AsyncFileHandle
|
|
|
11
11
|
from panpath.exceptions import MissingDependencyError, NoStatError
|
|
12
12
|
|
|
13
13
|
if TYPE_CHECKING:
|
|
14
|
+
from panpath.base import PanPath
|
|
14
15
|
from azure.storage.blob.aio import BlobServiceClient # type: ignore[import-not-found]
|
|
15
16
|
from azure.core.exceptions import ResourceNotFoundError # type: ignore[import-not-found]
|
|
16
17
|
|
|
@@ -402,15 +403,20 @@ class AsyncAzureBlobClient(AsyncClient):
|
|
|
402
403
|
# Set symlink metadata
|
|
403
404
|
await blob_client.set_blob_metadata({self.__class__.symlink_target_metaname: target})
|
|
404
405
|
|
|
405
|
-
async def glob(
|
|
406
|
+
async def glob(
|
|
407
|
+
self,
|
|
408
|
+
path: str,
|
|
409
|
+
pattern: str,
|
|
410
|
+
_return_panpath: bool = True,
|
|
411
|
+
) -> AsyncGenerator[Union[str, "PanPath"], None]:
|
|
406
412
|
"""Glob for files matching pattern.
|
|
407
413
|
|
|
408
414
|
Args:
|
|
409
415
|
path: Base Azure path
|
|
410
416
|
pattern: Glob pattern (e.g., "*.txt", "**/*.py")
|
|
411
417
|
|
|
412
|
-
|
|
413
|
-
|
|
418
|
+
Yields:
|
|
419
|
+
Matching paths as strings or PanPath objects
|
|
414
420
|
"""
|
|
415
421
|
from fnmatch import fnmatch
|
|
416
422
|
from panpath.base import PanPath
|
|
@@ -421,11 +427,6 @@ class AsyncAzureBlobClient(AsyncClient):
|
|
|
421
427
|
|
|
422
428
|
# Handle recursive patterns
|
|
423
429
|
if "**" in pattern:
|
|
424
|
-
# Recursive search - list all blobs under prefix
|
|
425
|
-
blobs = []
|
|
426
|
-
async for blob in container_client.list_blobs(name_starts_with=blob_prefix):
|
|
427
|
-
blobs.append(blob)
|
|
428
|
-
|
|
429
430
|
# Extract the pattern part after **
|
|
430
431
|
pattern_parts = pattern.split("**/")
|
|
431
432
|
if len(pattern_parts) > 1:
|
|
@@ -433,49 +434,38 @@ class AsyncAzureBlobClient(AsyncClient):
|
|
|
433
434
|
else:
|
|
434
435
|
file_pattern = "*"
|
|
435
436
|
|
|
436
|
-
|
|
437
|
-
for blob in blobs:
|
|
437
|
+
async for blob in container_client.list_blobs(name_starts_with=blob_prefix):
|
|
438
438
|
if fnmatch(blob.name, f"*{file_pattern}"):
|
|
439
439
|
# Determine scheme from original path
|
|
440
440
|
scheme = "az" if path.startswith(f"{self.prefix[0]}://") else "azure"
|
|
441
441
|
if not _return_panpath:
|
|
442
|
-
|
|
442
|
+
yield f"{scheme}://{container_name}/{blob.name}"
|
|
443
443
|
else:
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
f"{scheme}://{container_name}/{blob.name}"
|
|
447
|
-
) # type: ignore[arg-type]
|
|
448
|
-
)
|
|
449
|
-
return results
|
|
444
|
+
yield PanPath(f"{scheme}://{container_name}/{blob.name}")
|
|
445
|
+
|
|
450
446
|
else:
|
|
451
447
|
# Non-recursive - list blobs with prefix
|
|
452
448
|
prefix_with_slash = (
|
|
453
449
|
f"{blob_prefix}/" if blob_prefix and not blob_prefix.endswith("/") else blob_prefix
|
|
454
450
|
)
|
|
455
|
-
blobs = []
|
|
456
|
-
async for blob in container_client.list_blobs(name_starts_with=prefix_with_slash):
|
|
457
|
-
blobs.append(blob)
|
|
458
451
|
|
|
459
|
-
|
|
460
|
-
for blob in blobs:
|
|
452
|
+
async for blob in container_client.list_blobs(name_starts_with=prefix_with_slash):
|
|
461
453
|
# Only include direct children (no additional slashes)
|
|
462
454
|
rel_name = blob.name[len(prefix_with_slash) :]
|
|
463
455
|
if "/" not in rel_name and fnmatch(blob.name, f"{prefix_with_slash}{pattern}"):
|
|
464
456
|
scheme = "az" if path.startswith(f"{self.prefix[0]}://") else "azure"
|
|
465
457
|
if not _return_panpath:
|
|
466
|
-
|
|
458
|
+
yield f"{scheme}://{container_name}/{blob.name}"
|
|
467
459
|
else:
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
f"{scheme}://{container_name}/{blob.name}"
|
|
471
|
-
) # type: ignore[arg-type]
|
|
460
|
+
yield PanPath(
|
|
461
|
+
f"{scheme}://{container_name}/{blob.name}"
|
|
472
462
|
)
|
|
473
|
-
return results
|
|
474
463
|
|
|
475
464
|
async def walk( # type: ignore[override]
|
|
476
465
|
self,
|
|
477
466
|
path: str,
|
|
478
|
-
|
|
467
|
+
_return_panpath: bool = True,
|
|
468
|
+
) -> AsyncGenerator[tuple[Union[str, "PanPath"], list[str], list[str]], None]:
|
|
479
469
|
"""Walk directory tree.
|
|
480
470
|
|
|
481
471
|
Args:
|
|
@@ -484,6 +474,8 @@ class AsyncAzureBlobClient(AsyncClient):
|
|
|
484
474
|
Yields:
|
|
485
475
|
Tuples of (dirpath, dirnames, filenames)
|
|
486
476
|
"""
|
|
477
|
+
from panpath.base import PanPath
|
|
478
|
+
|
|
487
479
|
client = await self._get_client()
|
|
488
480
|
container_name, blob_prefix = self.__class__._parse_path(path)
|
|
489
481
|
container_client = client.get_container_client(container_name)
|
|
@@ -536,7 +528,7 @@ class AsyncAzureBlobClient(AsyncClient):
|
|
|
536
528
|
|
|
537
529
|
# Yield each directory tuple
|
|
538
530
|
for d, (subdirs, files) in sorted(dirs.items()):
|
|
539
|
-
yield (d, sorted(subdirs), sorted(files))
|
|
531
|
+
yield (PanPath(d) if _return_panpath else d, sorted(subdirs), sorted(files))
|
|
540
532
|
|
|
541
533
|
async def touch( # type: ignore[no-untyped-def, override]
|
|
542
534
|
self,
|
|
@@ -7,6 +7,7 @@ from panpath.clients import SyncClient, SyncFileHandle
|
|
|
7
7
|
from panpath.exceptions import MissingDependencyError, NoStatError
|
|
8
8
|
|
|
9
9
|
if TYPE_CHECKING:
|
|
10
|
+
from panpath.base import PanPath
|
|
10
11
|
from azure.storage.blob import BlobServiceClient # type: ignore[import-not-found]
|
|
11
12
|
from azure.core.exceptions import ResourceNotFoundError # type: ignore[import-not-found]
|
|
12
13
|
|
|
@@ -284,7 +285,7 @@ class AzureBlobClient(SyncClient):
|
|
|
284
285
|
path: str,
|
|
285
286
|
pattern: str,
|
|
286
287
|
_return_panpath: bool = True,
|
|
287
|
-
) ->
|
|
288
|
+
) -> Iterator[Union[str, "PanPath"]]:
|
|
288
289
|
"""Glob for files matching pattern.
|
|
289
290
|
|
|
290
291
|
Args:
|
|
@@ -312,20 +313,14 @@ class AzureBlobClient(SyncClient):
|
|
|
312
313
|
else:
|
|
313
314
|
file_pattern = "*"
|
|
314
315
|
|
|
315
|
-
results = []
|
|
316
316
|
for blob in blobs:
|
|
317
317
|
if fnmatch(blob.name, f"*{file_pattern}"):
|
|
318
318
|
# Determine scheme from original path
|
|
319
319
|
scheme = "az" if path.startswith(f"{self.prefix[0]}://") else "azure"
|
|
320
320
|
if not _return_panpath:
|
|
321
|
-
|
|
321
|
+
yield f"{scheme}://{container_name}/{blob.name}"
|
|
322
322
|
else:
|
|
323
|
-
|
|
324
|
-
PanPath(
|
|
325
|
-
f"{scheme}://{container_name}/{blob.name}"
|
|
326
|
-
) # type: ignore[arg-type]
|
|
327
|
-
)
|
|
328
|
-
return results
|
|
323
|
+
yield PanPath(f"{scheme}://{container_name}/{blob.name}")
|
|
329
324
|
else:
|
|
330
325
|
# Non-recursive - list blobs with prefix
|
|
331
326
|
prefix_with_slash = (
|
|
@@ -333,23 +328,21 @@ class AzureBlobClient(SyncClient):
|
|
|
333
328
|
)
|
|
334
329
|
blobs = container_client.list_blobs(name_starts_with=prefix_with_slash)
|
|
335
330
|
|
|
336
|
-
results = []
|
|
337
331
|
for blob in blobs:
|
|
338
332
|
# Only include direct children (no additional slashes)
|
|
339
333
|
rel_name = blob.name[len(prefix_with_slash) :]
|
|
340
334
|
if "/" not in rel_name and fnmatch(blob.name, f"{prefix_with_slash}{pattern}"):
|
|
341
335
|
scheme = "az" if path.startswith(f"{self.prefix[0]}://") else "azure"
|
|
342
336
|
if not _return_panpath:
|
|
343
|
-
|
|
337
|
+
yield f"{scheme}://{container_name}/{blob.name}"
|
|
344
338
|
else:
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
def walk(self, path: str) -> Iterator[tuple[str, list[str], list[str]]]:
|
|
339
|
+
yield PanPath(f"{scheme}://{container_name}/{blob.name}")
|
|
340
|
+
|
|
341
|
+
def walk(
|
|
342
|
+
self,
|
|
343
|
+
path: str,
|
|
344
|
+
_return_panpath: bool = True,
|
|
345
|
+
) -> Iterator[tuple[Union[str, "PanPath"], list[str], list[str]]]:
|
|
353
346
|
"""Walk directory tree.
|
|
354
347
|
|
|
355
348
|
Args:
|
|
@@ -358,6 +351,8 @@ class AzureBlobClient(SyncClient):
|
|
|
358
351
|
Yields:
|
|
359
352
|
Tuples of (dirpath, dirnames, filenames)
|
|
360
353
|
"""
|
|
354
|
+
from panpath.base import PanPath
|
|
355
|
+
|
|
361
356
|
container_name, blob_prefix = self.__class__._parse_path(path)
|
|
362
357
|
container_client = self._client.get_container_client(container_name)
|
|
363
358
|
|
|
@@ -409,7 +404,7 @@ class AzureBlobClient(SyncClient):
|
|
|
409
404
|
|
|
410
405
|
# Yield each directory tuple
|
|
411
406
|
for d, (subdirs, files) in sorted(dirs.items()):
|
|
412
|
-
yield (d, sorted(subdirs), sorted(files))
|
|
407
|
+
yield (PanPath(d) if _return_panpath else d, sorted(subdirs), sorted(files))
|
|
413
408
|
|
|
414
409
|
def touch( # type: ignore[no-untyped-def, override]
|
|
415
410
|
self,
|
|
@@ -111,9 +111,7 @@ class PanPath(PathlibPath):
|
|
|
111
111
|
# In Python 3.10, __init__ doesn't accept arguments
|
|
112
112
|
# In Python 3.12+, __init__ needs the arguments
|
|
113
113
|
if sys.version_info >= (3, 12):
|
|
114
|
-
LocalPath.__init__( # type: ignore[no-untyped-call]
|
|
115
|
-
instance, *new_args, **kwargs
|
|
116
|
-
)
|
|
114
|
+
LocalPath.__init__(instance, *new_args, **kwargs) # type: ignore[no-untyped-call]
|
|
117
115
|
else: # pragma: no cover
|
|
118
116
|
LocalPath.__init__(instance) # type: ignore[no-untyped-call]
|
|
119
117
|
return instance
|
|
@@ -125,6 +123,26 @@ class PanPath(PathlibPath):
|
|
|
125
123
|
except KeyError:
|
|
126
124
|
raise ValueError(f"Unsupported URI scheme: {scheme!r}")
|
|
127
125
|
|
|
126
|
+
@abstractmethod
|
|
127
|
+
async def a_resolve(self) -> "PanPath":
|
|
128
|
+
"""Resolve to absolute path (no-op for cloud paths).
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
Self (cloud paths are already absolute)
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
@abstractmethod
|
|
135
|
+
async def a_replace(self, target: Union[str, "PathlibPath"]) -> "PanPath":
|
|
136
|
+
"""Replace file at target (overwriting if exists).
|
|
137
|
+
|
|
138
|
+
Args:
|
|
139
|
+
target: Target path
|
|
140
|
+
|
|
141
|
+
Returns:
|
|
142
|
+
New path instance
|
|
143
|
+
"""
|
|
144
|
+
# For cloud storage, replace is same as rename (always overwrites)
|
|
145
|
+
|
|
128
146
|
@abstractmethod
|
|
129
147
|
async def a_exists(self) -> bool:
|
|
130
148
|
"""Asynchronously check if the path exists.
|
|
@@ -176,15 +194,11 @@ class PanPath(PathlibPath):
|
|
|
176
194
|
"""
|
|
177
195
|
|
|
178
196
|
@abstractmethod
|
|
179
|
-
async def a_unlink(self) -> None:
|
|
180
|
-
"""Asynchronously remove (delete) the file or empty directory.
|
|
197
|
+
async def a_unlink(self, missing_ok: bool = False) -> None:
|
|
198
|
+
"""Asynchronously remove (delete) the file or empty directory.
|
|
181
199
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
"""Asynchronously resolve the path to its absolute form.
|
|
185
|
-
|
|
186
|
-
Returns:
|
|
187
|
-
Resolved absolute path.
|
|
200
|
+
Args:
|
|
201
|
+
missing_ok: If True, does not raise an error if the file does not exist.
|
|
188
202
|
"""
|
|
189
203
|
|
|
190
204
|
@abstractmethod
|
|
@@ -268,7 +282,7 @@ class PanPath(PathlibPath):
|
|
|
268
282
|
"""
|
|
269
283
|
|
|
270
284
|
@abstractmethod
|
|
271
|
-
async def a_rename(self, target: Union[str, "
|
|
285
|
+
async def a_rename(self, target: Union[str, "PathlibPath"]) -> "PanPath":
|
|
272
286
|
"""Asynchronously rename this path to the target path.
|
|
273
287
|
|
|
274
288
|
Args:
|
|
@@ -279,7 +293,7 @@ class PanPath(PathlibPath):
|
|
|
279
293
|
"""
|
|
280
294
|
|
|
281
295
|
@abstractmethod
|
|
282
|
-
async def a_replace(self, target: Union[str, "
|
|
296
|
+
async def a_replace(self, target: Union[str, "PathlibPath"]) -> "PanPath":
|
|
283
297
|
"""Asynchronously replace this path with the target path.
|
|
284
298
|
|
|
285
299
|
Args:
|
|
@@ -318,7 +332,11 @@ class PanPath(PathlibPath):
|
|
|
318
332
|
"""
|
|
319
333
|
|
|
320
334
|
@abstractmethod
|
|
321
|
-
async def a_symlink_to(
|
|
335
|
+
async def a_symlink_to(
|
|
336
|
+
self,
|
|
337
|
+
target: Union[str, "PathlibPath"],
|
|
338
|
+
target_is_directory: bool = False,
|
|
339
|
+
) -> None:
|
|
322
340
|
"""Asynchronously create a symbolic link pointing to the target path.
|
|
323
341
|
|
|
324
342
|
For local path, this creates a real symlink.
|
|
@@ -326,6 +344,7 @@ class PanPath(PathlibPath):
|
|
|
326
344
|
|
|
327
345
|
Args:
|
|
328
346
|
target: The target PanPath the symlink points to.
|
|
347
|
+
target_is_directory: Whether the target is a directory (ignored for cloud paths).
|
|
329
348
|
"""
|
|
330
349
|
|
|
331
350
|
@abstractmethod
|
|
@@ -338,7 +357,7 @@ class PanPath(PathlibPath):
|
|
|
338
357
|
"""
|
|
339
358
|
|
|
340
359
|
@abstractmethod
|
|
341
|
-
async def a_copy(self, target: Union[str, "
|
|
360
|
+
async def a_copy(self, target: Union[str, "PathlibPath"]) -> "PanPath":
|
|
342
361
|
"""Asynchronously copy this path to the target path.
|
|
343
362
|
|
|
344
363
|
Args:
|
|
@@ -351,15 +370,14 @@ class PanPath(PathlibPath):
|
|
|
351
370
|
@abstractmethod
|
|
352
371
|
async def a_copytree(
|
|
353
372
|
self,
|
|
354
|
-
target: Union[str, "
|
|
355
|
-
|
|
373
|
+
target: Union[str, "PathlibPath"],
|
|
374
|
+
follow_symlinks: bool = True,
|
|
356
375
|
) -> "PanPath":
|
|
357
376
|
"""Asynchronously copy the directory and all its contents recursively to the target path.
|
|
358
377
|
|
|
359
378
|
Args:
|
|
360
379
|
target: Destination PanPath to copy to.
|
|
361
|
-
|
|
362
|
-
and returns a list of names to ignore.
|
|
380
|
+
follow_symlinks: If True, copies the contents of symlinks.
|
|
363
381
|
|
|
364
382
|
Returns:
|
|
365
383
|
The copied PanPath instance.
|
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
"""Base client classes for sync and async cloud storage operations."""
|
|
2
2
|
|
|
3
3
|
from abc import ABC, abstractmethod
|
|
4
|
-
from typing import
|
|
4
|
+
from typing import (
|
|
5
|
+
TYPE_CHECKING,
|
|
6
|
+
Any,
|
|
7
|
+
AsyncGenerator,
|
|
8
|
+
Callable,
|
|
9
|
+
Iterator,
|
|
10
|
+
List,
|
|
11
|
+
Optional,
|
|
12
|
+
Tuple,
|
|
13
|
+
Union,
|
|
14
|
+
Awaitable,
|
|
15
|
+
)
|
|
5
16
|
|
|
6
17
|
import re
|
|
7
18
|
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from panpath.base import PanPath
|
|
21
|
+
|
|
8
22
|
|
|
9
23
|
class Client(ABC):
|
|
10
24
|
"""Base class for cloud storage clients."""
|
|
@@ -98,7 +112,11 @@ class SyncClient(Client, ABC):
|
|
|
98
112
|
"""Find all paths matching pattern."""
|
|
99
113
|
|
|
100
114
|
@abstractmethod
|
|
101
|
-
def walk(
|
|
115
|
+
def walk(
|
|
116
|
+
self,
|
|
117
|
+
path: str,
|
|
118
|
+
_return_panpath: True,
|
|
119
|
+
) -> Iterator[tuple[Union[str, "PanPath"], list[str], list[str]]]:
|
|
102
120
|
"""Walk directory tree."""
|
|
103
121
|
|
|
104
122
|
@abstractmethod
|
|
@@ -232,11 +250,15 @@ class AsyncClient(Client, ABC):
|
|
|
232
250
|
"""Create a directory marker (empty blob with trailing slash)."""
|
|
233
251
|
|
|
234
252
|
@abstractmethod
|
|
235
|
-
async def glob(self, path: str, pattern: str) ->
|
|
253
|
+
async def glob(self, path: str, pattern: str) -> AsyncGenerator[str, None]:
|
|
236
254
|
"""Find all paths matching pattern."""
|
|
237
255
|
|
|
238
256
|
@abstractmethod
|
|
239
|
-
async def walk(
|
|
257
|
+
async def walk(
|
|
258
|
+
self,
|
|
259
|
+
path: str,
|
|
260
|
+
_return_panpath: bool = True,
|
|
261
|
+
) -> AsyncGenerator[tuple[Union[str, "PanPath"], list[str], list[str]], None]:
|
|
240
262
|
"""Walk directory tree."""
|
|
241
263
|
|
|
242
264
|
@abstractmethod
|
|
@@ -827,8 +849,8 @@ class SyncFileHandle(ABC):
|
|
|
827
849
|
if size == -1:
|
|
828
850
|
# Check if this is a boto3/botocore stream (wraps HTTPResponse)
|
|
829
851
|
# These don't accept -1 in Python 3.9
|
|
830
|
-
stream_module = getattr(self._stream.__class__,
|
|
831
|
-
if
|
|
852
|
+
stream_module = getattr(self._stream.__class__, "__module__", "")
|
|
853
|
+
if "botocore" in stream_module or "urllib3" in stream_module:
|
|
832
854
|
size = None # type: ignore
|
|
833
855
|
|
|
834
856
|
chunk = self._stream.read(size)
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"""Base classes for cloud path implementations."""
|
|
2
2
|
|
|
3
|
+
import sys
|
|
4
|
+
|
|
3
5
|
from abc import ABC, abstractmethod
|
|
4
6
|
from pathlib import PurePosixPath
|
|
5
7
|
from typing import (
|
|
@@ -51,8 +53,6 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
51
53
|
# Python version compatibility for PurePosixPath.__init__():
|
|
52
54
|
# - Python 3.9-3.11: Fully initialized in __new__()
|
|
53
55
|
# - Python 3.12+: Needs __init__(*args) to set _raw_paths, _drv, etc.
|
|
54
|
-
import sys
|
|
55
|
-
|
|
56
56
|
if sys.version_info >= (3, 12):
|
|
57
57
|
# Python 3.12+ requires calling __init__ with args to set internal properties
|
|
58
58
|
PurePosixPath.__init__(self, *args) # type: ignore
|
|
@@ -537,14 +537,6 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
537
537
|
if not missing_ok:
|
|
538
538
|
raise
|
|
539
539
|
|
|
540
|
-
async def a_resolve(self) -> "CloudPath":
|
|
541
|
-
"""Resolve to absolute path (no-op for cloud paths).
|
|
542
|
-
|
|
543
|
-
Returns:
|
|
544
|
-
Self (cloud paths are already absolute)
|
|
545
|
-
"""
|
|
546
|
-
return await self.a_readlink() if await self.a_is_symlink() else self
|
|
547
|
-
|
|
548
540
|
async def a_iterdir(self) -> AsyncGenerator["CloudPath", None]:
|
|
549
541
|
"""List directory contents (async version returns list)."""
|
|
550
542
|
for item in await self.async_client.list_dir(str(self)):
|
|
@@ -641,9 +633,7 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
641
633
|
f"Cannot rename directory {self} to non-directory target {target}"
|
|
642
634
|
)
|
|
643
635
|
if not source_is_dir and target_is_dir and target_exists:
|
|
644
|
-
raise IsADirectoryError(
|
|
645
|
-
f"Cannot rename file {self} to directory target {target}"
|
|
646
|
-
)
|
|
636
|
+
raise IsADirectoryError(f"Cannot rename file {self} to directory target {target}")
|
|
647
637
|
|
|
648
638
|
if source_is_dir:
|
|
649
639
|
if not target_exists:
|
|
@@ -680,6 +670,14 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
680
670
|
# For cloud storage, replace is same as rename (always overwrites)
|
|
681
671
|
return await self.a_rename(target)
|
|
682
672
|
|
|
673
|
+
async def a_resolve(self) -> "PanPath":
|
|
674
|
+
"""Resolve to absolute path (no-op for cloud paths).
|
|
675
|
+
|
|
676
|
+
Returns:
|
|
677
|
+
Self (cloud paths are already absolute)
|
|
678
|
+
"""
|
|
679
|
+
return await self.a_readlink() if await self.a_is_symlink() else self
|
|
680
|
+
|
|
683
681
|
async def a_rmdir(self) -> None:
|
|
684
682
|
"""Remove empty directory marker."""
|
|
685
683
|
await self.async_client.rmdir(str(self))
|
|
@@ -702,11 +700,16 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
702
700
|
|
|
703
701
|
return PanPath(target) # type: ignore
|
|
704
702
|
|
|
705
|
-
async def a_symlink_to(
|
|
703
|
+
async def a_symlink_to(
|
|
704
|
+
self,
|
|
705
|
+
target: Union[str, "CloudPath"],
|
|
706
|
+
target_is_directory: bool = False,
|
|
707
|
+
) -> None:
|
|
706
708
|
"""Create symlink pointing to target (via metadata).
|
|
707
709
|
|
|
708
710
|
Args:
|
|
709
711
|
target: Path this symlink should point to (absolute with scheme or relative)
|
|
712
|
+
target_is_directory: Ignored (for compatibility with pathlib)
|
|
710
713
|
"""
|
|
711
714
|
target_str = str(target)
|
|
712
715
|
# If target doesn't have a scheme prefix, treat as relative path
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"""Async Google Cloud Storage client implementation."""
|
|
2
2
|
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
3
5
|
import asyncio
|
|
4
6
|
import datetime
|
|
5
7
|
import weakref
|
|
@@ -11,6 +13,7 @@ from panpath.clients import AsyncClient, AsyncFileHandle
|
|
|
11
13
|
from panpath.exceptions import MissingDependencyError, NoStatError
|
|
12
14
|
|
|
13
15
|
if TYPE_CHECKING:
|
|
16
|
+
from panpath.base import PanPath
|
|
14
17
|
from gcloud.aio.storage import Storage
|
|
15
18
|
|
|
16
19
|
try:
|
|
@@ -438,7 +441,12 @@ class AsyncGSClient(AsyncClient):
|
|
|
438
441
|
# Then set the symlink metadata
|
|
439
442
|
await self.set_metadata(path, {self.__class__.symlink_target_metaname: target})
|
|
440
443
|
|
|
441
|
-
async def glob(
|
|
444
|
+
async def glob(
|
|
445
|
+
self,
|
|
446
|
+
path: str,
|
|
447
|
+
pattern: str,
|
|
448
|
+
_return_panpath: bool = True,
|
|
449
|
+
) -> AsyncGenerator[Union[str, "PanPath"], None]:
|
|
442
450
|
"""Glob for files matching pattern.
|
|
443
451
|
|
|
444
452
|
Args:
|
|
@@ -471,19 +479,13 @@ class AsyncGSClient(AsyncClient):
|
|
|
471
479
|
else:
|
|
472
480
|
file_pattern = "*"
|
|
473
481
|
|
|
474
|
-
results = []
|
|
475
482
|
for item in items:
|
|
476
483
|
blob_name = item["name"]
|
|
477
484
|
if fnmatch(blob_name, f"*{file_pattern}"):
|
|
478
485
|
if not _return_panpath:
|
|
479
|
-
|
|
486
|
+
yield f"{self.prefix[0]}://{bucket_name}/{blob_name}"
|
|
480
487
|
else:
|
|
481
|
-
|
|
482
|
-
PanPath(
|
|
483
|
-
f"{self.prefix[0]}://{bucket_name}/{blob_name}"
|
|
484
|
-
) # type: ignore[arg-type]
|
|
485
|
-
)
|
|
486
|
-
return results
|
|
488
|
+
yield PanPath(f"{self.prefix[0]}://{bucket_name}/{blob_name}")
|
|
487
489
|
else:
|
|
488
490
|
# Non-recursive - list blobs with delimiter
|
|
489
491
|
blob_prefix = f"{prefix}/" if prefix and not prefix.endswith("/") else prefix
|
|
@@ -495,24 +497,19 @@ class AsyncGSClient(AsyncClient):
|
|
|
495
497
|
)
|
|
496
498
|
items = response.get("items", [])
|
|
497
499
|
|
|
498
|
-
results = []
|
|
499
500
|
for item in items:
|
|
500
501
|
blob_name = item["name"]
|
|
501
502
|
if fnmatch(blob_name, f"{prefix}{pattern}"):
|
|
502
503
|
if not _return_panpath:
|
|
503
|
-
|
|
504
|
+
yield f"{self.prefix[0]}://{bucket_name}/{blob_name}"
|
|
504
505
|
else:
|
|
505
|
-
|
|
506
|
-
PanPath(
|
|
507
|
-
f"{self.prefix[0]}://{bucket_name}/{blob_name}"
|
|
508
|
-
) # type: ignore[arg-type]
|
|
509
|
-
)
|
|
510
|
-
return results
|
|
506
|
+
yield PanPath(f"{self.prefix[0]}://{bucket_name}/{blob_name}")
|
|
511
507
|
|
|
512
508
|
async def walk( # type: ignore[override]
|
|
513
509
|
self,
|
|
514
510
|
path: str,
|
|
515
|
-
|
|
511
|
+
_return_panpath: bool = True,
|
|
512
|
+
) -> AsyncGenerator[tuple[Union[str, "PanPath"], list[str], list[str]], None]:
|
|
516
513
|
"""Walk directory tree.
|
|
517
514
|
|
|
518
515
|
Args:
|
|
@@ -521,6 +518,8 @@ class AsyncGSClient(AsyncClient):
|
|
|
521
518
|
Yields:
|
|
522
519
|
Tuples of (dirpath, dirnames, filenames)
|
|
523
520
|
"""
|
|
521
|
+
from panpath.base import PanPath
|
|
522
|
+
|
|
524
523
|
bucket_name, blob_prefix = self.__class__._parse_path(path)
|
|
525
524
|
storage = await self._get_client()
|
|
526
525
|
|
|
@@ -579,7 +578,7 @@ class AsyncGSClient(AsyncClient):
|
|
|
579
578
|
|
|
580
579
|
# Yield each directory tuple
|
|
581
580
|
for d, (subdirs, files) in sorted(dirs.items()):
|
|
582
|
-
yield (d, sorted(subdirs), sorted(files))
|
|
581
|
+
yield (PanPath(d) if _return_panpath else d, sorted(subdirs), sorted(files))
|
|
583
582
|
|
|
584
583
|
async def touch( # type: ignore[no-untyped-def, override]
|
|
585
584
|
self,
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import warnings
|
|
4
4
|
import os
|
|
5
5
|
import sys
|
|
6
|
-
from typing import TYPE_CHECKING, Any, Optional, Union
|
|
6
|
+
from typing import TYPE_CHECKING, Any, Optional, Union, Iterator
|
|
7
7
|
|
|
8
8
|
from panpath.clients import SyncClient, SyncFileHandle
|
|
9
9
|
from panpath.exceptions import MissingDependencyError, NoStatError
|
|
@@ -11,6 +11,7 @@ from panpath.exceptions import MissingDependencyError, NoStatError
|
|
|
11
11
|
if TYPE_CHECKING:
|
|
12
12
|
from google.cloud import storage # type: ignore[import-untyped, unused-ignore]
|
|
13
13
|
from google.api_core.exceptions import NotFound
|
|
14
|
+
from panpath.base import PanPath
|
|
14
15
|
|
|
15
16
|
try:
|
|
16
17
|
with warnings.catch_warnings():
|
|
@@ -317,7 +318,7 @@ class GSClient(SyncClient):
|
|
|
317
318
|
path: str,
|
|
318
319
|
pattern: str,
|
|
319
320
|
_return_panpath: bool = False,
|
|
320
|
-
) ->
|
|
321
|
+
) -> Iterator[Union[str, "PanPath"]]:
|
|
321
322
|
"""Glob for files matching pattern.
|
|
322
323
|
|
|
323
324
|
Args:
|
|
@@ -347,15 +348,13 @@ class GSClient(SyncClient):
|
|
|
347
348
|
else:
|
|
348
349
|
file_pattern = "*"
|
|
349
350
|
|
|
350
|
-
results = []
|
|
351
351
|
for blob in blobs:
|
|
352
352
|
if fnmatch(blob.name, f"*{file_pattern}"):
|
|
353
353
|
path_str = f"{self.prefix[0]}://{bucket_name}/{blob.name}"
|
|
354
354
|
if _return_panpath:
|
|
355
|
-
|
|
355
|
+
yield PanPath(path_str)
|
|
356
356
|
else: # pragma: no cover
|
|
357
|
-
|
|
358
|
-
return results
|
|
357
|
+
yield path_str
|
|
359
358
|
else:
|
|
360
359
|
# Non-recursive - list blobs with delimiter
|
|
361
360
|
prefix = (
|
|
@@ -363,17 +362,18 @@ class GSClient(SyncClient):
|
|
|
363
362
|
)
|
|
364
363
|
blobs = bucket.list_blobs(prefix=prefix, delimiter="/")
|
|
365
364
|
|
|
366
|
-
results = []
|
|
367
365
|
for blob in blobs:
|
|
368
366
|
if fnmatch(blob.name, f"{prefix}{pattern}"):
|
|
369
367
|
path_str = f"{self.prefix[0]}://{bucket_name}/{blob.name}"
|
|
370
368
|
if _return_panpath:
|
|
371
|
-
|
|
369
|
+
yield PanPath(path_str)
|
|
372
370
|
else: # pragma: no cover
|
|
373
|
-
|
|
374
|
-
return results
|
|
371
|
+
yield path_str
|
|
375
372
|
|
|
376
|
-
def walk(
|
|
373
|
+
def walk( # type: ignore[override]
|
|
374
|
+
self,
|
|
375
|
+
path: str,
|
|
376
|
+
) -> Iterator[tuple[str, list[str], list[str]]]:
|
|
377
377
|
"""Walk directory tree.
|
|
378
378
|
|
|
379
379
|
Args:
|
|
@@ -425,8 +425,8 @@ class GSClient(SyncClient):
|
|
|
425
425
|
dirs[parent_dir] = (set(), set())
|
|
426
426
|
dirs[parent_dir][1].add(parts[-1])
|
|
427
427
|
|
|
428
|
-
|
|
429
|
-
|
|
428
|
+
for d, (subdirs, files) in dirs.items():
|
|
429
|
+
yield d, sorted(subdirs), sorted(files)
|
|
430
430
|
|
|
431
431
|
def touch(self, path: str, exist_ok: bool = True) -> None:
|
|
432
432
|
"""Create empty file.
|
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
from pathlib import Path, PosixPath, WindowsPath
|
|
4
4
|
import os
|
|
5
5
|
import sys
|
|
6
|
-
from typing import Any, AsyncGenerator, Optional, Union
|
|
6
|
+
from typing import Any, AsyncGenerator, List, Optional, Tuple, Union
|
|
7
7
|
from panpath.base import PanPath
|
|
8
8
|
from panpath.cloud import CloudPath
|
|
9
|
+
from panpath.exceptions import MissingDependencyError
|
|
9
10
|
|
|
10
11
|
# Determine the concrete Path class for the current platform
|
|
11
12
|
_ConcretePath = WindowsPath if os.name == "nt" else PosixPath
|
|
@@ -47,8 +48,6 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
47
48
|
async def a_touch(self, mode: int = 0o666, exist_ok: bool = True) -> None:
|
|
48
49
|
"""Create the file if it does not exist or update the modification time (async)."""
|
|
49
50
|
if not HAS_AIOFILES:
|
|
50
|
-
from panpath.exceptions import MissingDependencyError
|
|
51
|
-
|
|
52
51
|
raise MissingDependencyError(
|
|
53
52
|
backend="async local paths",
|
|
54
53
|
package="aiofiles",
|
|
@@ -71,10 +70,36 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
71
70
|
Returns:
|
|
72
71
|
New path instance
|
|
73
72
|
"""
|
|
73
|
+
if not HAS_AIOFILES:
|
|
74
|
+
raise MissingDependencyError(
|
|
75
|
+
backend="async local paths",
|
|
76
|
+
package="aiofiles",
|
|
77
|
+
extra="all-async",
|
|
78
|
+
)
|
|
79
|
+
|
|
74
80
|
target_str = str(target)
|
|
75
81
|
await aiofiles.os.rename(str(self), target_str)
|
|
76
82
|
return PanPath(target_str)
|
|
77
83
|
|
|
84
|
+
async def a_replace(self, target: Union[str, "Path"]) -> "PanPath":
|
|
85
|
+
"""Rename the file or directory to target, overwriting if target exists.
|
|
86
|
+
|
|
87
|
+
Args:
|
|
88
|
+
target: New path
|
|
89
|
+
|
|
90
|
+
Returns:
|
|
91
|
+
New path instance
|
|
92
|
+
"""
|
|
93
|
+
return await self.a_rename(target)
|
|
94
|
+
|
|
95
|
+
async def a_resolve(self) -> "PanPath":
|
|
96
|
+
"""Resolve to absolute path (no-op for cloud paths).
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
Self (cloud paths are already absolute)
|
|
100
|
+
"""
|
|
101
|
+
return await self.a_readlink() if await self.a_is_symlink() else self
|
|
102
|
+
|
|
78
103
|
async def a_copy(self, target: Union[str, "Path"], follow_symlinks: bool = True) -> "PanPath":
|
|
79
104
|
"""Copy file to target.
|
|
80
105
|
|
|
@@ -86,6 +111,13 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
86
111
|
Returns:
|
|
87
112
|
Target path instance
|
|
88
113
|
"""
|
|
114
|
+
if not HAS_AIOFILES:
|
|
115
|
+
raise MissingDependencyError(
|
|
116
|
+
backend="async local paths",
|
|
117
|
+
package="aiofiles",
|
|
118
|
+
extra="all-async",
|
|
119
|
+
)
|
|
120
|
+
|
|
89
121
|
target_str = str(target)
|
|
90
122
|
# Check if cross-storage operation
|
|
91
123
|
if CloudPath._is_cross_storage_op(str(self), target_str): # pragma: no cover
|
|
@@ -101,12 +133,165 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
101
133
|
|
|
102
134
|
return PanPath(target_str)
|
|
103
135
|
|
|
136
|
+
async def a_copytree(
|
|
137
|
+
self,
|
|
138
|
+
target: Union[str, "Path"],
|
|
139
|
+
follow_symlinks: bool = True,
|
|
140
|
+
) -> "PanPath":
|
|
141
|
+
"""Recursively copy the directory and all its contents to the target path.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
target: Destination PanPath to copy to.
|
|
145
|
+
follow_symlinks: If True, copies the contents of symlinks.
|
|
146
|
+
|
|
147
|
+
Returns:
|
|
148
|
+
The copied PanPath instance.
|
|
149
|
+
"""
|
|
150
|
+
if not HAS_AIOFILES:
|
|
151
|
+
raise MissingDependencyError(
|
|
152
|
+
backend="async local paths",
|
|
153
|
+
package="aiofiles",
|
|
154
|
+
extra="all-async",
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
target = PanPath(target)
|
|
158
|
+
await target.a_mkdir(parents=True, exist_ok=True)
|
|
159
|
+
|
|
160
|
+
async for entry in self.a_iterdir():
|
|
161
|
+
src_path = entry
|
|
162
|
+
dest_path = target / entry.name
|
|
163
|
+
|
|
164
|
+
if await src_path.a_is_dir():
|
|
165
|
+
await src_path.a_copytree(dest_path, follow_symlinks=follow_symlinks)
|
|
166
|
+
else:
|
|
167
|
+
await src_path.a_copy(dest_path, follow_symlinks=follow_symlinks)
|
|
168
|
+
|
|
169
|
+
return target
|
|
170
|
+
|
|
171
|
+
async def a_walk(self) -> AsyncGenerator[Tuple["LocalPath", List[str], List[str]], None]:
|
|
172
|
+
"""Asynchronously walk the directory tree.
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
A list of tuples (dirpath, dirnames, filenames)
|
|
176
|
+
"""
|
|
177
|
+
if not HAS_AIOFILES:
|
|
178
|
+
raise MissingDependencyError(
|
|
179
|
+
backend="async local paths",
|
|
180
|
+
package="aiofiles",
|
|
181
|
+
extra="all-async",
|
|
182
|
+
)
|
|
183
|
+
dirnames = []
|
|
184
|
+
filenames = []
|
|
185
|
+
for entry in await aiofiles.os.listdir(str(self)):
|
|
186
|
+
path = self / entry
|
|
187
|
+
if await path.a_is_dir():
|
|
188
|
+
dirnames.append(entry)
|
|
189
|
+
async for sub in path.a_walk():
|
|
190
|
+
yield sub
|
|
191
|
+
else:
|
|
192
|
+
filenames.append(entry)
|
|
193
|
+
yield (self, dirnames, filenames)
|
|
194
|
+
|
|
195
|
+
async def a_readlink(self) -> "LocalPath":
|
|
196
|
+
"""Asynchronously read the target of a symbolic link.
|
|
197
|
+
|
|
198
|
+
Returns:
|
|
199
|
+
The path to which the symbolic link points.
|
|
200
|
+
"""
|
|
201
|
+
if not HAS_AIOFILES:
|
|
202
|
+
raise MissingDependencyError(
|
|
203
|
+
backend="async local paths",
|
|
204
|
+
package="aiofiles",
|
|
205
|
+
extra="all-async",
|
|
206
|
+
)
|
|
207
|
+
|
|
208
|
+
return PanPath(await aiofiles.os.readlink(str(self)))
|
|
209
|
+
|
|
210
|
+
async def a_symlink_to(
|
|
211
|
+
self,
|
|
212
|
+
target: Union[str, "Path"],
|
|
213
|
+
target_is_directory: bool = False,
|
|
214
|
+
) -> None:
|
|
215
|
+
"""Asynchronously create a symbolic link pointing to target.
|
|
216
|
+
|
|
217
|
+
Args:
|
|
218
|
+
target: The target path the symbolic link points to.
|
|
219
|
+
target_is_directory: Whether the target is a directory.
|
|
220
|
+
"""
|
|
221
|
+
if not HAS_AIOFILES:
|
|
222
|
+
raise MissingDependencyError(
|
|
223
|
+
backend="async local paths",
|
|
224
|
+
package="aiofiles",
|
|
225
|
+
extra="all-async",
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
await aiofiles.os.symlink(str(target), str(self), target_is_directory=target_is_directory)
|
|
229
|
+
|
|
230
|
+
async def a_glob(self, pattern: str) -> AsyncGenerator["LocalPath", None]:
|
|
231
|
+
"""Asynchronously yield paths matching the glob pattern.
|
|
232
|
+
|
|
233
|
+
Args:
|
|
234
|
+
pattern: Glob pattern (relative)
|
|
235
|
+
|
|
236
|
+
Yields:
|
|
237
|
+
Matching LocalPath instances
|
|
238
|
+
"""
|
|
239
|
+
if not HAS_AIOFILES:
|
|
240
|
+
raise MissingDependencyError(
|
|
241
|
+
backend="async local paths",
|
|
242
|
+
package="aiofiles",
|
|
243
|
+
extra="all-async",
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
from fnmatch import fnmatch
|
|
247
|
+
|
|
248
|
+
if not pattern:
|
|
249
|
+
raise ValueError("Unacceptable pattern: {!r}".format(pattern))
|
|
250
|
+
|
|
251
|
+
# aiofiles does not support globbing natively
|
|
252
|
+
# let's implement it with walk
|
|
253
|
+
if "**" in pattern:
|
|
254
|
+
pattern_parts = pattern.split("**/")
|
|
255
|
+
if len(pattern_parts) > 1:
|
|
256
|
+
file_pattern = pattern_parts[1]
|
|
257
|
+
else: # pragma: no cover
|
|
258
|
+
file_pattern = "*"
|
|
259
|
+
async for dirpath, _, filenames in self.a_walk():
|
|
260
|
+
for filename in filenames:
|
|
261
|
+
if fnmatch(filename, file_pattern):
|
|
262
|
+
yield dirpath / filename
|
|
263
|
+
else:
|
|
264
|
+
async for entry in self.a_iterdir():
|
|
265
|
+
if fnmatch(entry.name, pattern):
|
|
266
|
+
yield entry
|
|
267
|
+
|
|
268
|
+
async def a_rglob(self, pattern: str) -> AsyncGenerator["LocalPath", None]:
|
|
269
|
+
"""Recursively yield all existing files matching the given pattern.
|
|
270
|
+
|
|
271
|
+
Args:
|
|
272
|
+
pattern: Glob pattern (relative)
|
|
273
|
+
|
|
274
|
+
Yields:
|
|
275
|
+
Matching LocalPath instances
|
|
276
|
+
"""
|
|
277
|
+
if not HAS_AIOFILES:
|
|
278
|
+
raise MissingDependencyError(
|
|
279
|
+
backend="async local paths",
|
|
280
|
+
package="aiofiles",
|
|
281
|
+
extra="all-async",
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
if not pattern:
|
|
285
|
+
raise ValueError("Unacceptable pattern: {!r}".format(pattern))
|
|
286
|
+
|
|
287
|
+
# use a_glob to implement rglob
|
|
288
|
+
async for path in self.a_glob(f"**/{pattern}"):
|
|
289
|
+
yield path
|
|
290
|
+
|
|
104
291
|
# Async I/O operations (prefixed with a_)
|
|
105
292
|
async def a_exists(self) -> bool:
|
|
106
293
|
"""Check if path exists (async)."""
|
|
107
294
|
if not HAS_AIOFILES:
|
|
108
|
-
from panpath.exceptions import MissingDependencyError
|
|
109
|
-
|
|
110
295
|
raise MissingDependencyError(
|
|
111
296
|
backend="async local paths",
|
|
112
297
|
package="aiofiles",
|
|
@@ -117,89 +302,93 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
117
302
|
async def a_is_file(self) -> bool:
|
|
118
303
|
"""Check if path is a file (async)."""
|
|
119
304
|
if not HAS_AIOFILES:
|
|
120
|
-
from panpath.exceptions import MissingDependencyError
|
|
121
|
-
|
|
122
305
|
raise MissingDependencyError(
|
|
123
306
|
backend="async local paths",
|
|
124
307
|
package="aiofiles",
|
|
125
308
|
extra="all-async",
|
|
126
309
|
)
|
|
310
|
+
|
|
127
311
|
return await aiofiles.os.path.isfile(str(self)) # type: ignore[no-any-return]
|
|
128
312
|
|
|
129
313
|
async def a_is_dir(self) -> bool:
|
|
130
314
|
"""Check if path is a directory (async)."""
|
|
131
315
|
if not HAS_AIOFILES:
|
|
132
|
-
from panpath.exceptions import MissingDependencyError
|
|
133
|
-
|
|
134
316
|
raise MissingDependencyError(
|
|
135
317
|
backend="async local paths",
|
|
136
318
|
package="aiofiles",
|
|
137
319
|
extra="all-async",
|
|
138
320
|
)
|
|
321
|
+
|
|
139
322
|
return await aiofiles.os.path.isdir(str(self)) # type: ignore[no-any-return]
|
|
140
323
|
|
|
141
324
|
async def a_read_bytes(self) -> bytes:
|
|
142
325
|
"""Read file as bytes (async)."""
|
|
143
326
|
if not HAS_AIOFILES:
|
|
144
|
-
from panpath.exceptions import MissingDependencyError
|
|
145
|
-
|
|
146
327
|
raise MissingDependencyError(
|
|
147
328
|
backend="async local paths",
|
|
148
329
|
package="aiofiles",
|
|
149
330
|
extra="all-async",
|
|
150
331
|
)
|
|
332
|
+
|
|
151
333
|
async with aiofiles.open(str(self), mode="rb") as f:
|
|
152
334
|
return await f.read() # type: ignore[no-any-return]
|
|
153
335
|
|
|
154
336
|
async def a_read_text(self, encoding: str = "utf-8") -> str:
|
|
155
337
|
"""Read file as text (async)."""
|
|
156
338
|
if not HAS_AIOFILES:
|
|
157
|
-
from panpath.exceptions import MissingDependencyError
|
|
158
|
-
|
|
159
339
|
raise MissingDependencyError(
|
|
160
340
|
backend="async local paths",
|
|
161
341
|
package="aiofiles",
|
|
162
342
|
extra="all-async",
|
|
163
343
|
)
|
|
344
|
+
|
|
164
345
|
async with aiofiles.open(str(self), mode="r", encoding=encoding) as f:
|
|
165
346
|
return await f.read() # type: ignore[no-any-return]
|
|
166
347
|
|
|
167
348
|
async def a_write_bytes(self, data: bytes) -> None:
|
|
168
349
|
"""Write bytes to file (async)."""
|
|
169
350
|
if not HAS_AIOFILES:
|
|
170
|
-
from panpath.exceptions import MissingDependencyError
|
|
171
|
-
|
|
172
351
|
raise MissingDependencyError(
|
|
173
352
|
backend="async local paths",
|
|
174
353
|
package="aiofiles",
|
|
175
354
|
extra="all-async",
|
|
176
355
|
)
|
|
356
|
+
|
|
177
357
|
async with aiofiles.open(str(self), mode="wb") as f:
|
|
178
358
|
await f.write(data)
|
|
179
359
|
|
|
180
360
|
async def a_write_text(self, data: str, encoding: str = "utf-8") -> None:
|
|
181
361
|
"""Write text to file (async)."""
|
|
182
362
|
if not HAS_AIOFILES:
|
|
183
|
-
from panpath.exceptions import MissingDependencyError
|
|
184
|
-
|
|
185
363
|
raise MissingDependencyError(
|
|
186
364
|
backend="async local paths",
|
|
187
365
|
package="aiofiles",
|
|
188
366
|
extra="all-async",
|
|
189
367
|
)
|
|
368
|
+
|
|
190
369
|
async with aiofiles.open(str(self), mode="w", encoding=encoding) as f:
|
|
191
370
|
await f.write(data)
|
|
192
371
|
|
|
372
|
+
async def a_is_symlink(self) -> bool:
|
|
373
|
+
"""Check if path is a symlink (async)."""
|
|
374
|
+
if not HAS_AIOFILES:
|
|
375
|
+
raise MissingDependencyError(
|
|
376
|
+
backend="async local paths",
|
|
377
|
+
package="aiofiles",
|
|
378
|
+
extra="all-async",
|
|
379
|
+
)
|
|
380
|
+
|
|
381
|
+
return await aiofiles.os.path.islink(str(self)) # type: ignore[no-any-return]
|
|
382
|
+
|
|
193
383
|
async def a_unlink(self, missing_ok: bool = False) -> None:
|
|
194
384
|
"""Delete file (async)."""
|
|
195
385
|
if not HAS_AIOFILES:
|
|
196
|
-
from panpath.exceptions import MissingDependencyError
|
|
197
|
-
|
|
198
386
|
raise MissingDependencyError(
|
|
199
387
|
backend="async local paths",
|
|
200
388
|
package="aiofiles",
|
|
201
389
|
extra="all-async",
|
|
202
390
|
)
|
|
391
|
+
|
|
203
392
|
try:
|
|
204
393
|
await aiofiles.os.remove(str(self))
|
|
205
394
|
except FileNotFoundError:
|
|
@@ -211,13 +400,12 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
211
400
|
) -> None:
|
|
212
401
|
"""Create directory (async)."""
|
|
213
402
|
if not HAS_AIOFILES:
|
|
214
|
-
from panpath.exceptions import MissingDependencyError
|
|
215
|
-
|
|
216
403
|
raise MissingDependencyError(
|
|
217
404
|
backend="async local paths",
|
|
218
405
|
package="aiofiles",
|
|
219
406
|
extra="all-async",
|
|
220
407
|
)
|
|
408
|
+
|
|
221
409
|
if parents:
|
|
222
410
|
await aiofiles.os.makedirs(str(self), mode=mode, exist_ok=exist_ok)
|
|
223
411
|
else:
|
|
@@ -230,20 +418,17 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
230
418
|
async def a_rmdir(self) -> None:
|
|
231
419
|
"""Remove empty directory (async)."""
|
|
232
420
|
if not HAS_AIOFILES:
|
|
233
|
-
from panpath.exceptions import MissingDependencyError
|
|
234
|
-
|
|
235
421
|
raise MissingDependencyError(
|
|
236
422
|
backend="async local paths",
|
|
237
423
|
package="aiofiles",
|
|
238
424
|
extra="all-async",
|
|
239
425
|
)
|
|
426
|
+
|
|
240
427
|
await aiofiles.os.rmdir(str(self))
|
|
241
428
|
|
|
242
429
|
async def a_rmtree(self) -> None:
|
|
243
430
|
"""Recursively remove directory and its contents (async)."""
|
|
244
431
|
if not HAS_AIOFILES:
|
|
245
|
-
from panpath.exceptions import MissingDependencyError
|
|
246
|
-
|
|
247
432
|
raise MissingDependencyError(
|
|
248
433
|
backend="async local paths",
|
|
249
434
|
package="aiofiles",
|
|
@@ -261,26 +446,24 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
261
446
|
async def a_iterdir(self) -> AsyncGenerator["LocalPath", None]:
|
|
262
447
|
"""List directory contents (async)."""
|
|
263
448
|
if not HAS_AIOFILES:
|
|
264
|
-
from panpath.exceptions import MissingDependencyError
|
|
265
|
-
|
|
266
449
|
raise MissingDependencyError(
|
|
267
450
|
backend="async local paths",
|
|
268
451
|
package="aiofiles",
|
|
269
452
|
extra="all-async",
|
|
270
453
|
)
|
|
454
|
+
|
|
271
455
|
for item in await aiofiles.os.listdir(str(self)):
|
|
272
456
|
yield self / item
|
|
273
457
|
|
|
274
458
|
async def a_stat(self) -> os.stat_result:
|
|
275
459
|
"""Get file stats (async)."""
|
|
276
460
|
if not HAS_AIOFILES:
|
|
277
|
-
from panpath.exceptions import MissingDependencyError
|
|
278
|
-
|
|
279
461
|
raise MissingDependencyError(
|
|
280
462
|
backend="async local paths",
|
|
281
463
|
package="aiofiles",
|
|
282
464
|
extra="all-async",
|
|
283
465
|
)
|
|
466
|
+
|
|
284
467
|
return await aiofiles.os.stat(str(self)) # type: ignore[no-any-return]
|
|
285
468
|
|
|
286
469
|
def a_open(
|
|
@@ -297,13 +480,12 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
297
480
|
Async file handle from aiofiles
|
|
298
481
|
"""
|
|
299
482
|
if not HAS_AIOFILES:
|
|
300
|
-
from panpath.exceptions import MissingDependencyError
|
|
301
|
-
|
|
302
483
|
raise MissingDependencyError(
|
|
303
484
|
backend="async local paths",
|
|
304
485
|
package="aiofiles",
|
|
305
486
|
extra="all-async",
|
|
306
487
|
)
|
|
488
|
+
|
|
307
489
|
return aiofiles.open(
|
|
308
490
|
str(self),
|
|
309
491
|
mode=mode,
|
|
@@ -13,6 +13,7 @@ from panpath.exceptions import MissingDependencyError, NoStatError
|
|
|
13
13
|
|
|
14
14
|
if TYPE_CHECKING:
|
|
15
15
|
import aioboto3 # type: ignore[import-not-found]
|
|
16
|
+
from panpath.base import PanPath
|
|
16
17
|
from aiobotocore.client import AioBaseClient # type: ignore[import-untyped, unused-ignore]
|
|
17
18
|
from botocore.exceptions import ClientError # type: ignore[import-untyped, unused-ignore]
|
|
18
19
|
|
|
@@ -443,7 +444,12 @@ class AsyncS3Client(AsyncClient):
|
|
|
443
444
|
Metadata={self.__class__.symlink_target_metaname: target},
|
|
444
445
|
)
|
|
445
446
|
|
|
446
|
-
async def glob(
|
|
447
|
+
async def glob(
|
|
448
|
+
self,
|
|
449
|
+
path: str,
|
|
450
|
+
pattern: str,
|
|
451
|
+
_return_panpath: bool = False,
|
|
452
|
+
) -> AsyncGenerator[Union[str, "PanPath"], None]:
|
|
447
453
|
"""Glob for files matching pattern.
|
|
448
454
|
|
|
449
455
|
Args:
|
|
@@ -455,6 +461,7 @@ class AsyncS3Client(AsyncClient):
|
|
|
455
461
|
List of matching paths (as PanPath objects or strings)
|
|
456
462
|
"""
|
|
457
463
|
from fnmatch import fnmatch
|
|
464
|
+
from panpath.base import PanPath
|
|
458
465
|
|
|
459
466
|
bucket, prefix = self.__class__._parse_path(path)
|
|
460
467
|
|
|
@@ -472,19 +479,15 @@ class AsyncS3Client(AsyncClient):
|
|
|
472
479
|
else:
|
|
473
480
|
file_pattern = "*"
|
|
474
481
|
|
|
475
|
-
results = []
|
|
476
482
|
async for page in pages:
|
|
477
483
|
for obj in page.get("Contents", []):
|
|
478
484
|
key = obj["Key"]
|
|
479
485
|
if fnmatch(key, f"*{file_pattern}"):
|
|
480
486
|
path_str = f"{self.prefix[0]}://{bucket}/{key}"
|
|
481
487
|
if _return_panpath:
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
results.append(PanPath(path_str))
|
|
488
|
+
yield PanPath(path_str)
|
|
485
489
|
else:
|
|
486
|
-
|
|
487
|
-
return results
|
|
490
|
+
yield path_str
|
|
488
491
|
else:
|
|
489
492
|
# Non-recursive - list objects with delimiter
|
|
490
493
|
prefix_with_slash = f"{prefix}/" if prefix and not prefix.endswith("/") else prefix
|
|
@@ -492,23 +495,20 @@ class AsyncS3Client(AsyncClient):
|
|
|
492
495
|
Bucket=bucket, Prefix=prefix_with_slash, Delimiter="/"
|
|
493
496
|
)
|
|
494
497
|
|
|
495
|
-
results = []
|
|
496
498
|
for obj in response.get("Contents", []):
|
|
497
499
|
key = obj["Key"]
|
|
498
500
|
if fnmatch(key, f"{prefix_with_slash}{pattern}"):
|
|
499
501
|
path_str = f"{self.prefix[0]}://{bucket}/{key}"
|
|
500
502
|
if _return_panpath:
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
results.append(PanPath(path_str))
|
|
503
|
+
yield PanPath(path_str)
|
|
504
504
|
else:
|
|
505
|
-
|
|
506
|
-
return results
|
|
505
|
+
yield path_str
|
|
507
506
|
|
|
508
507
|
async def walk( # type: ignore[override]
|
|
509
508
|
self,
|
|
510
509
|
path: str,
|
|
511
|
-
|
|
510
|
+
_return_panpath: bool = True,
|
|
511
|
+
) -> AsyncGenerator[tuple[Union[str, "PanPath"], list[str], list[str]], None]:
|
|
512
512
|
"""Walk directory tree.
|
|
513
513
|
|
|
514
514
|
Args:
|
|
@@ -517,6 +517,8 @@ class AsyncS3Client(AsyncClient):
|
|
|
517
517
|
Yields:
|
|
518
518
|
Tuples of (dirpath, dirnames, filenames)
|
|
519
519
|
"""
|
|
520
|
+
from panpath.base import PanPath
|
|
521
|
+
|
|
520
522
|
bucket, prefix = self.__class__._parse_path(path)
|
|
521
523
|
|
|
522
524
|
# List all objects under prefix
|
|
@@ -574,7 +576,7 @@ class AsyncS3Client(AsyncClient):
|
|
|
574
576
|
|
|
575
577
|
# Yield tuples
|
|
576
578
|
for d, (subdirs, files) in sorted(dirs.items()):
|
|
577
|
-
yield (d, sorted(subdirs), sorted(files))
|
|
579
|
+
yield (PanPath(d) if _return_panpath else d, sorted(subdirs), sorted(files))
|
|
578
580
|
|
|
579
581
|
async def touch(self, path: str, exist_ok: bool = True, mode: Optional[int] = None) -> None:
|
|
580
582
|
"""Create empty file.
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import os
|
|
4
4
|
import re
|
|
5
|
-
from typing import TYPE_CHECKING, Any, Optional, Union
|
|
5
|
+
from typing import TYPE_CHECKING, Any, Iterator, Optional, Union
|
|
6
6
|
|
|
7
7
|
from panpath.clients import SyncClient, SyncFileHandle
|
|
8
8
|
from panpath.exceptions import MissingDependencyError
|
|
@@ -10,6 +10,7 @@ from panpath.exceptions import MissingDependencyError
|
|
|
10
10
|
if TYPE_CHECKING:
|
|
11
11
|
import boto3 # type: ignore[import-untyped]
|
|
12
12
|
from botocore.exceptions import ClientError # type: ignore[import-untyped]
|
|
13
|
+
from panpath.base import PanPath
|
|
13
14
|
|
|
14
15
|
try:
|
|
15
16
|
import boto3
|
|
@@ -336,7 +337,7 @@ class S3Client(SyncClient):
|
|
|
336
337
|
|
|
337
338
|
def glob( # type: ignore[override]
|
|
338
339
|
self, path: str, pattern: str, _return_panpath: bool = False
|
|
339
|
-
) ->
|
|
340
|
+
) -> Iterator[Union[str, "PanPath"]]:
|
|
340
341
|
"""Glob for files matching pattern.
|
|
341
342
|
|
|
342
343
|
Args:
|
|
@@ -348,6 +349,7 @@ class S3Client(SyncClient):
|
|
|
348
349
|
List of matching paths (as PanPath objects or strings)
|
|
349
350
|
"""
|
|
350
351
|
from fnmatch import fnmatch
|
|
352
|
+
from panpath.base import PanPath
|
|
351
353
|
|
|
352
354
|
bucket, prefix = self.__class__._parse_path(path)
|
|
353
355
|
|
|
@@ -364,19 +366,15 @@ class S3Client(SyncClient):
|
|
|
364
366
|
else:
|
|
365
367
|
file_pattern = "*"
|
|
366
368
|
|
|
367
|
-
results = []
|
|
368
369
|
for page in pages:
|
|
369
370
|
for obj in page.get("Contents", []):
|
|
370
371
|
key = obj["Key"]
|
|
371
372
|
if fnmatch(key, f"*{file_pattern}"):
|
|
372
373
|
path_str = f"{self.prefix[0]}://{bucket}/{key}"
|
|
373
374
|
if _return_panpath:
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
results.append(PanPath(path_str))
|
|
375
|
+
yield PanPath(path_str)
|
|
377
376
|
else: # pragma: no cover
|
|
378
|
-
|
|
379
|
-
return results
|
|
377
|
+
yield path_str
|
|
380
378
|
else:
|
|
381
379
|
# Non-recursive - list objects with delimiter
|
|
382
380
|
prefix_with_slash = f"{prefix}/" if prefix and not prefix.endswith("/") else prefix
|
|
@@ -384,22 +382,18 @@ class S3Client(SyncClient):
|
|
|
384
382
|
Bucket=bucket, Prefix=prefix_with_slash, Delimiter="/"
|
|
385
383
|
)
|
|
386
384
|
|
|
387
|
-
results = []
|
|
388
385
|
for obj in response.get("Contents", []):
|
|
389
386
|
key = obj["Key"]
|
|
390
387
|
if fnmatch(key, f"{prefix_with_slash}{pattern}"):
|
|
391
388
|
path_str = f"{self.prefix[0]}://{bucket}/{key}"
|
|
392
389
|
if _return_panpath:
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
results.append(PanPath(path_str))
|
|
390
|
+
yield PanPath(path_str)
|
|
396
391
|
else: # pragma: no cover
|
|
397
|
-
|
|
398
|
-
return results
|
|
392
|
+
yield path_str
|
|
399
393
|
|
|
400
394
|
def walk( # type: ignore[override]
|
|
401
395
|
self, path: str
|
|
402
|
-
) ->
|
|
396
|
+
) -> Iterator[tuple[str, list[str], list[str]]]:
|
|
403
397
|
"""Walk directory tree.
|
|
404
398
|
|
|
405
399
|
Args:
|
|
@@ -462,8 +456,8 @@ class S3Client(SyncClient):
|
|
|
462
456
|
if parts[-1]: # Skip empty strings
|
|
463
457
|
dirs[parent_dir][1].add(parts[-1])
|
|
464
458
|
|
|
465
|
-
|
|
466
|
-
|
|
459
|
+
for d, (subdirs, files) in dirs.items():
|
|
460
|
+
yield d, sorted(subdirs), sorted(files)
|
|
467
461
|
|
|
468
462
|
def touch(self, path: str, exist_ok: bool = True) -> None:
|
|
469
463
|
"""Create empty file.
|
|
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
|
|
|
4
4
|
|
|
5
5
|
[tool.poetry]
|
|
6
6
|
name = "panpath"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.4.1"
|
|
8
8
|
description = "Universal sync/async local/cloud path library with pathlib-compatible interface"
|
|
9
9
|
authors = ["panpath contributors"]
|
|
10
10
|
readme = "README.md"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|