python-filewrap 0.0.1__tar.gz → 0.0.2__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.
- {python_filewrap-0.0.1 → python_filewrap-0.0.2}/PKG-INFO +1 -1
- {python_filewrap-0.0.1 → python_filewrap-0.0.2}/filewrap/__init__.py +55 -45
- {python_filewrap-0.0.1 → python_filewrap-0.0.2}/pyproject.toml +1 -1
- {python_filewrap-0.0.1 → python_filewrap-0.0.2}/LICENSE +0 -0
- {python_filewrap-0.0.1 → python_filewrap-0.0.2}/filewrap/py.typed +0 -0
- {python_filewrap-0.0.1 → python_filewrap-0.0.2}/readme.md +0 -0
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
# encoding: utf-8
|
|
3
3
|
|
|
4
4
|
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
-
__version__ = (0, 0,
|
|
5
|
+
__version__ = (0, 0, 2)
|
|
6
6
|
__all__ = [
|
|
7
7
|
"SupportsRead", "SupportsWrite", "bio_chunk_iter", "bio_chunk_async_iter",
|
|
8
8
|
"bio_skip_iter", "bio_skip_async_iter", "bio_skip_bytes", "bio_skip_bytes_async"
|
|
9
9
|
]
|
|
10
10
|
|
|
11
11
|
from asyncio import to_thread
|
|
12
|
+
from collections.abc import Awaitable
|
|
12
13
|
from functools import update_wrapper
|
|
13
14
|
from inspect import isawaitable, iscoroutinefunction
|
|
14
15
|
from collections.abc import AsyncIterator, Callable, Iterator
|
|
@@ -39,13 +40,16 @@ def ensure_async(func, /):
|
|
|
39
40
|
|
|
40
41
|
|
|
41
42
|
def bio_chunk_iter(
|
|
42
|
-
bio: SupportsRead[bytes],
|
|
43
|
+
bio: SupportsRead[bytes] | Callable[[int], bytes],
|
|
43
44
|
/,
|
|
44
45
|
size: int = -1,
|
|
45
46
|
chunksize: int = COPY_BUFSIZE,
|
|
46
47
|
callback: None | Callable[[int], Any] = None,
|
|
47
48
|
) -> Iterator[bytes]:
|
|
48
|
-
|
|
49
|
+
if callable(bio):
|
|
50
|
+
read = bio
|
|
51
|
+
else:
|
|
52
|
+
read = bio.read
|
|
49
53
|
if not callable(callback):
|
|
50
54
|
callback = None
|
|
51
55
|
if size > 0:
|
|
@@ -67,13 +71,16 @@ def bio_chunk_iter(
|
|
|
67
71
|
|
|
68
72
|
|
|
69
73
|
async def bio_chunk_async_iter(
|
|
70
|
-
bio: SupportsRead[bytes],
|
|
74
|
+
bio: SupportsRead[bytes] | Callable[[int], bytes | Awaitable[bytes]],
|
|
71
75
|
/,
|
|
72
76
|
size: int = -1,
|
|
73
77
|
chunksize: int = COPY_BUFSIZE,
|
|
74
78
|
callback: None | Callable[[int], Any] = None,
|
|
75
79
|
) -> AsyncIterator[bytes]:
|
|
76
|
-
|
|
80
|
+
if callable(bio):
|
|
81
|
+
read = ensure_async(bio)
|
|
82
|
+
else:
|
|
83
|
+
read = ensure_async(bio.read)
|
|
77
84
|
callback = ensure_async(callback) if callable(callback) else None
|
|
78
85
|
if size > 0:
|
|
79
86
|
while size:
|
|
@@ -94,7 +101,7 @@ async def bio_chunk_async_iter(
|
|
|
94
101
|
|
|
95
102
|
|
|
96
103
|
def bio_skip_iter(
|
|
97
|
-
bio: SupportsRead[bytes],
|
|
104
|
+
bio: SupportsRead[bytes] | Callable[[int], bytes],
|
|
98
105
|
/,
|
|
99
106
|
size: int = -1,
|
|
100
107
|
chunksize: int = COPY_BUFSIZE,
|
|
@@ -114,7 +121,9 @@ def bio_skip_iter(
|
|
|
114
121
|
except Exception:
|
|
115
122
|
if chunksize <= 0:
|
|
116
123
|
chunksize = COPY_BUFSIZE
|
|
117
|
-
if
|
|
124
|
+
if callable(bio):
|
|
125
|
+
read = bio
|
|
126
|
+
elif hasattr(bio, "readinto"):
|
|
118
127
|
readinto = bio.readinto
|
|
119
128
|
buf = bytearray(chunksize)
|
|
120
129
|
if size > 0:
|
|
@@ -138,23 +147,24 @@ def bio_skip_iter(
|
|
|
138
147
|
if callback:
|
|
139
148
|
callback(length)
|
|
140
149
|
yield length
|
|
150
|
+
return
|
|
141
151
|
else:
|
|
142
152
|
read = bio.read
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
153
|
+
if size > 0:
|
|
154
|
+
while size:
|
|
155
|
+
readsize = min(chunksize, size)
|
|
156
|
+
length = len(read(readsize))
|
|
157
|
+
if callback:
|
|
158
|
+
callback(length)
|
|
159
|
+
yield length
|
|
160
|
+
if length < readsize:
|
|
161
|
+
break
|
|
162
|
+
size -= readsize
|
|
163
|
+
else:
|
|
164
|
+
while (length := len(read(chunksize))):
|
|
165
|
+
if callback:
|
|
166
|
+
callback(length)
|
|
167
|
+
yield length
|
|
158
168
|
else:
|
|
159
169
|
if callback:
|
|
160
170
|
callback(length)
|
|
@@ -162,7 +172,7 @@ def bio_skip_iter(
|
|
|
162
172
|
|
|
163
173
|
|
|
164
174
|
async def bio_skip_async_iter(
|
|
165
|
-
bio: SupportsRead[bytes],
|
|
175
|
+
bio: SupportsRead[bytes] | Callable[[int], bytes | Awaitable[bytes]],
|
|
166
176
|
/,
|
|
167
177
|
size: int = -1,
|
|
168
178
|
chunksize: int = COPY_BUFSIZE,
|
|
@@ -181,7 +191,9 @@ async def bio_skip_async_iter(
|
|
|
181
191
|
except Exception:
|
|
182
192
|
if chunksize <= 0:
|
|
183
193
|
chunksize = COPY_BUFSIZE
|
|
184
|
-
if
|
|
194
|
+
if callable(bio):
|
|
195
|
+
read = ensure_async(bio)
|
|
196
|
+
elif hasattr(bio, "readinto"):
|
|
185
197
|
readinto = ensure_async(bio.readinto)
|
|
186
198
|
buf = bytearray(chunksize)
|
|
187
199
|
if size > 0:
|
|
@@ -207,21 +219,21 @@ async def bio_skip_async_iter(
|
|
|
207
219
|
yield length
|
|
208
220
|
else:
|
|
209
221
|
read = ensure_async(bio.read)
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
222
|
+
if size > 0:
|
|
223
|
+
while size:
|
|
224
|
+
readsize = min(chunksize, size)
|
|
225
|
+
length = len(await read(readsize))
|
|
226
|
+
if callback:
|
|
227
|
+
await callback(length)
|
|
228
|
+
yield length
|
|
229
|
+
if length < readsize:
|
|
230
|
+
break
|
|
231
|
+
size -= readsize
|
|
232
|
+
else:
|
|
233
|
+
while (length := len(await read(chunksize))):
|
|
234
|
+
if callback:
|
|
235
|
+
await callback(length)
|
|
236
|
+
yield length
|
|
225
237
|
else:
|
|
226
238
|
if callback:
|
|
227
239
|
await callback(length)
|
|
@@ -229,25 +241,23 @@ async def bio_skip_async_iter(
|
|
|
229
241
|
|
|
230
242
|
|
|
231
243
|
def bio_skip_bytes(
|
|
232
|
-
bio: SupportsRead[bytes],
|
|
244
|
+
bio: SupportsRead[bytes] | Callable[[int], bytes],
|
|
233
245
|
/,
|
|
234
246
|
size: int = -1,
|
|
235
247
|
chunksize: int = COPY_BUFSIZE,
|
|
236
248
|
callback: None | Callable[[int], Any] = None,
|
|
237
|
-
)
|
|
249
|
+
):
|
|
238
250
|
for _ in bio_skip_iter(bio, size, chunksize, callback=callback):
|
|
239
251
|
pass
|
|
240
|
-
return bio
|
|
241
252
|
|
|
242
253
|
|
|
243
254
|
async def bio_skip_bytes_async(
|
|
244
|
-
bio: SupportsRead[bytes],
|
|
255
|
+
bio: SupportsRead[bytes] | Callable[[int], bytes | Awaitable[bytes]],
|
|
245
256
|
/,
|
|
246
257
|
size: int = -1,
|
|
247
258
|
chunksize: int = COPY_BUFSIZE,
|
|
248
259
|
callback: None | Callable[[int], Any] = None,
|
|
249
|
-
)
|
|
260
|
+
):
|
|
250
261
|
async for _ in bio_skip_async_iter(bio, size, chunksize, callback=callback):
|
|
251
262
|
pass
|
|
252
|
-
return bio
|
|
253
263
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|