python-filewrap 0.0.5__py3-none-any.whl → 0.0.6__py3-none-any.whl

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.
filewrap/__init__.py CHANGED
@@ -2,13 +2,14 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 0, 5)
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"
11
+ "bytes_to_chunk_iter", "bytes_to_chunk_async_iter",
12
+ "bytes_ensure_part_iter", "bytes_ensure_part_async_iter",
12
13
  ]
13
14
 
14
15
  from asyncio import to_thread, Lock as AsyncLock
@@ -20,7 +21,12 @@ from shutil import COPY_BUFSIZE # type: ignore
20
21
  from threading import Lock
21
22
  from typing import Any, Protocol, TypeVar
22
23
 
23
- from asynctools import ensure_async
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
24
30
 
25
31
 
26
32
  _T_co = TypeVar("_T_co", covariant=True)
@@ -36,12 +42,12 @@ class SupportsWrite(Protocol[_T_contra]):
36
42
 
37
43
 
38
44
  def bio_chunk_iter(
39
- bio: SupportsRead[bytes] | Callable[[int], bytes],
45
+ bio: SupportsRead[Buffer] | Callable[[int], Buffer],
40
46
  /,
41
47
  size: int = -1,
42
48
  chunksize: int = COPY_BUFSIZE,
43
49
  callback: None | Callable[[int], Any] = None,
44
- ) -> Iterator[bytes]:
50
+ ) -> Iterator[Buffer]:
45
51
  if callable(bio):
46
52
  read = bio
47
53
  else:
@@ -67,12 +73,12 @@ def bio_chunk_iter(
67
73
 
68
74
 
69
75
  async def bio_chunk_async_iter(
70
- bio: SupportsRead[bytes] | Callable[[int], bytes | Awaitable[bytes]],
76
+ bio: SupportsRead[Buffer] | Callable[[int], Buffer | Awaitable[Buffer]],
71
77
  /,
72
78
  size: int = -1,
73
79
  chunksize: int = COPY_BUFSIZE,
74
80
  callback: None | Callable[[int], Any] = None,
75
- ) -> AsyncIterator[bytes]:
81
+ ) -> AsyncIterator[Buffer]:
76
82
  if callable(bio):
77
83
  read = ensure_async(bio)
78
84
  else:
@@ -97,7 +103,7 @@ async def bio_chunk_async_iter(
97
103
 
98
104
 
99
105
  def bio_skip_iter(
100
- bio: SupportsRead[bytes] | Callable[[int], bytes],
106
+ bio: SupportsRead[Buffer] | Callable[[int], Buffer],
101
107
  /,
102
108
  size: int = -1,
103
109
  chunksize: int = COPY_BUFSIZE,
@@ -168,7 +174,7 @@ def bio_skip_iter(
168
174
 
169
175
 
170
176
  async def bio_skip_async_iter(
171
- bio: SupportsRead[bytes] | Callable[[int], bytes | Awaitable[bytes]],
177
+ bio: SupportsRead[Buffer] | Callable[[int], Buffer | Awaitable[Buffer]],
172
178
  /,
173
179
  size: int = -1,
174
180
  chunksize: int = COPY_BUFSIZE,
@@ -237,7 +243,7 @@ async def bio_skip_async_iter(
237
243
 
238
244
 
239
245
  def bytes_iter_to_reader(
240
- it: Iterable[bytes | bytearray],
246
+ it: Iterable[Buffer],
241
247
  /,
242
248
  ) -> SupportsRead[bytearray]:
243
249
  getnext = iter(it).__next__
@@ -325,7 +331,7 @@ def bytes_iter_to_reader(
325
331
 
326
332
 
327
333
  def bytes_iter_to_async_reader(
328
- it: Iterable[bytes | bytearray] | AsyncIterable[bytes | bytearray],
334
+ it: Iterable[Buffer] | AsyncIterable[Buffer],
329
335
  /,
330
336
  threaded: bool = True,
331
337
  ) -> SupportsRead[bytearray]:
@@ -417,9 +423,9 @@ def bytes_iter_to_async_reader(
417
423
 
418
424
 
419
425
  def bytes_to_chunk_iter(
420
- b,
426
+ b: Buffer,
421
427
  /,
422
- chunksize=1<<16,
428
+ chunksize: int = COPY_BUFSIZE,
423
429
  ) -> Iterator[memoryview]:
424
430
  m = memoryview(b)
425
431
  for i in range(0, len(m), chunksize):
@@ -427,11 +433,67 @@ def bytes_to_chunk_iter(
427
433
 
428
434
 
429
435
  async def bytes_to_chunk_async_iter(
430
- b,
436
+ b: Buffer,
431
437
  /,
432
- chunksize=1<<16,
438
+ chunksize: int = COPY_BUFSIZE,
433
439
  ) -> AsyncIterator[memoryview]:
434
440
  m = memoryview(b)
435
441
  for i in range(0, len(m), chunksize):
436
442
  yield m[i:i+chunksize]
437
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
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-filewrap
3
- Version: 0.0.5
3
+ Version: 0.0.6
4
4
  Summary: Python file wrappers.
5
5
  Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-filewrap
6
6
  License: MIT
@@ -0,0 +1,7 @@
1
+ LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
+ filewrap/__init__.py,sha256=7bcdEybE4NnEW9XHB4Cs0GoKL8YRW-1B2tqzFXXcHbs,15223
3
+ filewrap/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ python_filewrap-0.0.6.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
+ python_filewrap-0.0.6.dist-info/METADATA,sha256=m-aMbZMtnccOwY6zSFw88J06MFazBae7gP1z8vipBTQ,1362
6
+ python_filewrap-0.0.6.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
+ python_filewrap-0.0.6.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
- filewrap/__init__.py,sha256=hhvcgtUXhPdw1MP_3ujFDuhMC0PZJgqQ9sEvmebfnFc,13683
3
- filewrap/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- python_filewrap-0.0.5.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
- python_filewrap-0.0.5.dist-info/METADATA,sha256=gsU9oW_Nzw8ORTz8aKuuvLKak8AP3kwLuJJugm5kDT4,1362
6
- python_filewrap-0.0.5.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
- python_filewrap-0.0.5.dist-info/RECORD,,