python-filewrap 0.0.4.1__tar.gz → 0.0.6__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.4.1 → python_filewrap-0.0.6}/PKG-INFO +1 -1
- {python_filewrap-0.0.4.1 → python_filewrap-0.0.6}/filewrap/__init__.py +93 -10
- {python_filewrap-0.0.4.1 → python_filewrap-0.0.6}/pyproject.toml +1 -1
- {python_filewrap-0.0.4.1 → python_filewrap-0.0.6}/LICENSE +0 -0
- {python_filewrap-0.0.4.1 → python_filewrap-0.0.6}/filewrap/py.typed +0 -0
- {python_filewrap-0.0.4.1 → python_filewrap-0.0.6}/readme.md +0 -0
|
@@ -2,12 +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, 6)
|
|
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
10
|
"bytes_iter_to_reader", "bytes_iter_to_async_reader",
|
|
11
|
+
"bytes_to_chunk_iter", "bytes_to_chunk_async_iter",
|
|
12
|
+
"bytes_ensure_part_iter", "bytes_ensure_part_async_iter",
|
|
11
13
|
]
|
|
12
14
|
|
|
13
15
|
from asyncio import to_thread, Lock as AsyncLock
|
|
@@ -19,7 +21,12 @@ from shutil import COPY_BUFSIZE # type: ignore
|
|
|
19
21
|
from threading import Lock
|
|
20
22
|
from typing import Any, Protocol, TypeVar
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
try:
|
|
25
|
+
from collections.abc import Buffer # type: ignore
|
|
26
|
+
except ImportError:
|
|
27
|
+
Buffer = Any
|
|
28
|
+
|
|
29
|
+
from asynctools import ensure_async, ensure_aiter
|
|
23
30
|
|
|
24
31
|
|
|
25
32
|
_T_co = TypeVar("_T_co", covariant=True)
|
|
@@ -35,12 +42,12 @@ class SupportsWrite(Protocol[_T_contra]):
|
|
|
35
42
|
|
|
36
43
|
|
|
37
44
|
def bio_chunk_iter(
|
|
38
|
-
bio: SupportsRead[
|
|
45
|
+
bio: SupportsRead[Buffer] | Callable[[int], Buffer],
|
|
39
46
|
/,
|
|
40
47
|
size: int = -1,
|
|
41
48
|
chunksize: int = COPY_BUFSIZE,
|
|
42
49
|
callback: None | Callable[[int], Any] = None,
|
|
43
|
-
) -> Iterator[
|
|
50
|
+
) -> Iterator[Buffer]:
|
|
44
51
|
if callable(bio):
|
|
45
52
|
read = bio
|
|
46
53
|
else:
|
|
@@ -66,12 +73,12 @@ def bio_chunk_iter(
|
|
|
66
73
|
|
|
67
74
|
|
|
68
75
|
async def bio_chunk_async_iter(
|
|
69
|
-
bio: SupportsRead[
|
|
76
|
+
bio: SupportsRead[Buffer] | Callable[[int], Buffer | Awaitable[Buffer]],
|
|
70
77
|
/,
|
|
71
78
|
size: int = -1,
|
|
72
79
|
chunksize: int = COPY_BUFSIZE,
|
|
73
80
|
callback: None | Callable[[int], Any] = None,
|
|
74
|
-
) -> AsyncIterator[
|
|
81
|
+
) -> AsyncIterator[Buffer]:
|
|
75
82
|
if callable(bio):
|
|
76
83
|
read = ensure_async(bio)
|
|
77
84
|
else:
|
|
@@ -96,7 +103,7 @@ async def bio_chunk_async_iter(
|
|
|
96
103
|
|
|
97
104
|
|
|
98
105
|
def bio_skip_iter(
|
|
99
|
-
bio: SupportsRead[
|
|
106
|
+
bio: SupportsRead[Buffer] | Callable[[int], Buffer],
|
|
100
107
|
/,
|
|
101
108
|
size: int = -1,
|
|
102
109
|
chunksize: int = COPY_BUFSIZE,
|
|
@@ -167,7 +174,7 @@ def bio_skip_iter(
|
|
|
167
174
|
|
|
168
175
|
|
|
169
176
|
async def bio_skip_async_iter(
|
|
170
|
-
bio: SupportsRead[
|
|
177
|
+
bio: SupportsRead[Buffer] | Callable[[int], Buffer | Awaitable[Buffer]],
|
|
171
178
|
/,
|
|
172
179
|
size: int = -1,
|
|
173
180
|
chunksize: int = COPY_BUFSIZE,
|
|
@@ -236,7 +243,7 @@ async def bio_skip_async_iter(
|
|
|
236
243
|
|
|
237
244
|
|
|
238
245
|
def bytes_iter_to_reader(
|
|
239
|
-
it: Iterable[
|
|
246
|
+
it: Iterable[Buffer],
|
|
240
247
|
/,
|
|
241
248
|
) -> SupportsRead[bytearray]:
|
|
242
249
|
getnext = iter(it).__next__
|
|
@@ -324,7 +331,7 @@ def bytes_iter_to_reader(
|
|
|
324
331
|
|
|
325
332
|
|
|
326
333
|
def bytes_iter_to_async_reader(
|
|
327
|
-
it: Iterable[
|
|
334
|
+
it: Iterable[Buffer] | AsyncIterable[Buffer],
|
|
328
335
|
/,
|
|
329
336
|
threaded: bool = True,
|
|
330
337
|
) -> SupportsRead[bytearray]:
|
|
@@ -414,3 +421,79 @@ def bytes_iter_to_async_reader(
|
|
|
414
421
|
"__repr__": staticmethod(lambda: reprs),
|
|
415
422
|
})()
|
|
416
423
|
|
|
424
|
+
|
|
425
|
+
def bytes_to_chunk_iter(
|
|
426
|
+
b: Buffer,
|
|
427
|
+
/,
|
|
428
|
+
chunksize: int = COPY_BUFSIZE,
|
|
429
|
+
) -> Iterator[memoryview]:
|
|
430
|
+
m = memoryview(b)
|
|
431
|
+
for i in range(0, len(m), chunksize):
|
|
432
|
+
yield m[i:i+chunksize]
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
async def bytes_to_chunk_async_iter(
|
|
436
|
+
b: Buffer,
|
|
437
|
+
/,
|
|
438
|
+
chunksize: int = COPY_BUFSIZE,
|
|
439
|
+
) -> AsyncIterator[memoryview]:
|
|
440
|
+
m = memoryview(b)
|
|
441
|
+
for i in range(0, len(m), chunksize):
|
|
442
|
+
yield m[i:i+chunksize]
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
def bytes_ensure_part_iter(
|
|
446
|
+
it: Iterable[Buffer],
|
|
447
|
+
/,
|
|
448
|
+
partsize: int = COPY_BUFSIZE,
|
|
449
|
+
) -> Iterator[Buffer | memoryview]:
|
|
450
|
+
n = partsize
|
|
451
|
+
for b in it:
|
|
452
|
+
m = memoryview(b)
|
|
453
|
+
l = len(m)
|
|
454
|
+
if l <= n:
|
|
455
|
+
yield b
|
|
456
|
+
if l == n:
|
|
457
|
+
n = partsize
|
|
458
|
+
else:
|
|
459
|
+
n -= l
|
|
460
|
+
else:
|
|
461
|
+
yield m[:n]
|
|
462
|
+
m = m[n:]
|
|
463
|
+
while len(m) >= partsize:
|
|
464
|
+
yield m[:partsize]
|
|
465
|
+
m = m[partsize:]
|
|
466
|
+
if m:
|
|
467
|
+
yield m
|
|
468
|
+
n = partsize - len(m)
|
|
469
|
+
else:
|
|
470
|
+
n = partsize
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
async def bytes_ensure_part_async_iter(
|
|
474
|
+
it: Iterable[Buffer] | AsyncIterable[Buffer],
|
|
475
|
+
/,
|
|
476
|
+
partsize: int = COPY_BUFSIZE,
|
|
477
|
+
) -> AsyncIterator[Buffer | memoryview]:
|
|
478
|
+
n = partsize
|
|
479
|
+
async for b in ensure_aiter(it):
|
|
480
|
+
m = memoryview(b)
|
|
481
|
+
l = len(m)
|
|
482
|
+
if l <= n:
|
|
483
|
+
yield b
|
|
484
|
+
if l == n:
|
|
485
|
+
n = partsize
|
|
486
|
+
else:
|
|
487
|
+
n -= l
|
|
488
|
+
else:
|
|
489
|
+
yield m[:n]
|
|
490
|
+
m = m[n:]
|
|
491
|
+
while len(m) >= partsize:
|
|
492
|
+
yield m[:partsize]
|
|
493
|
+
m = m[partsize:]
|
|
494
|
+
if m:
|
|
495
|
+
yield m
|
|
496
|
+
n = partsize - len(m)
|
|
497
|
+
else:
|
|
498
|
+
n = partsize
|
|
499
|
+
|
|
File without changes
|
|
File without changes
|
|
File without changes
|