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