panpath 0.4.6__tar.gz → 0.4.8__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.4.6 → panpath-0.4.8}/PKG-INFO +2 -2
- {panpath-0.4.6 → panpath-0.4.8}/panpath/__init__.py +1 -1
- {panpath-0.4.6 → panpath-0.4.8}/panpath/azure_async_client.py +8 -1
- {panpath-0.4.6 → panpath-0.4.8}/panpath/azure_client.py +10 -2
- {panpath-0.4.6 → panpath-0.4.8}/panpath/base.py +64 -47
- {panpath-0.4.6 → panpath-0.4.8}/panpath/clients.py +26 -0
- {panpath-0.4.6 → panpath-0.4.8}/panpath/cloud.py +104 -62
- {panpath-0.4.6 → panpath-0.4.8}/panpath/gs_async_client.py +17 -12
- {panpath-0.4.6 → panpath-0.4.8}/panpath/gs_client.py +21 -9
- {panpath-0.4.6 → panpath-0.4.8}/panpath/local_path.py +155 -38
- {panpath-0.4.6 → panpath-0.4.8}/panpath/s3_async_client.py +3 -2
- {panpath-0.4.6 → panpath-0.4.8}/panpath/s3_client.py +3 -2
- {panpath-0.4.6 → panpath-0.4.8}/pyproject.toml +2 -2
- {panpath-0.4.6 → panpath-0.4.8}/LICENSE +0 -0
- {panpath-0.4.6 → panpath-0.4.8}/README.md +0 -0
- {panpath-0.4.6 → panpath-0.4.8}/panpath/azure_path.py +0 -0
- {panpath-0.4.6 → panpath-0.4.8}/panpath/exceptions.py +0 -0
- {panpath-0.4.6 → panpath-0.4.8}/panpath/gs_path.py +0 -0
- {panpath-0.4.6 → panpath-0.4.8}/panpath/py.typed +0 -0
- {panpath-0.4.6 → panpath-0.4.8}/panpath/registry.py +0 -0
- {panpath-0.4.6 → panpath-0.4.8}/panpath/s3_path.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: panpath
|
|
3
|
-
Version: 0.4.
|
|
3
|
+
Version: 0.4.8
|
|
4
4
|
Summary: Universal sync/async local/cloud path library with pathlib-compatible interface
|
|
5
5
|
License: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -32,7 +32,7 @@ Requires-Dist: aioboto3 (>=11.0.0) ; extra == "async-s3" or extra == "all-async"
|
|
|
32
32
|
Requires-Dist: aiofiles (>=23.0.0) ; extra == "async-s3" or extra == "async-gs" or extra == "async-azure" or extra == "all-async" or extra == "all"
|
|
33
33
|
Requires-Dist: azure-storage-blob (>=12.0.0) ; extra == "azure" or extra == "async-azure" or extra == "all-sync" or extra == "all-async" or extra == "all"
|
|
34
34
|
Requires-Dist: boto3 (>=1.20.0) ; extra == "s3" or extra == "all-sync" or extra == "all"
|
|
35
|
-
Requires-Dist: gcloud-aio-storage (>=
|
|
35
|
+
Requires-Dist: gcloud-aio-storage (>=9.5) ; extra == "async-gs" or extra == "all-async" or extra == "all"
|
|
36
36
|
Requires-Dist: google-cloud-storage (>=2.0.0) ; extra == "gs" or extra == "all-sync" or extra == "all"
|
|
37
37
|
Requires-Dist: typing-extensions (>=4.0.0)
|
|
38
38
|
Project-URL: Documentation, https://github.com/pwwang/panpath#readme
|
|
@@ -301,6 +301,13 @@ class AsyncAzureBlobClient(AsyncClient):
|
|
|
301
301
|
|
|
302
302
|
Note: For better streaming support, use a_open() instead.
|
|
303
303
|
This method returns a file-like object that supports the standard file API.
|
|
304
|
+
|
|
305
|
+
Args:
|
|
306
|
+
path: Azure path
|
|
307
|
+
mode: File mode
|
|
308
|
+
encoding: Text encoding
|
|
309
|
+
**kwargs: Additional arguments (chunk_size, upload_warning_threshold,
|
|
310
|
+
upload_interval supported)
|
|
304
311
|
"""
|
|
305
312
|
if mode not in ("r", "rb", "w", "wb", "a", "ab"):
|
|
306
313
|
raise ValueError(f"Unsupported mode '{mode}'. Use 'r', 'rb', 'w', 'wb', 'a', or 'ab'.")
|
|
@@ -519,7 +526,7 @@ class AsyncAzureBlobClient(AsyncClient):
|
|
|
519
526
|
|
|
520
527
|
# Yield each directory tuple
|
|
521
528
|
for d, (subdirs, files) in sorted(dirs.items()):
|
|
522
|
-
yield (d, sorted(subdirs), sorted(files))
|
|
529
|
+
yield (d, sorted(subdirs), sorted(filter(None, files)))
|
|
523
530
|
|
|
524
531
|
async def touch( # type: ignore[no-untyped-def, override]
|
|
525
532
|
self,
|
|
@@ -180,7 +180,15 @@ class AzureBlobClient(SyncClient):
|
|
|
180
180
|
encoding: Optional[str] = None,
|
|
181
181
|
**kwargs: Any,
|
|
182
182
|
) -> SyncFileHandle:
|
|
183
|
-
"""Open Azure blob for reading/writing.
|
|
183
|
+
"""Open Azure blob for reading/writing.
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
path: Azure path
|
|
187
|
+
mode: File mode
|
|
188
|
+
encoding: Text encoding
|
|
189
|
+
**kwargs: Additional arguments (chunk_size, upload_warning_threshold,
|
|
190
|
+
upload_interval supported)
|
|
191
|
+
"""
|
|
184
192
|
if mode not in ("r", "rb", "w", "wb", "a", "ab"):
|
|
185
193
|
raise ValueError(f"Unsupported mode '{mode}'. Use 'r', 'rb', 'w', 'wb', 'a', or 'ab'.")
|
|
186
194
|
|
|
@@ -386,7 +394,7 @@ class AzureBlobClient(SyncClient):
|
|
|
386
394
|
|
|
387
395
|
# Yield each directory tuple
|
|
388
396
|
for d, (subdirs, files) in sorted(dirs.items()):
|
|
389
|
-
yield (d, sorted(subdirs), sorted(files))
|
|
397
|
+
yield (d, sorted(subdirs), sorted(filter(None, files)))
|
|
390
398
|
|
|
391
399
|
def touch( # type: ignore[no-untyped-def, override]
|
|
392
400
|
self,
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
import os
|
|
4
4
|
import re
|
|
5
5
|
import sys
|
|
6
|
-
from abc import abstractmethod
|
|
7
6
|
from pathlib import Path as PathlibPath, PurePosixPath
|
|
8
7
|
from typing import TYPE_CHECKING, Any, AsyncGenerator, List, Union
|
|
9
8
|
|
|
@@ -123,32 +122,31 @@ class PanPath(PathlibPath):
|
|
|
123
122
|
except KeyError:
|
|
124
123
|
raise ValueError(f"Unsupported URI scheme: {scheme!r}")
|
|
125
124
|
|
|
126
|
-
|
|
127
|
-
|
|
125
|
+
# These methods are used for IDE type hinting and documentation generation.
|
|
126
|
+
# Actual implementations are in LocalPath and CloudPath subclasses and
|
|
127
|
+
# are routed via __new__.
|
|
128
|
+
async def a_resolve(self) -> "PanPath": # type: ignore[empty-body]
|
|
128
129
|
"""Resolve to absolute path (no-op for cloud paths).
|
|
129
130
|
|
|
130
131
|
Returns:
|
|
131
132
|
Self (cloud paths are already absolute)
|
|
132
133
|
"""
|
|
133
134
|
|
|
134
|
-
|
|
135
|
-
async def a_exists(self) -> bool:
|
|
135
|
+
async def a_exists(self) -> bool: # type: ignore[empty-body]
|
|
136
136
|
"""Asynchronously check if the path exists.
|
|
137
137
|
|
|
138
138
|
Returns:
|
|
139
139
|
True if the path exists, False otherwise.
|
|
140
140
|
"""
|
|
141
141
|
|
|
142
|
-
|
|
143
|
-
async def a_read_bytes(self) -> bytes:
|
|
142
|
+
async def a_read_bytes(self) -> bytes: # type: ignore[empty-body]
|
|
144
143
|
"""Asynchronously read the file's bytes.
|
|
145
144
|
|
|
146
145
|
Returns:
|
|
147
146
|
File content as bytes.
|
|
148
147
|
"""
|
|
149
148
|
|
|
150
|
-
|
|
151
|
-
async def a_read_text(self, encoding: str = "utf-8") -> str:
|
|
149
|
+
async def a_read_text(self, encoding: str = "utf-8") -> str: # type: ignore[empty-body]
|
|
152
150
|
"""Asynchronously read the file's text content.
|
|
153
151
|
|
|
154
152
|
Args:
|
|
@@ -158,7 +156,6 @@ class PanPath(PathlibPath):
|
|
|
158
156
|
File content as string.
|
|
159
157
|
"""
|
|
160
158
|
|
|
161
|
-
@abstractmethod
|
|
162
159
|
async def a_write_bytes(self, data: bytes) -> Union[int, None]:
|
|
163
160
|
"""Asynchronously write bytes to the file.
|
|
164
161
|
|
|
@@ -169,8 +166,11 @@ class PanPath(PathlibPath):
|
|
|
169
166
|
Number of bytes written. For some cloud paths, may return None.
|
|
170
167
|
"""
|
|
171
168
|
|
|
172
|
-
|
|
173
|
-
|
|
169
|
+
async def a_write_text( # type: ignore[empty-body]
|
|
170
|
+
self,
|
|
171
|
+
data: str,
|
|
172
|
+
encoding: str = "utf-8",
|
|
173
|
+
) -> int:
|
|
174
174
|
"""Asynchronously write text to the file.
|
|
175
175
|
|
|
176
176
|
Args:
|
|
@@ -181,7 +181,6 @@ class PanPath(PathlibPath):
|
|
|
181
181
|
Number of characters written.
|
|
182
182
|
"""
|
|
183
183
|
|
|
184
|
-
@abstractmethod
|
|
185
184
|
async def a_unlink(self, missing_ok: bool = False) -> None:
|
|
186
185
|
"""Asynchronously remove (delete) the file or empty directory.
|
|
187
186
|
|
|
@@ -189,39 +188,37 @@ class PanPath(PathlibPath):
|
|
|
189
188
|
missing_ok: If True, does not raise an error if the file does not exist.
|
|
190
189
|
"""
|
|
191
190
|
|
|
192
|
-
|
|
193
|
-
async def a_iterdir(self) -> AsyncGenerator["PanPath", None]:
|
|
191
|
+
async def a_iterdir(self) -> AsyncGenerator["PanPath", None]: # type: ignore[empty-body]
|
|
194
192
|
"""Asynchronously iterate over directory contents.
|
|
195
193
|
|
|
196
194
|
Yields:
|
|
197
195
|
PanPath instances for each item in the directory.
|
|
198
196
|
"""
|
|
199
197
|
|
|
200
|
-
|
|
201
|
-
async def a_is_dir(self) -> bool:
|
|
198
|
+
async def a_is_dir(self) -> bool: # type: ignore[empty-body]
|
|
202
199
|
"""Asynchronously check if the path is a directory.
|
|
203
200
|
|
|
204
201
|
Returns:
|
|
205
202
|
True if the path is a directory, False otherwise.
|
|
206
203
|
"""
|
|
207
204
|
|
|
208
|
-
|
|
209
|
-
async def a_is_file(self) -> bool:
|
|
205
|
+
async def a_is_file(self) -> bool: # type: ignore[empty-body]
|
|
210
206
|
"""Asynchronously check if the path is a file.
|
|
211
207
|
|
|
212
208
|
Returns:
|
|
213
209
|
True if the path is a file, False otherwise.
|
|
214
210
|
"""
|
|
215
211
|
|
|
216
|
-
|
|
217
|
-
|
|
212
|
+
async def a_stat( # type: ignore[empty-body]
|
|
213
|
+
self,
|
|
214
|
+
follow_symlinks: bool = True,
|
|
215
|
+
) -> os.stat_result:
|
|
218
216
|
"""Asynchronously get the file or directory's status information.
|
|
219
217
|
|
|
220
218
|
Returns:
|
|
221
219
|
An object containing file status information (platform-dependent).
|
|
222
220
|
"""
|
|
223
221
|
|
|
224
|
-
@abstractmethod
|
|
225
222
|
async def a_mkdir(
|
|
226
223
|
self,
|
|
227
224
|
mode: int = 0o777,
|
|
@@ -236,8 +233,10 @@ class PanPath(PathlibPath):
|
|
|
236
233
|
exist_ok: If True, does not raise an error if the directory already exists.
|
|
237
234
|
"""
|
|
238
235
|
|
|
239
|
-
|
|
240
|
-
|
|
236
|
+
async def a_glob( # type: ignore[empty-body]
|
|
237
|
+
self,
|
|
238
|
+
pattern: str,
|
|
239
|
+
) -> AsyncGenerator["PanPath", None]:
|
|
241
240
|
"""Asynchronously yield paths matching a glob pattern.
|
|
242
241
|
|
|
243
242
|
Args:
|
|
@@ -247,8 +246,10 @@ class PanPath(PathlibPath):
|
|
|
247
246
|
List of PanPath instances matching the pattern.
|
|
248
247
|
"""
|
|
249
248
|
|
|
250
|
-
|
|
251
|
-
|
|
249
|
+
async def a_rglob( # type: ignore[empty-body]
|
|
250
|
+
self,
|
|
251
|
+
pattern: str,
|
|
252
|
+
) -> AsyncGenerator["PanPath", None]:
|
|
252
253
|
"""Asynchronously yield paths matching a recursive glob pattern.
|
|
253
254
|
|
|
254
255
|
Args:
|
|
@@ -258,16 +259,20 @@ class PanPath(PathlibPath):
|
|
|
258
259
|
List of PanPath instances matching the pattern.
|
|
259
260
|
"""
|
|
260
261
|
|
|
261
|
-
|
|
262
|
-
|
|
262
|
+
async def a_walk( # type: ignore[empty-body]
|
|
263
|
+
self,
|
|
264
|
+
) -> AsyncGenerator[tuple["PanPath", List[str], List[str]], None]:
|
|
263
265
|
"""Asynchronously walk the directory tree.
|
|
264
266
|
|
|
265
267
|
Yields:
|
|
266
268
|
Tuples of (current_path, dirnames, filenames) at each level.
|
|
267
269
|
"""
|
|
268
270
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
+
async def a_touch(
|
|
272
|
+
self,
|
|
273
|
+
mode: int = 0o666,
|
|
274
|
+
exist_ok: bool = True,
|
|
275
|
+
) -> None:
|
|
271
276
|
"""Asynchronously create the file if it does not exist.
|
|
272
277
|
|
|
273
278
|
Args:
|
|
@@ -275,8 +280,10 @@ class PanPath(PathlibPath):
|
|
|
275
280
|
exist_ok: If False, raises an error if the file already exists.
|
|
276
281
|
"""
|
|
277
282
|
|
|
278
|
-
|
|
279
|
-
|
|
283
|
+
async def a_rename( # type: ignore[empty-body]
|
|
284
|
+
self,
|
|
285
|
+
target: Union[str, "PathlibPath"],
|
|
286
|
+
) -> "PanPath":
|
|
280
287
|
"""Asynchronously rename this path to the target path.
|
|
281
288
|
|
|
282
289
|
Args:
|
|
@@ -286,8 +293,10 @@ class PanPath(PathlibPath):
|
|
|
286
293
|
The renamed PanPath instance.
|
|
287
294
|
"""
|
|
288
295
|
|
|
289
|
-
|
|
290
|
-
|
|
296
|
+
async def a_replace( # type: ignore[empty-body]
|
|
297
|
+
self,
|
|
298
|
+
target: Union[str, "PathlibPath"],
|
|
299
|
+
) -> "PanPath":
|
|
291
300
|
"""Asynchronously replace this path with the target path.
|
|
292
301
|
|
|
293
302
|
Args:
|
|
@@ -297,12 +306,10 @@ class PanPath(PathlibPath):
|
|
|
297
306
|
The replaced PanPath instance.
|
|
298
307
|
"""
|
|
299
308
|
|
|
300
|
-
@abstractmethod
|
|
301
309
|
async def a_rmdir(self) -> None:
|
|
302
310
|
"""Asynchronously remove the directory and its contents recursively."""
|
|
303
311
|
|
|
304
|
-
|
|
305
|
-
async def a_is_symlink(self) -> bool:
|
|
312
|
+
async def a_is_symlink(self) -> bool: # type: ignore[empty-body]
|
|
306
313
|
"""Asynchronously check if the path is a symbolic link.
|
|
307
314
|
|
|
308
315
|
For local path, this checks if the path is a symlink.
|
|
@@ -314,8 +321,7 @@ class PanPath(PathlibPath):
|
|
|
314
321
|
True if the path is a symlink, False otherwise.
|
|
315
322
|
"""
|
|
316
323
|
|
|
317
|
-
|
|
318
|
-
async def a_readlink(self) -> "PanPath":
|
|
324
|
+
async def a_readlink(self) -> "PanPath": # type: ignore[empty-body]
|
|
319
325
|
"""Asynchronously read the target of the symbolic link.
|
|
320
326
|
|
|
321
327
|
For local path, this reads the symlink target.
|
|
@@ -325,7 +331,6 @@ class PanPath(PathlibPath):
|
|
|
325
331
|
The target PanPath of the symlink.
|
|
326
332
|
"""
|
|
327
333
|
|
|
328
|
-
@abstractmethod
|
|
329
334
|
async def a_symlink_to(
|
|
330
335
|
self,
|
|
331
336
|
target: Union[str, "PathlibPath"],
|
|
@@ -341,7 +346,6 @@ class PanPath(PathlibPath):
|
|
|
341
346
|
target_is_directory: Whether the target is a directory (ignored for cloud paths).
|
|
342
347
|
"""
|
|
343
348
|
|
|
344
|
-
@abstractmethod
|
|
345
349
|
async def a_rmtree(self, ignore_errors: bool = False, onerror: Any = None) -> None:
|
|
346
350
|
"""Asynchronously remove the directory and all its contents recursively.
|
|
347
351
|
|
|
@@ -350,8 +354,10 @@ class PanPath(PathlibPath):
|
|
|
350
354
|
onerror: Optional function to call on errors.
|
|
351
355
|
"""
|
|
352
356
|
|
|
353
|
-
|
|
354
|
-
|
|
357
|
+
async def a_copy( # type: ignore[empty-body]
|
|
358
|
+
self,
|
|
359
|
+
target: Union[str, "PathlibPath"],
|
|
360
|
+
) -> "PanPath":
|
|
355
361
|
"""Asynchronously copy this path to the target path.
|
|
356
362
|
|
|
357
363
|
Args:
|
|
@@ -361,8 +367,7 @@ class PanPath(PathlibPath):
|
|
|
361
367
|
The copied PanPath instance.
|
|
362
368
|
"""
|
|
363
369
|
|
|
364
|
-
|
|
365
|
-
async def a_copytree(
|
|
370
|
+
async def a_copytree( # type: ignore[empty-body]
|
|
366
371
|
self,
|
|
367
372
|
target: Union[str, "PathlibPath"],
|
|
368
373
|
follow_symlinks: bool = True,
|
|
@@ -377,8 +382,12 @@ class PanPath(PathlibPath):
|
|
|
377
382
|
The copied PanPath instance.
|
|
378
383
|
"""
|
|
379
384
|
|
|
380
|
-
|
|
381
|
-
|
|
385
|
+
def a_open( # type: ignore[empty-body]
|
|
386
|
+
self,
|
|
387
|
+
mode: str = "r",
|
|
388
|
+
encoding: str = "utf-8",
|
|
389
|
+
**kwargs: Any,
|
|
390
|
+
) -> "AsyncFileHandle":
|
|
382
391
|
"""Asynchronously open the file and return an async file handle.
|
|
383
392
|
|
|
384
393
|
Args:
|
|
@@ -389,3 +398,11 @@ class PanPath(PathlibPath):
|
|
|
389
398
|
Returns:
|
|
390
399
|
An async file handle.
|
|
391
400
|
"""
|
|
401
|
+
|
|
402
|
+
# backports
|
|
403
|
+
def walk(self) -> Any:
|
|
404
|
+
"""Walk the directory tree.
|
|
405
|
+
|
|
406
|
+
Yields:
|
|
407
|
+
Tuples of (current_path, dirnames, filenames) at each level.
|
|
408
|
+
"""
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"""Base client classes for sync and async cloud storage operations."""
|
|
2
2
|
|
|
3
3
|
from abc import ABC, abstractmethod
|
|
4
|
+
import asyncio
|
|
5
|
+
import time
|
|
4
6
|
from typing import (
|
|
5
7
|
Any,
|
|
6
8
|
AsyncGenerator,
|
|
@@ -368,6 +370,7 @@ class AsyncFileHandle(ABC):
|
|
|
368
370
|
encoding: Optional[str] = None,
|
|
369
371
|
chunk_size: int = 4096,
|
|
370
372
|
upload_warning_threshold: int = 100,
|
|
373
|
+
upload_interval: float = 1.0,
|
|
371
374
|
):
|
|
372
375
|
"""Initialize async file handle.
|
|
373
376
|
|
|
@@ -381,6 +384,8 @@ class AsyncFileHandle(ABC):
|
|
|
381
384
|
chunk_size: Size of chunks to read
|
|
382
385
|
upload_warning_threshold: Number of chunk uploads before warning (default: 100)
|
|
383
386
|
-1 to disable warning
|
|
387
|
+
upload_interval: Minimum interval (in seconds) between uploads to avoid
|
|
388
|
+
rate limits (default: 1.0)
|
|
384
389
|
"""
|
|
385
390
|
self._client_factory = client_factory
|
|
386
391
|
self._client: Optional[AsyncClient] = None
|
|
@@ -394,6 +399,8 @@ class AsyncFileHandle(ABC):
|
|
|
394
399
|
self._upload_warning_threshold = upload_warning_threshold
|
|
395
400
|
self._upload_count = 0
|
|
396
401
|
self._first_write = True # Track if this is the first write (for 'w' mode clearing)
|
|
402
|
+
self._upload_interval = upload_interval
|
|
403
|
+
self._last_upload_time: Optional[float] = None
|
|
397
404
|
|
|
398
405
|
# For write modes
|
|
399
406
|
self._write_buffer: Union[bytearray, List[str]] = bytearray() if "b" in mode else []
|
|
@@ -453,7 +460,14 @@ class AsyncFileHandle(ABC):
|
|
|
453
460
|
else:
|
|
454
461
|
data = "".join(self._write_buffer) # type: ignore
|
|
455
462
|
|
|
463
|
+
# Rate limiting: wait if needed to respect upload_interval
|
|
464
|
+
if self._upload_interval > 0 and self._last_upload_time is not None:
|
|
465
|
+
elapsed = time.time() - self._last_upload_time
|
|
466
|
+
if elapsed < self._upload_interval:
|
|
467
|
+
await asyncio.sleep(self._upload_interval - elapsed)
|
|
468
|
+
|
|
456
469
|
await self._upload(data)
|
|
470
|
+
self._last_upload_time = time.time()
|
|
457
471
|
self._write_buffer = bytearray() if self._is_binary else []
|
|
458
472
|
|
|
459
473
|
# Track upload count and warn if threshold exceeded
|
|
@@ -763,6 +777,7 @@ class SyncFileHandle(ABC):
|
|
|
763
777
|
encoding: Optional[str] = None,
|
|
764
778
|
chunk_size: int = 4096,
|
|
765
779
|
upload_warning_threshold: int = 100,
|
|
780
|
+
upload_interval: float = 1.0,
|
|
766
781
|
):
|
|
767
782
|
"""Initialize sync file handle.
|
|
768
783
|
|
|
@@ -775,6 +790,8 @@ class SyncFileHandle(ABC):
|
|
|
775
790
|
encoding: Text encoding (for text modes)
|
|
776
791
|
chunk_size: Size of chunks to read
|
|
777
792
|
upload_warning_threshold: Number of chunk uploads before warning (default: 100)
|
|
793
|
+
upload_interval: Minimum interval (in seconds) between uploads to avoid
|
|
794
|
+
rate limits (default: 1.0)
|
|
778
795
|
"""
|
|
779
796
|
self._client = client
|
|
780
797
|
self._bucket = bucket
|
|
@@ -787,6 +804,8 @@ class SyncFileHandle(ABC):
|
|
|
787
804
|
self._upload_warning_threshold = upload_warning_threshold
|
|
788
805
|
self._upload_count = 0
|
|
789
806
|
self._first_write = True # Track if this is the first write (for 'w' mode clearing)
|
|
807
|
+
self._upload_interval = upload_interval
|
|
808
|
+
self._last_upload_time: Optional[float] = None
|
|
790
809
|
|
|
791
810
|
# For write modes
|
|
792
811
|
self._write_buffer: Union[bytearray, List[str]] = bytearray() if "b" in mode else []
|
|
@@ -836,7 +855,14 @@ class SyncFileHandle(ABC):
|
|
|
836
855
|
else:
|
|
837
856
|
data = "".join(self._write_buffer) # type: ignore
|
|
838
857
|
|
|
858
|
+
# Rate limiting: wait if needed to respect upload_interval
|
|
859
|
+
if self._upload_interval > 0 and self._last_upload_time is not None:
|
|
860
|
+
elapsed = time.time() - self._last_upload_time
|
|
861
|
+
if elapsed < self._upload_interval:
|
|
862
|
+
time.sleep(self._upload_interval - elapsed)
|
|
863
|
+
|
|
839
864
|
self._upload(data)
|
|
865
|
+
self._last_upload_time = time.time()
|
|
840
866
|
self._write_buffer = bytearray() if self._is_binary else []
|
|
841
867
|
|
|
842
868
|
# Track upload count and warn if threshold exceeded
|
|
@@ -322,9 +322,12 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
322
322
|
target_str = str(target)
|
|
323
323
|
# Check if cross-storage operation (cloud <-> local or cloud <-> cloud)
|
|
324
324
|
if self._is_cross_storage_op(str(self), target_str): # pragma: no cover
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
325
|
+
if self.is_dir():
|
|
326
|
+
self._copytree_cross_storage(str(self), target_str)
|
|
327
|
+
self.rmtree()
|
|
328
|
+
else:
|
|
329
|
+
self._copy_cross_storage(str(self), target_str)
|
|
330
|
+
self.unlink()
|
|
328
331
|
else:
|
|
329
332
|
# Same storage, use native rename
|
|
330
333
|
self.client.rename(str(self), target_str)
|
|
@@ -469,37 +472,58 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
469
472
|
|
|
470
473
|
@staticmethod
|
|
471
474
|
def _copy_cross_storage(
|
|
472
|
-
src: str,
|
|
475
|
+
src: str,
|
|
476
|
+
dst: str,
|
|
477
|
+
follow_symlinks: bool = True,
|
|
478
|
+
chunk_size: int = 1024 * 1024,
|
|
473
479
|
) -> None: # pragma: no cover
|
|
474
|
-
"""Copy file across storage boundaries.
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
480
|
+
"""Copy file across storage boundaries.
|
|
481
|
+
|
|
482
|
+
Args:
|
|
483
|
+
src: Source path
|
|
484
|
+
dst: Destination path
|
|
485
|
+
follow_symlinks: If False, copy symlink as symlink
|
|
486
|
+
chunk_size: Size of chunks to read/write (for large files)
|
|
487
|
+
"""
|
|
488
|
+
src_path = PanPath(src)
|
|
489
|
+
dst_path = PanPath(dst)
|
|
490
|
+
|
|
491
|
+
if follow_symlinks and src_path.is_symlink():
|
|
492
|
+
# If following symlinks, read the target and copy that instead
|
|
493
|
+
src_path = src_path.readlink()
|
|
494
|
+
|
|
495
|
+
with src_path.open("rb") as src_file, dst_path.open("wb") as dst_file:
|
|
496
|
+
while True:
|
|
497
|
+
chunk = src_file.read(chunk_size)
|
|
498
|
+
if not chunk:
|
|
499
|
+
break
|
|
500
|
+
dst_file.write(chunk)
|
|
487
501
|
|
|
488
502
|
@staticmethod
|
|
489
503
|
def _copytree_cross_storage(
|
|
490
|
-
src: str,
|
|
504
|
+
src: str,
|
|
505
|
+
dst: str,
|
|
506
|
+
follow_symlinks: bool = True,
|
|
507
|
+
chunk_size: int = 1024 * 1024,
|
|
491
508
|
) -> None: # pragma: no cover
|
|
492
|
-
"""Copy directory tree across storage boundaries.
|
|
493
|
-
|
|
494
|
-
|
|
509
|
+
"""Copy directory tree across storage boundaries.
|
|
510
|
+
|
|
511
|
+
Args:
|
|
512
|
+
src: Source directory path
|
|
513
|
+
dst: Destination directory path
|
|
514
|
+
follow_symlinks: If False, copy symlinks as symlinks
|
|
515
|
+
chunk_size: Size of chunks to read/write (for large files)
|
|
516
|
+
"""
|
|
517
|
+
src_path = PanPath(src)
|
|
518
|
+
dst_path = PanPath(dst)
|
|
495
519
|
|
|
496
520
|
# Create destination directory
|
|
497
521
|
dst_path.mkdir(parents=True, exist_ok=True)
|
|
498
522
|
|
|
499
523
|
# Walk source tree and copy all files
|
|
500
|
-
for dirpath, dirnames, filenames in src_path.walk():
|
|
524
|
+
for dirpath, dirnames, filenames in src_path.walk():
|
|
501
525
|
# Calculate relative path from src
|
|
502
|
-
rel_dir = dirpath[len(str(src)) :].lstrip("/")
|
|
526
|
+
rel_dir = str(dirpath)[len(str(src)) :].lstrip("/")
|
|
503
527
|
|
|
504
528
|
# Create subdirectories in destination
|
|
505
529
|
for dirname in dirnames:
|
|
@@ -508,16 +532,14 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
508
532
|
|
|
509
533
|
# Copy files
|
|
510
534
|
for filename in filenames:
|
|
511
|
-
src_file =
|
|
535
|
+
src_file = dirpath / filename
|
|
512
536
|
dst_file = dst_path / rel_dir / filename if rel_dir else dst_path / filename
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
data = src_file.read_bytes()
|
|
520
|
-
dst_file.write_bytes(data)
|
|
537
|
+
CloudPath._copy_cross_storage(
|
|
538
|
+
str(src_file),
|
|
539
|
+
str(dst_file),
|
|
540
|
+
follow_symlinks=follow_symlinks,
|
|
541
|
+
chunk_size=chunk_size,
|
|
542
|
+
)
|
|
521
543
|
|
|
522
544
|
# Async methods (prefixed with a_)
|
|
523
545
|
async def a_exists(self) -> bool:
|
|
@@ -666,7 +688,7 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
666
688
|
|
|
667
689
|
target_str = str(target)
|
|
668
690
|
if not isinstance(target, PanPath): # pragma: no cover
|
|
669
|
-
target = PanPath(target_str) # type: ignore[
|
|
691
|
+
target = PanPath(target_str) # type: ignore[assignment]
|
|
670
692
|
|
|
671
693
|
source_is_dir = await self.a_is_dir()
|
|
672
694
|
target_is_dir = await target.a_is_dir() # type: ignore[union-attr]
|
|
@@ -799,7 +821,7 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
799
821
|
# Same storage, use native copy
|
|
800
822
|
await self.async_client.copy(str(self), target_str, follow_symlinks=follow_symlinks)
|
|
801
823
|
|
|
802
|
-
return PanPath(target_str)
|
|
824
|
+
return PanPath(target_str)
|
|
803
825
|
|
|
804
826
|
async def a_copytree(
|
|
805
827
|
self,
|
|
@@ -831,29 +853,51 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
831
853
|
|
|
832
854
|
@staticmethod
|
|
833
855
|
async def _a_copy_cross_storage(
|
|
834
|
-
src: str,
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
856
|
+
src: str,
|
|
857
|
+
dst: str,
|
|
858
|
+
follow_symlinks: bool = True,
|
|
859
|
+
chunk_size: int = 1024 * 1024,
|
|
860
|
+
) -> None:
|
|
861
|
+
"""Copy file across storage boundaries (async).
|
|
862
|
+
|
|
863
|
+
Args:
|
|
864
|
+
src: Source path
|
|
865
|
+
dst: Destination path
|
|
866
|
+
follow_symlinks: If False, copy symlinks as symlinks
|
|
867
|
+
chunk_size: Size of chunks to read/write (default 1MB)
|
|
868
|
+
"""
|
|
869
|
+
src_path = PanPath(src)
|
|
870
|
+
dst_path = PanPath(dst)
|
|
871
|
+
|
|
872
|
+
if follow_symlinks and await src_path.a_is_symlink(): # pragma: no cover
|
|
873
|
+
# If following symlinks, read the target and copy that instead
|
|
874
|
+
src_path = await src_path.a_readlink()
|
|
875
|
+
|
|
876
|
+
async with src_path.a_open("rb") as fsrc:
|
|
877
|
+
async with dst_path.a_open("wb") as fdst:
|
|
878
|
+
while True:
|
|
879
|
+
buf = await fsrc.read(chunk_size) # Read in 1MB chunks
|
|
880
|
+
if not buf:
|
|
881
|
+
break
|
|
882
|
+
await fdst.write(buf)
|
|
849
883
|
|
|
850
884
|
@staticmethod
|
|
851
885
|
async def _a_copytree_cross_storage(
|
|
852
|
-
src: str,
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
886
|
+
src: str,
|
|
887
|
+
dst: str,
|
|
888
|
+
follow_symlinks: bool = True,
|
|
889
|
+
chunk_size: int = 1024 * 1024,
|
|
890
|
+
) -> None:
|
|
891
|
+
"""Copy directory tree across storage boundaries (async).
|
|
892
|
+
|
|
893
|
+
Args:
|
|
894
|
+
src: Source path
|
|
895
|
+
dst: Destination path
|
|
896
|
+
follow_symlinks: If False, symlinks are copied as symlinks (not dereferenced)
|
|
897
|
+
chunk_size: Size of chunks to read/write (default 1MB)
|
|
898
|
+
"""
|
|
899
|
+
src_path = PanPath(src)
|
|
900
|
+
dst_path = PanPath(dst)
|
|
857
901
|
|
|
858
902
|
# Create destination directory
|
|
859
903
|
await dst_path.a_mkdir(parents=True, exist_ok=True)
|
|
@@ -872,14 +916,12 @@ class CloudPath(PanPath, PurePosixPath, ABC):
|
|
|
872
916
|
for filename in filenames:
|
|
873
917
|
src_file = dirpath / filename
|
|
874
918
|
dst_file = dst_path / rel_dir / filename if rel_dir else dst_path / filename
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
data = await src_file.a_read_bytes()
|
|
882
|
-
await dst_file.a_write_bytes(data)
|
|
919
|
+
await CloudPath._a_copy_cross_storage(
|
|
920
|
+
str(src_file),
|
|
921
|
+
str(dst_file),
|
|
922
|
+
follow_symlinks=follow_symlinks,
|
|
923
|
+
chunk_size=chunk_size,
|
|
924
|
+
)
|
|
883
925
|
|
|
884
926
|
def a_open(
|
|
885
927
|
self,
|
|
@@ -322,7 +322,8 @@ class AsyncGSClient(AsyncClient):
|
|
|
322
322
|
path: GCS path (gs://bucket/blob)
|
|
323
323
|
mode: File mode ('r', 'w', 'rb', 'wb', 'a', 'ab')
|
|
324
324
|
encoding: Text encoding (for text modes)
|
|
325
|
-
**kwargs: Additional arguments (
|
|
325
|
+
**kwargs: Additional arguments (chunk_size, upload_warning_threshold,
|
|
326
|
+
upload_interval supported)
|
|
326
327
|
|
|
327
328
|
Returns:
|
|
328
329
|
GSAsyncFileHandle with streaming support
|
|
@@ -570,7 +571,7 @@ class AsyncGSClient(AsyncClient):
|
|
|
570
571
|
|
|
571
572
|
# Yield each directory tuple
|
|
572
573
|
for d, (subdirs, files) in sorted(dirs.items()):
|
|
573
|
-
yield (d, sorted(subdirs), sorted(files))
|
|
574
|
+
yield (d, sorted(subdirs), sorted(filter(None, files)))
|
|
574
575
|
|
|
575
576
|
async def touch( # type: ignore[no-untyped-def, override]
|
|
576
577
|
self,
|
|
@@ -813,19 +814,23 @@ class GSAsyncFileHandle(AsyncFileHandle):
|
|
|
813
814
|
await storage.upload(self._bucket, self._blob, data)
|
|
814
815
|
else:
|
|
815
816
|
# Upload new data to a temporary blob
|
|
816
|
-
|
|
817
|
+
# Use upload count to ensure unique temp blob names across multiple flushes
|
|
818
|
+
temp_blob = f"{self._blob}.tmp.{os.getpid()}.{self._upload_count}"
|
|
817
819
|
await storage.upload(
|
|
818
820
|
self._bucket,
|
|
819
821
|
temp_blob,
|
|
820
822
|
data,
|
|
821
823
|
)
|
|
822
824
|
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
825
|
+
try:
|
|
826
|
+
# Use compose API to concatenate original + new data
|
|
827
|
+
await storage.compose(
|
|
828
|
+
bucket=self._bucket,
|
|
829
|
+
object_name=self._blob,
|
|
830
|
+
source_object_names=[self._blob, temp_blob],
|
|
831
|
+
)
|
|
832
|
+
except Exception as e: # pragma: no cover
|
|
833
|
+
raise IOError(f"Failed to append data to GCS blob: {self._blob}") from e
|
|
834
|
+
finally:
|
|
835
|
+
# Clean up the temporary blob
|
|
836
|
+
await storage.delete(self._bucket, temp_blob)
|
|
@@ -176,7 +176,8 @@ class GSClient(SyncClient):
|
|
|
176
176
|
path: GCS path (gs://bucket/blob)
|
|
177
177
|
mode: File mode ('r', 'w', 'rb', 'wb', 'a', 'ab')
|
|
178
178
|
encoding: Text encoding (for text modes)
|
|
179
|
-
**kwargs: Additional arguments (chunk_size
|
|
179
|
+
**kwargs: Additional arguments (chunk_size, upload_warning_threshold,
|
|
180
|
+
upload_interval supported)
|
|
180
181
|
|
|
181
182
|
Returns:
|
|
182
183
|
GSSyncFileHandle with streaming support
|
|
@@ -405,6 +406,13 @@ class GSClient(SyncClient):
|
|
|
405
406
|
if i < len(parts) - 2: # pragma: no cover
|
|
406
407
|
dirs[dir_path][0].add(parts[i + 1])
|
|
407
408
|
|
|
409
|
+
sub_parent = (
|
|
410
|
+
f"{path}/" + "/".join(parts[: i]) if path else "/".join(parts[: i])
|
|
411
|
+
).rstrip("/")
|
|
412
|
+
if sub_parent not in dirs: # pragma: no cover
|
|
413
|
+
dirs[sub_parent] = (set(), set())
|
|
414
|
+
dirs[sub_parent][0].add(parts[i])
|
|
415
|
+
|
|
408
416
|
# Add file to its parent directory
|
|
409
417
|
parent_dir = f"{path}/" + "/".join(parts[:-1]) if path else "/".join(parts[:-1])
|
|
410
418
|
if parent_dir not in dirs: # pragma: no cover
|
|
@@ -412,7 +420,7 @@ class GSClient(SyncClient):
|
|
|
412
420
|
dirs[parent_dir][1].add(parts[-1])
|
|
413
421
|
|
|
414
422
|
for d, (subdirs, files) in dirs.items():
|
|
415
|
-
yield d, sorted(subdirs), sorted(files)
|
|
423
|
+
yield d, sorted(subdirs), sorted(filter(None, files))
|
|
416
424
|
|
|
417
425
|
def touch(self, path: str, exist_ok: bool = True) -> None:
|
|
418
426
|
"""Create empty file.
|
|
@@ -587,7 +595,7 @@ class GSSyncFileHandle(SyncFileHandle):
|
|
|
587
595
|
super().__init__(*args, **kwargs)
|
|
588
596
|
self._blob: storage.Blob = self._client.bucket(self._bucket).blob(self._blob)
|
|
589
597
|
if self._is_read and not self._blob.exists():
|
|
590
|
-
raise FileNotFoundError(f"GCS blob not found: {self._bucket}/{self._blob}")
|
|
598
|
+
raise FileNotFoundError(f"GCS blob not found: {self._bucket}/{self._blob.name}")
|
|
591
599
|
|
|
592
600
|
@classmethod
|
|
593
601
|
def _expception_as_filenotfound(cls, exception: Exception) -> bool: # pragma: no cover
|
|
@@ -648,14 +656,18 @@ class GSSyncFileHandle(SyncFileHandle):
|
|
|
648
656
|
self._blob.upload_from_string(data)
|
|
649
657
|
else:
|
|
650
658
|
bucket = self._blob.bucket
|
|
651
|
-
|
|
659
|
+
# Use upload count to ensure unique temp blob names across multiple flushes
|
|
660
|
+
temp_blob_name = f"{self._blob.name}.tmp.{os.getpid()}.{self._upload_count}"
|
|
652
661
|
temp_blob = bucket.blob(temp_blob_name)
|
|
653
662
|
|
|
654
663
|
# Upload new data to temp blob
|
|
655
664
|
temp_blob.upload_from_string(data)
|
|
656
665
|
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
#
|
|
661
|
-
|
|
666
|
+
try:
|
|
667
|
+
# Compose: original + temp = original
|
|
668
|
+
self._blob.compose([self._blob, temp_blob])
|
|
669
|
+
except Exception as e: # pragma: no cover
|
|
670
|
+
raise IOError(f"Failed to append data to GCS blob: {self._blob}") from e
|
|
671
|
+
finally:
|
|
672
|
+
# Clean up temp blob
|
|
673
|
+
temp_blob.delete()
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"""Local filesystem path implementation."""
|
|
2
2
|
|
|
3
|
-
from pathlib import Path, PosixPath, WindowsPath
|
|
4
3
|
import os
|
|
4
|
+
import shutil
|
|
5
5
|
import sys
|
|
6
|
-
from
|
|
6
|
+
from pathlib import Path, PosixPath, WindowsPath
|
|
7
|
+
from typing import Any, AsyncGenerator, Iterator, List, Optional, Tuple, Union
|
|
7
8
|
from panpath.base import PanPath
|
|
8
9
|
from panpath.cloud import CloudPath
|
|
9
10
|
from panpath.exceptions import MissingDependencyError
|
|
@@ -70,16 +71,23 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
70
71
|
Returns:
|
|
71
72
|
New path instance
|
|
72
73
|
"""
|
|
73
|
-
if not HAS_AIOFILES:
|
|
74
|
-
raise MissingDependencyError(
|
|
75
|
-
backend="async local paths",
|
|
76
|
-
package="aiofiles",
|
|
77
|
-
extra="all-async",
|
|
78
|
-
)
|
|
79
|
-
|
|
80
74
|
target_str = str(target)
|
|
81
|
-
|
|
82
|
-
|
|
75
|
+
if CloudPath._is_cross_storage_op(str(self), target_str):
|
|
76
|
+
if await self.a_is_dir():
|
|
77
|
+
await CloudPath._a_copytree_cross_storage(str(self), target_str)
|
|
78
|
+
await self.a_rmtree()
|
|
79
|
+
else:
|
|
80
|
+
await CloudPath._a_copy_cross_storage(self, target_str)
|
|
81
|
+
await self.a_unlink()
|
|
82
|
+
else:
|
|
83
|
+
if not HAS_AIOFILES:
|
|
84
|
+
raise MissingDependencyError(
|
|
85
|
+
backend="async local paths",
|
|
86
|
+
package="aiofiles",
|
|
87
|
+
extra="all-async",
|
|
88
|
+
)
|
|
89
|
+
await aiofiles.os.rename(str(self), target_str)
|
|
90
|
+
return PanPath(target_str)
|
|
83
91
|
|
|
84
92
|
async def a_replace(self, target: Union[str, "Path"]) -> "PanPath":
|
|
85
93
|
"""Rename the file or directory to target, overwriting if target exists.
|
|
@@ -111,18 +119,18 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
111
119
|
Returns:
|
|
112
120
|
Target path instance
|
|
113
121
|
"""
|
|
114
|
-
if not HAS_AIOFILES:
|
|
115
|
-
raise MissingDependencyError(
|
|
116
|
-
backend="async local paths",
|
|
117
|
-
package="aiofiles",
|
|
118
|
-
extra="all-async",
|
|
119
|
-
)
|
|
120
122
|
|
|
121
123
|
target_str = str(target)
|
|
122
124
|
# Check if cross-storage operation
|
|
123
|
-
if CloudPath._is_cross_storage_op(str(self), target_str):
|
|
125
|
+
if CloudPath._is_cross_storage_op(str(self), target_str):
|
|
124
126
|
await CloudPath._a_copy_cross_storage(self, target_str, follow_symlinks=follow_symlinks)
|
|
125
127
|
else:
|
|
128
|
+
if not HAS_AIOFILES:
|
|
129
|
+
raise MissingDependencyError(
|
|
130
|
+
backend="async local paths",
|
|
131
|
+
package="aiofiles",
|
|
132
|
+
extra="all-async",
|
|
133
|
+
)
|
|
126
134
|
async with aiofiles.open(str(self), mode="rb") as sf:
|
|
127
135
|
async with aiofiles.open(target_str, mode="wb") as df:
|
|
128
136
|
while True:
|
|
@@ -131,7 +139,7 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
131
139
|
break
|
|
132
140
|
await df.write(chunk)
|
|
133
141
|
|
|
134
|
-
return PanPath(target_str)
|
|
142
|
+
return PanPath(target_str)
|
|
135
143
|
|
|
136
144
|
async def a_copytree(
|
|
137
145
|
self,
|
|
@@ -147,26 +155,28 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
147
155
|
Returns:
|
|
148
156
|
The copied PanPath instance.
|
|
149
157
|
"""
|
|
150
|
-
|
|
151
|
-
raise MissingDependencyError(
|
|
152
|
-
backend="async local paths",
|
|
153
|
-
package="aiofiles",
|
|
154
|
-
extra="all-async",
|
|
155
|
-
)
|
|
158
|
+
target_str = str(target)
|
|
156
159
|
|
|
157
|
-
|
|
158
|
-
|
|
160
|
+
if CloudPath._is_cross_storage_op(str(self), target_str):
|
|
161
|
+
await CloudPath._a_copytree_cross_storage(
|
|
162
|
+
self,
|
|
163
|
+
target_str,
|
|
164
|
+
follow_symlinks=follow_symlinks,
|
|
165
|
+
)
|
|
166
|
+
else:
|
|
167
|
+
target = PanPath(target)
|
|
168
|
+
await target.a_mkdir(parents=True, exist_ok=True)
|
|
159
169
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
170
|
+
async for entry in self.a_iterdir():
|
|
171
|
+
src_path = entry
|
|
172
|
+
dest_path = target / entry.name
|
|
163
173
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
174
|
+
if await src_path.a_is_dir():
|
|
175
|
+
await src_path.a_copytree(dest_path, follow_symlinks=follow_symlinks)
|
|
176
|
+
else:
|
|
177
|
+
await src_path.a_copy(dest_path, follow_symlinks=follow_symlinks)
|
|
168
178
|
|
|
169
|
-
return target
|
|
179
|
+
return PanPath(target)
|
|
170
180
|
|
|
171
181
|
async def a_walk( # type: ignore[override]
|
|
172
182
|
self,
|
|
@@ -207,9 +217,7 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
207
217
|
extra="all-async",
|
|
208
218
|
)
|
|
209
219
|
|
|
210
|
-
return PanPath( # type: ignore[return-value
|
|
211
|
-
await aiofiles.os.readlink(str(self))
|
|
212
|
-
)
|
|
220
|
+
return PanPath(await aiofiles.os.readlink(str(self))) # type: ignore[return-value]
|
|
213
221
|
|
|
214
222
|
async def a_symlink_to(
|
|
215
223
|
self,
|
|
@@ -512,3 +520,112 @@ class LocalPath(_ConcretePath, PanPath): # type: ignore[valid-type, misc]
|
|
|
512
520
|
errors=errors,
|
|
513
521
|
newline=newline,
|
|
514
522
|
)
|
|
523
|
+
|
|
524
|
+
def rename(self, target: Union[str, "Path"]) -> "PanPath": # type: ignore[override]
|
|
525
|
+
"""Rename the file or directory to target.
|
|
526
|
+
|
|
527
|
+
Args:
|
|
528
|
+
target: New path
|
|
529
|
+
|
|
530
|
+
Returns:
|
|
531
|
+
New path instance
|
|
532
|
+
"""
|
|
533
|
+
target_str = str(target)
|
|
534
|
+
if CloudPath._is_cross_storage_op(str(self), target_str):
|
|
535
|
+
if self.is_dir():
|
|
536
|
+
CloudPath._copytree_cross_storage(self, target_str)
|
|
537
|
+
self.rmtree()
|
|
538
|
+
else:
|
|
539
|
+
CloudPath._copy_cross_storage(self, target_str)
|
|
540
|
+
self.unlink()
|
|
541
|
+
else:
|
|
542
|
+
os.rename(str(self), target_str)
|
|
543
|
+
|
|
544
|
+
return PanPath(target_str)
|
|
545
|
+
|
|
546
|
+
def copy(self, target: Union[str, "Path"], follow_symlinks: bool = True) -> "PanPath":
|
|
547
|
+
"""Copy file to target.
|
|
548
|
+
|
|
549
|
+
Can copy between cloud and local paths.
|
|
550
|
+
|
|
551
|
+
Args:
|
|
552
|
+
target: Destination path (can be cloud or local)
|
|
553
|
+
follow_symlinks: If True, follow symbolic links
|
|
554
|
+
|
|
555
|
+
Returns:
|
|
556
|
+
Target path instance
|
|
557
|
+
"""
|
|
558
|
+
target_str = str(target)
|
|
559
|
+
if CloudPath._is_cross_storage_op(str(self), target_str):
|
|
560
|
+
CloudPath._copy_cross_storage(self, target_str, follow_symlinks=follow_symlinks)
|
|
561
|
+
else:
|
|
562
|
+
shutil.copy2(str(self), target_str, follow_symlinks=follow_symlinks)
|
|
563
|
+
|
|
564
|
+
return PanPath(target)
|
|
565
|
+
|
|
566
|
+
def copytree(
|
|
567
|
+
self,
|
|
568
|
+
target: Union[str, "Path"],
|
|
569
|
+
follow_symlinks: bool = True,
|
|
570
|
+
) -> "PanPath":
|
|
571
|
+
"""Recursively copy the directory and all its contents to the target path.
|
|
572
|
+
|
|
573
|
+
Args:
|
|
574
|
+
target: Destination PanPath to copy to.
|
|
575
|
+
follow_symlinks: If True, copies the contents of symlinks.
|
|
576
|
+
|
|
577
|
+
Returns:
|
|
578
|
+
The copied PanPath instance.
|
|
579
|
+
"""
|
|
580
|
+
target_str = str(target)
|
|
581
|
+
if CloudPath._is_cross_storage_op(str(self), target_str):
|
|
582
|
+
CloudPath._copytree_cross_storage(self, target_str, follow_symlinks=follow_symlinks)
|
|
583
|
+
else:
|
|
584
|
+
target = PanPath(target)
|
|
585
|
+
|
|
586
|
+
target.mkdir(parents=True, exist_ok=True)
|
|
587
|
+
|
|
588
|
+
for entry in self.iterdir():
|
|
589
|
+
src_path = entry
|
|
590
|
+
dest_path = target / entry.name
|
|
591
|
+
|
|
592
|
+
if src_path.is_dir():
|
|
593
|
+
src_path.copytree(dest_path, follow_symlinks=follow_symlinks)
|
|
594
|
+
else:
|
|
595
|
+
src_path.copy(dest_path, follow_symlinks=follow_symlinks)
|
|
596
|
+
|
|
597
|
+
return PanPath(target)
|
|
598
|
+
|
|
599
|
+
def rmdir(self) -> None:
|
|
600
|
+
"""Remove empty directory."""
|
|
601
|
+
os.rmdir(str(self))
|
|
602
|
+
|
|
603
|
+
def rmtree(self) -> None:
|
|
604
|
+
"""Recursively remove directory and its contents."""
|
|
605
|
+
shutil.rmtree(str(self))
|
|
606
|
+
|
|
607
|
+
# backports, walk is introduced in Python 3.12
|
|
608
|
+
def walk( # type: ignore[no-untyped-def]
|
|
609
|
+
self,
|
|
610
|
+
*args,
|
|
611
|
+
**kwargs,
|
|
612
|
+
) -> Iterator[Tuple["LocalPath", List[str], List[str]]]:
|
|
613
|
+
"""Walk the directory tree.
|
|
614
|
+
|
|
615
|
+
Returns:
|
|
616
|
+
A list of tuples (dirpath, dirnames, filenames)
|
|
617
|
+
"""
|
|
618
|
+
if sys.version_info >= (3, 12):
|
|
619
|
+
yield from Path.walk(self, *args, **kwargs) # type: ignore[no-untyped-call]
|
|
620
|
+
|
|
621
|
+
if args or kwargs: # pragma: no cover
|
|
622
|
+
raise NotImplementedError(
|
|
623
|
+
"walk() does not accept arguments in this backport."
|
|
624
|
+
)
|
|
625
|
+
else: # pragma: no cover
|
|
626
|
+
for dirpath, dirnames, filenames in os.walk(str(self)):
|
|
627
|
+
yield ( # type: ignore[misc]
|
|
628
|
+
PanPath(dirpath),
|
|
629
|
+
dirnames,
|
|
630
|
+
filenames,
|
|
631
|
+
)
|
|
@@ -298,7 +298,8 @@ class AsyncS3Client(AsyncClient):
|
|
|
298
298
|
path: S3 path (s3://bucket/key)
|
|
299
299
|
mode: File mode ('r', 'w', 'rb', 'wb', 'a', 'ab')
|
|
300
300
|
encoding: Text encoding (for text modes)
|
|
301
|
-
**kwargs: Additional arguments (
|
|
301
|
+
**kwargs: Additional arguments (chunk_size, upload_warning_threshold,
|
|
302
|
+
upload_interval supported)
|
|
302
303
|
|
|
303
304
|
Returns:
|
|
304
305
|
S3AsyncFileHandle with streaming support
|
|
@@ -567,7 +568,7 @@ class AsyncS3Client(AsyncClient):
|
|
|
567
568
|
|
|
568
569
|
# Yield tuples
|
|
569
570
|
for d, (subdirs, files) in sorted(dirs.items()):
|
|
570
|
-
yield (d, sorted(subdirs), sorted(files))
|
|
571
|
+
yield (d, sorted(subdirs), sorted(filter(None, files)))
|
|
571
572
|
|
|
572
573
|
async def touch(self, path: str, exist_ok: bool = True, mode: Optional[int] = None) -> None:
|
|
573
574
|
"""Create empty file.
|
|
@@ -188,7 +188,8 @@ class S3Client(SyncClient):
|
|
|
188
188
|
path: S3 path (s3://bucket/key)
|
|
189
189
|
mode: File mode ('r', 'w', 'rb', 'wb', 'a', 'ab')
|
|
190
190
|
encoding: Text encoding (for text modes)
|
|
191
|
-
**kwargs: Additional arguments (chunk_size
|
|
191
|
+
**kwargs: Additional arguments (chunk_size, upload_warning_threshold,
|
|
192
|
+
upload_interval supported)
|
|
192
193
|
|
|
193
194
|
Returns:
|
|
194
195
|
S3SyncFileHandle with streaming support
|
|
@@ -446,7 +447,7 @@ class S3Client(SyncClient):
|
|
|
446
447
|
dirs[parent_dir][1].add(parts[-1])
|
|
447
448
|
|
|
448
449
|
for d, (subdirs, files) in dirs.items():
|
|
449
|
-
yield d, sorted(subdirs), sorted(files)
|
|
450
|
+
yield d, sorted(subdirs), sorted(filter(None, files))
|
|
450
451
|
|
|
451
452
|
def touch(self, path: str, exist_ok: bool = True) -> None:
|
|
452
453
|
"""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.4.
|
|
7
|
+
version = "0.4.8"
|
|
8
8
|
description = "Universal sync/async local/cloud path library with pathlib-compatible interface"
|
|
9
9
|
authors = ["panpath contributors"]
|
|
10
10
|
readme = "README.md"
|
|
@@ -40,7 +40,7 @@ azure-storage-blob = {version = ">=12.0.0", optional = true}
|
|
|
40
40
|
# Async cloud storage backends
|
|
41
41
|
aioboto3 = {version = ">=11.0.0", optional = true}
|
|
42
42
|
aiofiles = {version = ">=23.0.0", optional = true}
|
|
43
|
-
gcloud-aio-storage = {version = ">=
|
|
43
|
+
gcloud-aio-storage = {version = ">=9.5", optional = true}
|
|
44
44
|
|
|
45
45
|
[tool.poetry.extras]
|
|
46
46
|
s3 = ["boto3"]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|