panpath 0.1.0__tar.gz → 0.2.0__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.1.0 → panpath-0.2.0}/PKG-INFO +31 -33
- {panpath-0.1.0 → panpath-0.2.0}/README.md +30 -32
- panpath-0.2.0/panpath/__init__.py +82 -0
- {panpath-0.1.0 → panpath-0.2.0}/panpath/azure_async_client.py +246 -5
- {panpath-0.1.0 → panpath-0.2.0}/panpath/azure_client.py +1 -1
- panpath-0.2.0/panpath/azure_path.py +26 -0
- panpath-0.2.0/panpath/base.py +125 -0
- panpath-0.2.0/panpath/clients.py +566 -0
- panpath-0.1.0/panpath/base.py → panpath-0.2.0/panpath/cloud.py +172 -322
- {panpath-0.1.0 → panpath-0.2.0}/panpath/exceptions.py +0 -6
- {panpath-0.1.0 → panpath-0.2.0}/panpath/gs_async_client.py +311 -6
- {panpath-0.1.0 → panpath-0.2.0}/panpath/gs_client.py +1 -1
- panpath-0.1.0/panpath/gs_sync.py → panpath-0.2.0/panpath/gs_path.py +9 -3
- panpath-0.2.0/panpath/local_path.py +228 -0
- panpath-0.2.0/panpath/registry.py +66 -0
- {panpath-0.1.0 → panpath-0.2.0}/panpath/s3_async_client.py +269 -5
- {panpath-0.1.0 → panpath-0.2.0}/panpath/s3_client.py +1 -1
- panpath-0.1.0/panpath/s3_sync.py → panpath-0.2.0/panpath/s3_path.py +8 -5
- {panpath-0.1.0 → panpath-0.2.0}/pyproject.toml +2 -3
- panpath-0.1.0/panpath/__init__.py +0 -86
- panpath-0.1.0/panpath/azure_async.py +0 -20
- panpath-0.1.0/panpath/azure_sync.py +0 -20
- panpath-0.1.0/panpath/clients.py +0 -273
- panpath-0.1.0/panpath/gs_async.py +0 -20
- panpath-0.1.0/panpath/local_async.py +0 -160
- panpath-0.1.0/panpath/local_sync.py +0 -29
- panpath-0.1.0/panpath/registry.py +0 -71
- panpath-0.1.0/panpath/router.py +0 -174
- panpath-0.1.0/panpath/s3_async.py +0 -20
- {panpath-0.1.0 → panpath-0.2.0}/LICENSE +0 -0
- {panpath-0.1.0 → panpath-0.2.0}/panpath/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: panpath
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Universal sync/async local/cloud path library with pathlib-compatible interface
|
|
5
5
|
License: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -104,20 +104,20 @@ content = azure_file.read_text()
|
|
|
104
104
|
### Asynchronous Usage
|
|
105
105
|
|
|
106
106
|
```python
|
|
107
|
-
from panpath import PanPath
|
|
107
|
+
from panpath import PanPath
|
|
108
108
|
|
|
109
|
-
#
|
|
110
|
-
|
|
111
|
-
content = await
|
|
109
|
+
# All path classes support async methods with a_ prefix
|
|
110
|
+
s3_path = PanPath("s3://bucket/key/file.txt")
|
|
111
|
+
content = await s3_path.a_read_text()
|
|
112
112
|
|
|
113
|
-
#
|
|
114
|
-
|
|
115
|
-
content = await
|
|
113
|
+
# Works for all cloud storage providers
|
|
114
|
+
gs_path = PanPath("gs://bucket/path/file.txt")
|
|
115
|
+
content = await gs_path.a_read_text()
|
|
116
116
|
|
|
117
117
|
# Async local files
|
|
118
|
-
|
|
119
|
-
async with
|
|
120
|
-
content = await f.
|
|
118
|
+
local_path = PanPath("/path/to/file.txt")
|
|
119
|
+
async with local_path.a_open("r") as f:
|
|
120
|
+
content = await f.a_read()
|
|
121
121
|
```
|
|
122
122
|
|
|
123
123
|
### Path Operations
|
|
@@ -125,15 +125,14 @@ async with async_local.open("r") as f:
|
|
|
125
125
|
```python
|
|
126
126
|
from panpath import PanPath
|
|
127
127
|
|
|
128
|
-
# Path operations preserve type
|
|
128
|
+
# Path operations preserve type
|
|
129
129
|
s3_path = PanPath("s3://bucket/data/file.txt")
|
|
130
|
-
parent = s3_path.parent # Returns S3Path
|
|
131
|
-
sibling = s3_path.parent / "other.txt" # Returns S3Path
|
|
130
|
+
parent = s3_path.parent # Returns S3Path
|
|
131
|
+
sibling = s3_path.parent / "other.txt" # Returns S3Path
|
|
132
132
|
|
|
133
|
-
#
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
async_sibling = async_parent / "other.txt" # Returns AsyncS3Path
|
|
133
|
+
# Each path supports both sync and async methods
|
|
134
|
+
content = s3_path.read_text() # Synchronous
|
|
135
|
+
content = await s3_path.a_read_text() # Asynchronous
|
|
137
136
|
```
|
|
138
137
|
|
|
139
138
|
### Bulk Operations and Cross-Storage Transfers
|
|
@@ -173,24 +172,25 @@ See [bulk-operations.md](docs/guide/bulk-operations.md) for detailed documentati
|
|
|
173
172
|
|
|
174
173
|
## Architecture
|
|
175
174
|
|
|
176
|
-
PanPath uses a
|
|
175
|
+
PanPath uses a factory pattern to dispatch path creation based on URI scheme:
|
|
177
176
|
|
|
178
|
-
-
|
|
179
|
-
-
|
|
177
|
+
- `PanPath(pathlib.Path)` - Root factory and base class
|
|
178
|
+
- `LocalPath(PanPath)` - Local filesystem paths
|
|
179
|
+
- `CloudPath(PanPath)` - Base for all cloud storage paths
|
|
180
|
+
- `GSPath(CloudPath)`, `S3Path(CloudPath)`, `AzurePath(CloudPath)` - Cloud-specific implementations
|
|
180
181
|
|
|
181
|
-
Cloud paths use lazy client instantiation - SDK clients are only created on first I/O operation.
|
|
182
|
+
Each path class provides both synchronous methods and asynchronous methods (prefixed with `a_`). Cloud paths use lazy client instantiation - SDK clients are only created on first I/O operation.
|
|
182
183
|
|
|
183
184
|
## Type Hints
|
|
184
185
|
|
|
185
|
-
PanPath provides comprehensive type hints
|
|
186
|
+
PanPath provides comprehensive type hints:
|
|
186
187
|
|
|
187
188
|
```python
|
|
188
189
|
from panpath import PanPath
|
|
189
|
-
from
|
|
190
|
+
from panpath.s3_path import S3Path
|
|
190
191
|
|
|
191
|
-
# Type checker knows return type based on
|
|
192
|
-
|
|
193
|
-
async_path: AsyncS3Path = PanPath("s3://bucket/key", mode="async")
|
|
192
|
+
# Type checker knows return type based on URI scheme
|
|
193
|
+
path: S3Path = PanPath("s3://bucket/key")
|
|
194
194
|
```
|
|
195
195
|
|
|
196
196
|
## Testing
|
|
@@ -240,17 +240,15 @@ path = PanPath("s3://bucket/key")
|
|
|
240
240
|
# Before
|
|
241
241
|
from aiopath import AsyncPath
|
|
242
242
|
path = AsyncPath("/local/file.txt")
|
|
243
|
+
# await path.read_text()
|
|
243
244
|
|
|
244
245
|
# After
|
|
245
|
-
from panpath import
|
|
246
|
-
path =
|
|
246
|
+
from panpath import PanPath
|
|
247
|
+
path = PanPath("/local/file.txt")
|
|
248
|
+
# await path.a_read_text()
|
|
247
249
|
```
|
|
248
250
|
|
|
249
251
|
## License
|
|
250
252
|
|
|
251
253
|
MIT License - see LICENSE file for details.
|
|
252
254
|
|
|
253
|
-
## Contributing
|
|
254
|
-
|
|
255
|
-
Contributions welcome! Please see CONTRIBUTING.md for guidelines.
|
|
256
|
-
|
|
@@ -62,20 +62,20 @@ content = azure_file.read_text()
|
|
|
62
62
|
### Asynchronous Usage
|
|
63
63
|
|
|
64
64
|
```python
|
|
65
|
-
from panpath import PanPath
|
|
65
|
+
from panpath import PanPath
|
|
66
66
|
|
|
67
|
-
#
|
|
68
|
-
|
|
69
|
-
content = await
|
|
67
|
+
# All path classes support async methods with a_ prefix
|
|
68
|
+
s3_path = PanPath("s3://bucket/key/file.txt")
|
|
69
|
+
content = await s3_path.a_read_text()
|
|
70
70
|
|
|
71
|
-
#
|
|
72
|
-
|
|
73
|
-
content = await
|
|
71
|
+
# Works for all cloud storage providers
|
|
72
|
+
gs_path = PanPath("gs://bucket/path/file.txt")
|
|
73
|
+
content = await gs_path.a_read_text()
|
|
74
74
|
|
|
75
75
|
# Async local files
|
|
76
|
-
|
|
77
|
-
async with
|
|
78
|
-
content = await f.
|
|
76
|
+
local_path = PanPath("/path/to/file.txt")
|
|
77
|
+
async with local_path.a_open("r") as f:
|
|
78
|
+
content = await f.a_read()
|
|
79
79
|
```
|
|
80
80
|
|
|
81
81
|
### Path Operations
|
|
@@ -83,15 +83,14 @@ async with async_local.open("r") as f:
|
|
|
83
83
|
```python
|
|
84
84
|
from panpath import PanPath
|
|
85
85
|
|
|
86
|
-
# Path operations preserve type
|
|
86
|
+
# Path operations preserve type
|
|
87
87
|
s3_path = PanPath("s3://bucket/data/file.txt")
|
|
88
|
-
parent = s3_path.parent # Returns S3Path
|
|
89
|
-
sibling = s3_path.parent / "other.txt" # Returns S3Path
|
|
88
|
+
parent = s3_path.parent # Returns S3Path
|
|
89
|
+
sibling = s3_path.parent / "other.txt" # Returns S3Path
|
|
90
90
|
|
|
91
|
-
#
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
async_sibling = async_parent / "other.txt" # Returns AsyncS3Path
|
|
91
|
+
# Each path supports both sync and async methods
|
|
92
|
+
content = s3_path.read_text() # Synchronous
|
|
93
|
+
content = await s3_path.a_read_text() # Asynchronous
|
|
95
94
|
```
|
|
96
95
|
|
|
97
96
|
### Bulk Operations and Cross-Storage Transfers
|
|
@@ -131,24 +130,25 @@ See [bulk-operations.md](docs/guide/bulk-operations.md) for detailed documentati
|
|
|
131
130
|
|
|
132
131
|
## Architecture
|
|
133
132
|
|
|
134
|
-
PanPath uses a
|
|
133
|
+
PanPath uses a factory pattern to dispatch path creation based on URI scheme:
|
|
135
134
|
|
|
136
|
-
-
|
|
137
|
-
-
|
|
135
|
+
- `PanPath(pathlib.Path)` - Root factory and base class
|
|
136
|
+
- `LocalPath(PanPath)` - Local filesystem paths
|
|
137
|
+
- `CloudPath(PanPath)` - Base for all cloud storage paths
|
|
138
|
+
- `GSPath(CloudPath)`, `S3Path(CloudPath)`, `AzurePath(CloudPath)` - Cloud-specific implementations
|
|
138
139
|
|
|
139
|
-
Cloud paths use lazy client instantiation - SDK clients are only created on first I/O operation.
|
|
140
|
+
Each path class provides both synchronous methods and asynchronous methods (prefixed with `a_`). Cloud paths use lazy client instantiation - SDK clients are only created on first I/O operation.
|
|
140
141
|
|
|
141
142
|
## Type Hints
|
|
142
143
|
|
|
143
|
-
PanPath provides comprehensive type hints
|
|
144
|
+
PanPath provides comprehensive type hints:
|
|
144
145
|
|
|
145
146
|
```python
|
|
146
147
|
from panpath import PanPath
|
|
147
|
-
from
|
|
148
|
+
from panpath.s3_path import S3Path
|
|
148
149
|
|
|
149
|
-
# Type checker knows return type based on
|
|
150
|
-
|
|
151
|
-
async_path: AsyncS3Path = PanPath("s3://bucket/key", mode="async")
|
|
150
|
+
# Type checker knows return type based on URI scheme
|
|
151
|
+
path: S3Path = PanPath("s3://bucket/key")
|
|
152
152
|
```
|
|
153
153
|
|
|
154
154
|
## Testing
|
|
@@ -198,16 +198,14 @@ path = PanPath("s3://bucket/key")
|
|
|
198
198
|
# Before
|
|
199
199
|
from aiopath import AsyncPath
|
|
200
200
|
path = AsyncPath("/local/file.txt")
|
|
201
|
+
# await path.read_text()
|
|
201
202
|
|
|
202
203
|
# After
|
|
203
|
-
from panpath import
|
|
204
|
-
path =
|
|
204
|
+
from panpath import PanPath
|
|
205
|
+
path = PanPath("/local/file.txt")
|
|
206
|
+
# await path.a_read_text()
|
|
205
207
|
```
|
|
206
208
|
|
|
207
209
|
## License
|
|
208
210
|
|
|
209
211
|
MIT License - see LICENSE file for details.
|
|
210
|
-
|
|
211
|
-
## Contributing
|
|
212
|
-
|
|
213
|
-
Contributions welcome! Please see CONTRIBUTING.md for guidelines.
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""PanPath - Universal sync/async local/cloud path library.
|
|
2
|
+
|
|
3
|
+
Examples:
|
|
4
|
+
>>> from panpath import PanPath
|
|
5
|
+
>>>
|
|
6
|
+
>>> # Local path (sync methods)
|
|
7
|
+
>>> path = PanPath("/path/to/file.txt")
|
|
8
|
+
>>> content = path.read_text()
|
|
9
|
+
>>>
|
|
10
|
+
>>> # Local path (async methods with a_ prefix)
|
|
11
|
+
>>> content = await path.a_read_text()
|
|
12
|
+
>>>
|
|
13
|
+
>>> # S3 path (sync methods)
|
|
14
|
+
>>> s3_path = PanPath("s3://bucket/key.txt")
|
|
15
|
+
>>> content = s3_path.read_text()
|
|
16
|
+
>>>
|
|
17
|
+
>>> # S3 path (async methods with a_ prefix)
|
|
18
|
+
>>> content = await s3_path.a_read_text()
|
|
19
|
+
>>>
|
|
20
|
+
>>> # Check if object is a PanPath instance
|
|
21
|
+
>>> isinstance(path, PanPath) # True for any path created by this package
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from panpath.base import PanPath
|
|
25
|
+
from panpath.cloud import CloudPath
|
|
26
|
+
from panpath.local_path import LocalPath
|
|
27
|
+
|
|
28
|
+
# Import path classes and register them
|
|
29
|
+
from panpath.registry import register_path_class
|
|
30
|
+
|
|
31
|
+
# Register S3
|
|
32
|
+
try:
|
|
33
|
+
from panpath.s3_path import S3Path
|
|
34
|
+
|
|
35
|
+
register_path_class("s3", S3Path)
|
|
36
|
+
except ImportError:
|
|
37
|
+
# S3 dependencies not installed
|
|
38
|
+
pass
|
|
39
|
+
|
|
40
|
+
# Register Google Cloud Storage
|
|
41
|
+
try:
|
|
42
|
+
from panpath.gs_path import GSPath
|
|
43
|
+
|
|
44
|
+
register_path_class("gs", GSPath)
|
|
45
|
+
except ImportError:
|
|
46
|
+
# GCS dependencies not installed
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
# Register Azure Blob Storage
|
|
50
|
+
try:
|
|
51
|
+
from panpath.azure_path import AzurePath
|
|
52
|
+
|
|
53
|
+
register_path_class("az", AzurePath)
|
|
54
|
+
register_path_class("azure", AzurePath) # Support both schemes
|
|
55
|
+
except ImportError:
|
|
56
|
+
# Azure dependencies not installed
|
|
57
|
+
pass
|
|
58
|
+
|
|
59
|
+
__version__ = "0.2.0"
|
|
60
|
+
|
|
61
|
+
__all__ = [
|
|
62
|
+
"PanPath",
|
|
63
|
+
"CloudPath",
|
|
64
|
+
"LocalPath",
|
|
65
|
+
# Export cloud paths if available
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
# Add cloud path classes to __all__ if they're available
|
|
69
|
+
try:
|
|
70
|
+
__all__.extend(["S3Path"])
|
|
71
|
+
except NameError:
|
|
72
|
+
pass
|
|
73
|
+
|
|
74
|
+
try:
|
|
75
|
+
__all__.extend(["GSPath"])
|
|
76
|
+
except NameError:
|
|
77
|
+
pass
|
|
78
|
+
|
|
79
|
+
try:
|
|
80
|
+
__all__.extend(["AzurePath"])
|
|
81
|
+
except NameError:
|
|
82
|
+
pass
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""Async Azure Blob Storage client implementation."""
|
|
2
|
-
from typing import Any, Optional
|
|
2
|
+
from typing import Any, List, Optional, Union
|
|
3
3
|
|
|
4
|
-
from panpath.clients import AsyncClient
|
|
4
|
+
from panpath.clients import AsyncClient, AsyncFileHandle, BaseAsyncFileHandle
|
|
5
5
|
from panpath.exceptions import MissingDependencyError, NoSuchFileError
|
|
6
6
|
|
|
7
7
|
try:
|
|
@@ -318,7 +318,7 @@ class AsyncAzureBlobClient(AsyncClient):
|
|
|
318
318
|
List of matching AsyncCloudPath objects
|
|
319
319
|
"""
|
|
320
320
|
from fnmatch import fnmatch
|
|
321
|
-
from panpath.
|
|
321
|
+
from panpath.base import PanPath
|
|
322
322
|
|
|
323
323
|
container_name, blob_prefix = self._parse_azure_path(path)
|
|
324
324
|
container_client = self._client.get_container_client(container_name)
|
|
@@ -342,7 +342,7 @@ class AsyncAzureBlobClient(AsyncClient):
|
|
|
342
342
|
if fnmatch(blob.name, f"*{file_pattern}"):
|
|
343
343
|
# Determine scheme from original path
|
|
344
344
|
scheme = "az" if path.startswith("az://") else "azure"
|
|
345
|
-
results.append(PanPath(f"{scheme}://{container_name}/{blob.name}"
|
|
345
|
+
results.append(PanPath(f"{scheme}://{container_name}/{blob.name}"))
|
|
346
346
|
return results
|
|
347
347
|
else:
|
|
348
348
|
# Non-recursive - list blobs with prefix
|
|
@@ -357,7 +357,7 @@ class AsyncAzureBlobClient(AsyncClient):
|
|
|
357
357
|
rel_name = blob.name[len(prefix_with_slash):]
|
|
358
358
|
if "/" not in rel_name and fnmatch(blob.name, f"{prefix_with_slash}{pattern}"):
|
|
359
359
|
scheme = "az" if path.startswith("az://") else "azure"
|
|
360
|
-
results.append(PanPath(f"{scheme}://{container_name}/{blob.name}"
|
|
360
|
+
results.append(PanPath(f"{scheme}://{container_name}/{blob.name}"))
|
|
361
361
|
return results
|
|
362
362
|
|
|
363
363
|
async def walk(self, path: str) -> list[tuple[str, list[str], list[str]]]:
|
|
@@ -549,3 +549,244 @@ class AsyncAzureBlobClient(AsyncClient):
|
|
|
549
549
|
tgt_blob_client = self._client.get_blob_client(tgt_container_name, tgt_blob_name)
|
|
550
550
|
source_url = src_blob_client.url
|
|
551
551
|
await tgt_blob_client.start_copy_from_url(source_url)
|
|
552
|
+
|
|
553
|
+
def a_open(
|
|
554
|
+
self,
|
|
555
|
+
path: str,
|
|
556
|
+
mode: str = "r",
|
|
557
|
+
encoding: Optional[str] = None,
|
|
558
|
+
**kwargs: Any,
|
|
559
|
+
) -> AsyncFileHandle:
|
|
560
|
+
"""Open Azure blob and return async file handle with streaming support.
|
|
561
|
+
|
|
562
|
+
Args:
|
|
563
|
+
path: Azure path (az://container/blob)
|
|
564
|
+
mode: File mode ('r', 'w', 'rb', 'wb', 'a', 'ab')
|
|
565
|
+
encoding: Text encoding (for text modes)
|
|
566
|
+
**kwargs: Additional arguments (unused for Azure)
|
|
567
|
+
|
|
568
|
+
Returns:
|
|
569
|
+
AzureAsyncFileHandle with streaming support
|
|
570
|
+
"""
|
|
571
|
+
container, blob = self._parse_azure_path(path)
|
|
572
|
+
return AzureAsyncFileHandle(
|
|
573
|
+
client=self._client,
|
|
574
|
+
container=container,
|
|
575
|
+
blob=blob,
|
|
576
|
+
mode=mode,
|
|
577
|
+
encoding=encoding,
|
|
578
|
+
)
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
class AzureAsyncFileHandle(AsyncFileHandle):
|
|
582
|
+
"""Async file handle for Azure with chunked streaming support.
|
|
583
|
+
|
|
584
|
+
Uses Azure SDK's download_blob streaming API.
|
|
585
|
+
"""
|
|
586
|
+
|
|
587
|
+
def __init__(
|
|
588
|
+
self,
|
|
589
|
+
client: Any,
|
|
590
|
+
container: str,
|
|
591
|
+
blob: str,
|
|
592
|
+
mode: str = "r",
|
|
593
|
+
encoding: Optional[str] = None,
|
|
594
|
+
chunk_size: int = 4096,
|
|
595
|
+
):
|
|
596
|
+
"""Initialize Azure file handle.
|
|
597
|
+
|
|
598
|
+
Args:
|
|
599
|
+
client: BlobServiceClient instance
|
|
600
|
+
container: Azure container name
|
|
601
|
+
blob: Azure blob name
|
|
602
|
+
mode: File mode
|
|
603
|
+
encoding: Text encoding
|
|
604
|
+
chunk_size: Size of chunks for streaming reads
|
|
605
|
+
"""
|
|
606
|
+
self._client = client
|
|
607
|
+
self._container = container
|
|
608
|
+
self._blob = blob
|
|
609
|
+
self._mode = mode
|
|
610
|
+
self._encoding = encoding or "utf-8"
|
|
611
|
+
self._chunk_size = chunk_size
|
|
612
|
+
self._closed = False
|
|
613
|
+
|
|
614
|
+
# For reading
|
|
615
|
+
self._read_buffer = b"" if "b" in mode else ""
|
|
616
|
+
self._download_stream = None
|
|
617
|
+
self._chunks_iter = None
|
|
618
|
+
self._eof = False
|
|
619
|
+
|
|
620
|
+
# For writing
|
|
621
|
+
self._write_buffer: Union[bytearray, List[str]] = bytearray() if "b" in mode else []
|
|
622
|
+
|
|
623
|
+
# Parse mode
|
|
624
|
+
self._is_read = "r" in mode
|
|
625
|
+
self._is_write = "w" in mode or "a" in mode
|
|
626
|
+
self._is_binary = "b" in mode
|
|
627
|
+
self._is_append = "a" in mode
|
|
628
|
+
|
|
629
|
+
async def __aenter__(self) -> "AzureAsyncFileHandle":
|
|
630
|
+
"""Initialize file handle."""
|
|
631
|
+
if self._is_read:
|
|
632
|
+
# Initialize streaming download
|
|
633
|
+
blob_client = self._client.get_blob_client(self._container, self._blob)
|
|
634
|
+
try:
|
|
635
|
+
self._download_stream = await blob_client.download_blob()
|
|
636
|
+
self._chunks_iter = self._download_stream.chunks()
|
|
637
|
+
except ResourceNotFoundError as e:
|
|
638
|
+
raise NoSuchFileError(f"Azure blob not found: az://{self._container}/{self._blob}") from e
|
|
639
|
+
elif self._is_append:
|
|
640
|
+
# Load existing data for append
|
|
641
|
+
blob_client = self._client.get_blob_client(self._container, self._blob)
|
|
642
|
+
try:
|
|
643
|
+
download_stream = await blob_client.download_blob()
|
|
644
|
+
existing = await download_stream.readall()
|
|
645
|
+
if self._is_binary:
|
|
646
|
+
self._write_buffer = bytearray(existing)
|
|
647
|
+
else:
|
|
648
|
+
self._write_buffer = [existing.decode(self._encoding)]
|
|
649
|
+
except ResourceNotFoundError:
|
|
650
|
+
# Blob doesn't exist, start with empty buffer
|
|
651
|
+
pass
|
|
652
|
+
|
|
653
|
+
return self
|
|
654
|
+
|
|
655
|
+
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
|
|
656
|
+
"""Close file handle."""
|
|
657
|
+
await self.close()
|
|
658
|
+
|
|
659
|
+
async def _fill_buffer(self, min_size: int = 1) -> None:
|
|
660
|
+
"""Fill read buffer by reading chunks from Azure.
|
|
661
|
+
|
|
662
|
+
Args:
|
|
663
|
+
min_size: Minimum number of bytes/chars to have in buffer
|
|
664
|
+
"""
|
|
665
|
+
if self._eof or self._chunks_iter is None:
|
|
666
|
+
return
|
|
667
|
+
|
|
668
|
+
while len(self._read_buffer) < min_size and not self._eof:
|
|
669
|
+
try:
|
|
670
|
+
chunk = await self._chunks_iter.__anext__()
|
|
671
|
+
if self._is_binary:
|
|
672
|
+
self._read_buffer += chunk # type: ignore
|
|
673
|
+
else:
|
|
674
|
+
self._read_buffer += chunk.decode(self._encoding) # type: ignore
|
|
675
|
+
except StopAsyncIteration:
|
|
676
|
+
self._eof = True
|
|
677
|
+
break
|
|
678
|
+
|
|
679
|
+
async def read(self, size: int = -1) -> Union[str, bytes]:
|
|
680
|
+
"""Read from Azure blob with streaming."""
|
|
681
|
+
if not self._is_read:
|
|
682
|
+
raise ValueError("File not opened for reading")
|
|
683
|
+
if self._closed:
|
|
684
|
+
raise ValueError("I/O operation on closed file")
|
|
685
|
+
|
|
686
|
+
if size == -1:
|
|
687
|
+
# Read all remaining
|
|
688
|
+
await self._fill_buffer(float('inf')) # type: ignore
|
|
689
|
+
result = self._read_buffer
|
|
690
|
+
self._read_buffer = b"" if self._is_binary else ""
|
|
691
|
+
return result
|
|
692
|
+
else:
|
|
693
|
+
# Read specific size
|
|
694
|
+
await self._fill_buffer(size)
|
|
695
|
+
result = self._read_buffer[:size]
|
|
696
|
+
self._read_buffer = self._read_buffer[size:]
|
|
697
|
+
return result
|
|
698
|
+
|
|
699
|
+
async def readline(self, size: int = -1) -> Union[str, bytes]:
|
|
700
|
+
"""Read one line from Azure blob."""
|
|
701
|
+
if not self._is_read:
|
|
702
|
+
raise ValueError("File not opened for reading")
|
|
703
|
+
if self._closed:
|
|
704
|
+
raise ValueError("I/O operation on closed file")
|
|
705
|
+
|
|
706
|
+
newline = b"\n" if self._is_binary else "\n"
|
|
707
|
+
|
|
708
|
+
# Fill buffer until we find a newline
|
|
709
|
+
while newline not in self._read_buffer and not self._eof: # type: ignore
|
|
710
|
+
await self._fill_buffer(self._chunk_size)
|
|
711
|
+
|
|
712
|
+
# Find newline
|
|
713
|
+
try:
|
|
714
|
+
newline_pos = self._read_buffer.index(newline) # type: ignore
|
|
715
|
+
end = newline_pos + 1
|
|
716
|
+
except ValueError:
|
|
717
|
+
end = len(self._read_buffer)
|
|
718
|
+
|
|
719
|
+
if size != -1 and end > size:
|
|
720
|
+
end = size
|
|
721
|
+
|
|
722
|
+
result = self._read_buffer[:end]
|
|
723
|
+
self._read_buffer = self._read_buffer[end:]
|
|
724
|
+
return result
|
|
725
|
+
|
|
726
|
+
async def readlines(self) -> List[Union[str, bytes]]:
|
|
727
|
+
"""Read all lines."""
|
|
728
|
+
lines = []
|
|
729
|
+
while True:
|
|
730
|
+
line = await self.readline()
|
|
731
|
+
if not line:
|
|
732
|
+
break
|
|
733
|
+
lines.append(line)
|
|
734
|
+
return lines
|
|
735
|
+
|
|
736
|
+
async def write(self, data: Union[str, bytes]) -> int:
|
|
737
|
+
"""Buffer data for writing."""
|
|
738
|
+
if not self._is_write:
|
|
739
|
+
raise ValueError("File not opened for writing")
|
|
740
|
+
if self._closed:
|
|
741
|
+
raise ValueError("I/O operation on closed file")
|
|
742
|
+
|
|
743
|
+
if self._is_binary:
|
|
744
|
+
if isinstance(data, str):
|
|
745
|
+
data = data.encode(self._encoding)
|
|
746
|
+
self._write_buffer.extend(data) # type: ignore
|
|
747
|
+
else:
|
|
748
|
+
if isinstance(data, bytes):
|
|
749
|
+
data = data.decode(self._encoding)
|
|
750
|
+
self._write_buffer.append(data) # type: ignore
|
|
751
|
+
|
|
752
|
+
return len(data)
|
|
753
|
+
|
|
754
|
+
async def writelines(self, lines: List[Union[str, bytes]]) -> None:
|
|
755
|
+
"""Write multiple lines."""
|
|
756
|
+
for line in lines:
|
|
757
|
+
await self.write(line)
|
|
758
|
+
|
|
759
|
+
async def close(self) -> None:
|
|
760
|
+
"""Close and upload buffered writes."""
|
|
761
|
+
if self._closed:
|
|
762
|
+
return
|
|
763
|
+
|
|
764
|
+
if self._is_write:
|
|
765
|
+
if self._is_binary:
|
|
766
|
+
body = bytes(self._write_buffer)
|
|
767
|
+
else:
|
|
768
|
+
body = "".join(self._write_buffer).encode(self._encoding) # type: ignore
|
|
769
|
+
|
|
770
|
+
blob_client = self._client.get_blob_client(self._container, self._blob)
|
|
771
|
+
await blob_client.upload_blob(body, overwrite=True)
|
|
772
|
+
|
|
773
|
+
self._closed = True
|
|
774
|
+
|
|
775
|
+
def __aiter__(self) -> "AzureAsyncFileHandle":
|
|
776
|
+
"""Support async iteration."""
|
|
777
|
+
if not self._is_read:
|
|
778
|
+
raise ValueError("File not opened for reading")
|
|
779
|
+
return self
|
|
780
|
+
|
|
781
|
+
async def __anext__(self) -> Union[str, bytes]:
|
|
782
|
+
"""Get next line."""
|
|
783
|
+
line = await self.readline()
|
|
784
|
+
if not line:
|
|
785
|
+
raise StopAsyncIteration
|
|
786
|
+
return line
|
|
787
|
+
|
|
788
|
+
@property
|
|
789
|
+
def closed(self) -> bool:
|
|
790
|
+
"""Check if closed."""
|
|
791
|
+
return self._closed
|
|
792
|
+
|
|
@@ -315,7 +315,7 @@ class AzureBlobClient(Client):
|
|
|
315
315
|
List of matching CloudPath objects
|
|
316
316
|
"""
|
|
317
317
|
from fnmatch import fnmatch
|
|
318
|
-
from panpath.
|
|
318
|
+
from panpath.base import PanPath
|
|
319
319
|
|
|
320
320
|
container_name, blob_prefix = self._parse_azure_path(path)
|
|
321
321
|
container_client = self._client.get_container_client(container_name)
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Azure Blob Storage path implementation."""
|
|
2
|
+
from typing import TYPE_CHECKING, Optional
|
|
3
|
+
|
|
4
|
+
from panpath.cloud import CloudPath
|
|
5
|
+
from panpath.azure_client import AzureBlobClient
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from panpath.clients import Client, AsyncClient
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class AzurePath(CloudPath):
|
|
12
|
+
"""Azure Blob Storage path implementation (sync and async methods)."""
|
|
13
|
+
|
|
14
|
+
_client: Optional[AzureBlobClient] = None
|
|
15
|
+
_default_client: Optional[AzureBlobClient] = None
|
|
16
|
+
|
|
17
|
+
@classmethod
|
|
18
|
+
def _create_default_client(cls) -> "Client":
|
|
19
|
+
"""Create default Azure Blob client."""
|
|
20
|
+
return AzureBlobClient()
|
|
21
|
+
|
|
22
|
+
@classmethod
|
|
23
|
+
def _create_default_async_client(cls) -> "AsyncClient":
|
|
24
|
+
"""Create default async Azure Blob client."""
|
|
25
|
+
from panpath.azure_async_client import AsyncAzureBlobClient
|
|
26
|
+
return AsyncAzureBlobClient()
|