python-filewrap 0.0.4__py3-none-any.whl → 0.0.5__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 +52 -31
- {python_filewrap-0.0.4.dist-info → python_filewrap-0.0.5.dist-info}/METADATA +1 -1
- python_filewrap-0.0.5.dist-info/RECORD +7 -0
- python_filewrap-0.0.4.dist-info/RECORD +0 -7
- {python_filewrap-0.0.4.dist-info → python_filewrap-0.0.5.dist-info}/LICENSE +0 -0
- {python_filewrap-0.0.4.dist-info → python_filewrap-0.0.5.dist-info}/WHEEL +0 -0
filewrap/__init__.py
CHANGED
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
# encoding: utf-8
|
|
3
3
|
|
|
4
4
|
__author__ = "ChenyangGao <https://chenyanggao.github.io>"
|
|
5
|
-
__version__ = (0, 0,
|
|
5
|
+
__version__ = (0, 0, 5)
|
|
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
12
|
]
|
|
12
13
|
|
|
13
14
|
from asyncio import to_thread, Lock as AsyncLock
|
|
@@ -238,15 +239,15 @@ async def bio_skip_async_iter(
|
|
|
238
239
|
def bytes_iter_to_reader(
|
|
239
240
|
it: Iterable[bytes | bytearray],
|
|
240
241
|
/,
|
|
241
|
-
) -> SupportsRead[
|
|
242
|
+
) -> SupportsRead[bytearray]:
|
|
242
243
|
getnext = iter(it).__next__
|
|
243
244
|
at_end = False
|
|
244
|
-
unconsumed: bytearray = bytearray(
|
|
245
|
+
unconsumed: bytearray = bytearray()
|
|
245
246
|
lock = Lock()
|
|
246
|
-
def read(n=-1, /) ->
|
|
247
|
+
def read(n=-1, /) -> bytearray:
|
|
247
248
|
nonlocal at_end, unconsumed
|
|
248
249
|
if at_end or n == 0:
|
|
249
|
-
return
|
|
250
|
+
return bytearray()
|
|
250
251
|
with lock:
|
|
251
252
|
try:
|
|
252
253
|
if n is None or n < 0:
|
|
@@ -255,11 +256,11 @@ def bytes_iter_to_reader(
|
|
|
255
256
|
else:
|
|
256
257
|
while n > len(unconsumed):
|
|
257
258
|
unconsumed += getnext()
|
|
258
|
-
b, unconsumed =
|
|
259
|
+
b, unconsumed = unconsumed[:n], unconsumed[n:]
|
|
259
260
|
return b
|
|
260
261
|
except StopIteration:
|
|
261
262
|
at_end = True
|
|
262
|
-
return
|
|
263
|
+
return unconsumed
|
|
263
264
|
def readinto(buf, /) -> int:
|
|
264
265
|
nonlocal at_end, unconsumed
|
|
265
266
|
if at_end or not (bufsize := len(buf)):
|
|
@@ -287,7 +288,7 @@ def bytes_iter_to_reader(
|
|
|
287
288
|
except StopIteration:
|
|
288
289
|
at_end = True
|
|
289
290
|
return n
|
|
290
|
-
def __next__() ->
|
|
291
|
+
def __next__() -> bytearray:
|
|
291
292
|
nonlocal unconsumed, at_end
|
|
292
293
|
if at_end:
|
|
293
294
|
raise StopIteration
|
|
@@ -295,23 +296,23 @@ def bytes_iter_to_reader(
|
|
|
295
296
|
# search for b"\n"
|
|
296
297
|
if (idx := unconsumed.find(49)) > -1:
|
|
297
298
|
idx += 1
|
|
298
|
-
b, unconsumed =
|
|
299
|
+
b, unconsumed = unconsumed[:idx], unconsumed[idx:]
|
|
299
300
|
return b
|
|
300
301
|
try:
|
|
301
302
|
while True:
|
|
302
|
-
|
|
303
|
-
if not
|
|
303
|
+
r = getnext()
|
|
304
|
+
if not r:
|
|
304
305
|
continue
|
|
305
|
-
if (idx :=
|
|
306
|
+
if (idx := r.find(49)) > -1:
|
|
306
307
|
idx += 1
|
|
307
|
-
unconsumed +=
|
|
308
|
-
b, unconsumed =
|
|
308
|
+
unconsumed += r[:idx]
|
|
309
|
+
b, unconsumed = unconsumed, bytearray(r[idx:])
|
|
309
310
|
return b
|
|
310
|
-
unconsumed +=
|
|
311
|
+
unconsumed += r
|
|
311
312
|
except StopIteration:
|
|
312
313
|
at_end = True
|
|
313
314
|
if unconsumed:
|
|
314
|
-
return
|
|
315
|
+
return unconsumed
|
|
315
316
|
raise
|
|
316
317
|
reprs = f"<reader for {it!r}>"
|
|
317
318
|
return type("reader", (), {
|
|
@@ -327,18 +328,18 @@ def bytes_iter_to_async_reader(
|
|
|
327
328
|
it: Iterable[bytes | bytearray] | AsyncIterable[bytes | bytearray],
|
|
328
329
|
/,
|
|
329
330
|
threaded: bool = True,
|
|
330
|
-
) -> SupportsRead[
|
|
331
|
+
) -> SupportsRead[bytearray]:
|
|
331
332
|
if isinstance(it, AsyncIterable):
|
|
332
333
|
getnext = aiter(it).__anext__
|
|
333
334
|
else:
|
|
334
335
|
getnext = ensure_async(iter(it).__next__, threaded=threaded)
|
|
335
336
|
at_end = False
|
|
336
|
-
unconsumed: bytearray = bytearray(
|
|
337
|
+
unconsumed: bytearray = bytearray()
|
|
337
338
|
lock = AsyncLock()
|
|
338
|
-
async def read(n=-1, /) ->
|
|
339
|
+
async def read(n=-1, /) -> bytearray:
|
|
339
340
|
nonlocal at_end, unconsumed
|
|
340
341
|
if at_end or n == 0:
|
|
341
|
-
return
|
|
342
|
+
return bytearray()
|
|
342
343
|
async with lock:
|
|
343
344
|
try:
|
|
344
345
|
if n is None or n < 0:
|
|
@@ -347,11 +348,11 @@ def bytes_iter_to_async_reader(
|
|
|
347
348
|
else:
|
|
348
349
|
while n > len(unconsumed):
|
|
349
350
|
unconsumed += await getnext()
|
|
350
|
-
b, unconsumed =
|
|
351
|
+
b, unconsumed = unconsumed[:n], unconsumed[n:]
|
|
351
352
|
return b
|
|
352
353
|
except StopAsyncIteration:
|
|
353
354
|
at_end = True
|
|
354
|
-
return
|
|
355
|
+
return unconsumed
|
|
355
356
|
async def readinto(buf, /) -> int:
|
|
356
357
|
nonlocal at_end, unconsumed
|
|
357
358
|
if at_end or not (bufsize := len(buf)):
|
|
@@ -379,7 +380,7 @@ def bytes_iter_to_async_reader(
|
|
|
379
380
|
except StopAsyncIteration:
|
|
380
381
|
at_end = True
|
|
381
382
|
return n
|
|
382
|
-
async def __next__() ->
|
|
383
|
+
async def __next__() -> bytearray:
|
|
383
384
|
nonlocal unconsumed, at_end
|
|
384
385
|
if at_end:
|
|
385
386
|
raise StopIteration
|
|
@@ -387,23 +388,23 @@ def bytes_iter_to_async_reader(
|
|
|
387
388
|
# search for b"\n"
|
|
388
389
|
if (idx := unconsumed.find(49)) > -1:
|
|
389
390
|
idx += 1
|
|
390
|
-
b, unconsumed =
|
|
391
|
+
b, unconsumed = unconsumed[:idx], unconsumed[idx:]
|
|
391
392
|
return b
|
|
392
393
|
try:
|
|
393
394
|
while True:
|
|
394
|
-
|
|
395
|
-
if not
|
|
395
|
+
r = await getnext()
|
|
396
|
+
if not r:
|
|
396
397
|
continue
|
|
397
|
-
if (idx :=
|
|
398
|
+
if (idx := r.find(49)) > -1:
|
|
398
399
|
idx += 1
|
|
399
|
-
unconsumed +=
|
|
400
|
-
b, unconsumed =
|
|
400
|
+
unconsumed += r[:idx]
|
|
401
|
+
b, unconsumed = unconsumed, bytearray(r[idx:])
|
|
401
402
|
return b
|
|
402
|
-
unconsumed +=
|
|
403
|
+
unconsumed += r
|
|
403
404
|
except StopIteration:
|
|
404
405
|
at_end = True
|
|
405
406
|
if unconsumed:
|
|
406
|
-
return
|
|
407
|
+
return unconsumed
|
|
407
408
|
raise
|
|
408
409
|
reprs = f"<reader for {it!r}>"
|
|
409
410
|
return type("reader", (), {
|
|
@@ -414,3 +415,23 @@ def bytes_iter_to_async_reader(
|
|
|
414
415
|
"__repr__": staticmethod(lambda: reprs),
|
|
415
416
|
})()
|
|
416
417
|
|
|
418
|
+
|
|
419
|
+
def bytes_to_chunk_iter(
|
|
420
|
+
b,
|
|
421
|
+
/,
|
|
422
|
+
chunksize=1<<16,
|
|
423
|
+
) -> Iterator[memoryview]:
|
|
424
|
+
m = memoryview(b)
|
|
425
|
+
for i in range(0, len(m), chunksize):
|
|
426
|
+
yield m[i:i+chunksize]
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
async def bytes_to_chunk_async_iter(
|
|
430
|
+
b,
|
|
431
|
+
/,
|
|
432
|
+
chunksize=1<<16,
|
|
433
|
+
) -> AsyncIterator[memoryview]:
|
|
434
|
+
m = memoryview(b)
|
|
435
|
+
for i in range(0, len(m), chunksize):
|
|
436
|
+
yield m[i:i+chunksize]
|
|
437
|
+
|
|
@@ -0,0 +1,7 @@
|
|
|
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,,
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
2
|
-
filewrap/__init__.py,sha256=28PmcNRaPhSDiWpDStAHTGguChOYdxRfAfezwLlH9z8,13273
|
|
3
|
-
filewrap/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
|
-
python_filewrap-0.0.4.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
|
|
5
|
-
python_filewrap-0.0.4.dist-info/METADATA,sha256=K12tl1IE20BhHOIhUhJXx0v-l5g1ws3_6S1iC1gLWB8,1362
|
|
6
|
-
python_filewrap-0.0.4.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
|
|
7
|
-
python_filewrap-0.0.4.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|