python-filewrap 0.0.6__tar.gz → 0.0.7.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.6 → python_filewrap-0.0.7.1}/PKG-INFO +1 -1
- {python_filewrap-0.0.6 → python_filewrap-0.0.7.1}/filewrap/__init__.py +58 -3
- {python_filewrap-0.0.6 → python_filewrap-0.0.7.1}/pyproject.toml +1 -1
- {python_filewrap-0.0.6 → python_filewrap-0.0.7.1}/LICENSE +0 -0
- {python_filewrap-0.0.6 → python_filewrap-0.0.7.1}/filewrap/py.typed +0 -0
- {python_filewrap-0.0.6 → python_filewrap-0.0.7.1}/readme.md +0 -0
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
# encoding: utf-8
|
|
3
3
|
|
|
4
4
|
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
-
__version__ = (0, 0,
|
|
5
|
+
__version__ = (0, 0, 7)
|
|
6
6
|
__all__ = [
|
|
7
7
|
"SupportsRead", "SupportsWrite",
|
|
8
8
|
"bio_chunk_iter", "bio_chunk_async_iter",
|
|
9
9
|
"bio_skip_iter", "bio_skip_async_iter",
|
|
10
|
+
"bytes_iter_skip", "bytes_async_iter_skip",
|
|
10
11
|
"bytes_iter_to_reader", "bytes_iter_to_async_reader",
|
|
11
12
|
"bytes_to_chunk_iter", "bytes_to_chunk_async_iter",
|
|
12
13
|
"bytes_ensure_part_iter", "bytes_ensure_part_async_iter",
|
|
@@ -16,27 +17,30 @@ from asyncio import to_thread, Lock as AsyncLock
|
|
|
16
17
|
from collections.abc import Awaitable, AsyncIterable, Iterable
|
|
17
18
|
from functools import update_wrapper
|
|
18
19
|
from inspect import isawaitable, iscoroutinefunction
|
|
20
|
+
from itertools import chain
|
|
19
21
|
from collections.abc import AsyncIterator, Callable, Iterator
|
|
20
22
|
from shutil import COPY_BUFSIZE # type: ignore
|
|
21
23
|
from threading import Lock
|
|
22
|
-
from typing import Any, Protocol, TypeVar
|
|
24
|
+
from typing import runtime_checkable, Any, Protocol, TypeVar
|
|
23
25
|
|
|
24
26
|
try:
|
|
25
27
|
from collections.abc import Buffer # type: ignore
|
|
26
28
|
except ImportError:
|
|
27
29
|
Buffer = Any
|
|
28
30
|
|
|
29
|
-
from asynctools import ensure_async, ensure_aiter
|
|
31
|
+
from asynctools import async_chain, ensure_async, ensure_aiter
|
|
30
32
|
|
|
31
33
|
|
|
32
34
|
_T_co = TypeVar("_T_co", covariant=True)
|
|
33
35
|
_T_contra = TypeVar("_T_contra", contravariant=True)
|
|
34
36
|
|
|
35
37
|
|
|
38
|
+
@runtime_checkable
|
|
36
39
|
class SupportsRead(Protocol[_T_co]):
|
|
37
40
|
def read(self, __length: int = ...) -> _T_co: ...
|
|
38
41
|
|
|
39
42
|
|
|
43
|
+
@runtime_checkable
|
|
40
44
|
class SupportsWrite(Protocol[_T_contra]):
|
|
41
45
|
def write(self, __s: _T_contra) -> object: ...
|
|
42
46
|
|
|
@@ -242,6 +246,57 @@ async def bio_skip_async_iter(
|
|
|
242
246
|
yield length
|
|
243
247
|
|
|
244
248
|
|
|
249
|
+
def bytes_iter_skip(
|
|
250
|
+
it: Iterable[Buffer],
|
|
251
|
+
/,
|
|
252
|
+
size: int = -1,
|
|
253
|
+
callback: None | Callable[[int], Any] = None,
|
|
254
|
+
) -> Iterator[memoryview | Buffer]:
|
|
255
|
+
it = iter(it)
|
|
256
|
+
if size == 0:
|
|
257
|
+
return it
|
|
258
|
+
if not callable(callback):
|
|
259
|
+
callback = None
|
|
260
|
+
for m in map(memoryview, it):
|
|
261
|
+
l = len(m)
|
|
262
|
+
if callback:
|
|
263
|
+
callback(min(l, size))
|
|
264
|
+
if l == size:
|
|
265
|
+
return it
|
|
266
|
+
elif l > size:
|
|
267
|
+
return chain((m[size:],), it)
|
|
268
|
+
else:
|
|
269
|
+
size -= l
|
|
270
|
+
return iter(())
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
async def bytes_async_iter_skip(
|
|
274
|
+
it: Iterable[Buffer] | AsyncIterator[Buffer],
|
|
275
|
+
/,
|
|
276
|
+
size: int = -1,
|
|
277
|
+
callback: None | Callable[[int], Any] = None,
|
|
278
|
+
) -> AsyncIterator[memoryview | Buffer]:
|
|
279
|
+
it = aiter(ensure_aiter(it))
|
|
280
|
+
if size == 0:
|
|
281
|
+
return it
|
|
282
|
+
callback = ensure_async(callback) if callable(callback) else None
|
|
283
|
+
async for b in it:
|
|
284
|
+
m = memoryview(b)
|
|
285
|
+
l = len(m)
|
|
286
|
+
if callback:
|
|
287
|
+
await callback(min(l, size))
|
|
288
|
+
if l == size:
|
|
289
|
+
return it
|
|
290
|
+
elif l > size:
|
|
291
|
+
return async_chain((m[size:],), it)
|
|
292
|
+
else:
|
|
293
|
+
size -= l
|
|
294
|
+
async def make_iter():
|
|
295
|
+
if False:
|
|
296
|
+
yield
|
|
297
|
+
return make_iter()
|
|
298
|
+
|
|
299
|
+
|
|
245
300
|
def bytes_iter_to_reader(
|
|
246
301
|
it: Iterable[Buffer],
|
|
247
302
|
/,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|