python-filewrap 0.2__tar.gz → 0.2.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-filewrap
3
- Version: 0.2
3
+ Version: 0.2.2
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
@@ -21,7 +21,7 @@ Classifier: Programming Language :: Python :: 3 :: Only
21
21
  Classifier: Topic :: Software Development
22
22
  Classifier: Topic :: Software Development :: Libraries
23
23
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
- Requires-Dist: python-asynctools (>=0.0.4)
24
+ Requires-Dist: python-asynctools (>=0.0.5)
25
25
  Requires-Dist: python-property (>=0.0.2)
26
26
  Project-URL: Repository, https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-filewrap
27
27
  Description-Content-Type: text/markdown
@@ -2,7 +2,7 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 2)
5
+ __version__ = (0, 2, 2)
6
6
  __all__ = [
7
7
  "Buffer", "SupportsRead", "SupportsReadinto", "SupportsWrite", "SupportsSeek",
8
8
  "AsyncBufferedReader", "AsyncTextIOWrapper",
@@ -14,9 +14,10 @@ __all__ = [
14
14
  "bytes_to_chunk_iter", "bytes_to_chunk_async_iter",
15
15
  "bytes_ensure_part_iter", "bytes_ensure_part_async_iter",
16
16
  "progress_bytes_iter", "progress_bytes_async_iter",
17
+ "copyfileobj", "copyfileobj_async",
17
18
  ]
18
19
 
19
- from asyncio import run as run_async, to_thread, Lock as AsyncLock
20
+ from asyncio import to_thread, Lock as AsyncLock
20
21
  from collections.abc import Awaitable, AsyncIterable, AsyncIterator, Callable, Iterable, Iterator
21
22
  from functools import update_wrapper
22
23
  from io import BufferedIOBase, BufferedReader, BytesIO, RawIOBase, TextIOWrapper
@@ -45,7 +46,7 @@ except ImportError:
45
46
  Buffer.register(_SimpleCData)
46
47
  Buffer.register(array)
47
48
 
48
- from asynctools import async_chain, ensure_async, ensure_aiter, ensure_coroutine
49
+ from asynctools import async_chain, ensure_async, ensure_aiter, run_async
49
50
  from property import staticproperty
50
51
 
51
52
 
@@ -164,7 +165,7 @@ class AsyncBufferedReader(BufferedReader):
164
165
  except (AttributeError, TypeError):
165
166
  ret = getattr(raw, "close")()
166
167
  if isawaitable(ret):
167
- run_async(ensure_coroutine(ret))
168
+ run_async(ret)
168
169
 
169
170
  async def flush(self, /):
170
171
  return await ensure_async(self.raw.flush, threaded=True)()
@@ -588,7 +589,7 @@ class AsyncTextIOWrapper(TextIOWrapper):
588
589
  except (AttributeError, TypeError):
589
590
  ret = getattr(buffer, "close")()
590
591
  if isawaitable(ret):
591
- run_async(ensure_coroutine(ret))
592
+ run_async(ret)
592
593
 
593
594
  async def flush(self, /):
594
595
  return await ensure_async(self.buffer.flush, threaded=True)()
@@ -1476,14 +1477,17 @@ def bytes_iter_to_async_reader(
1476
1477
  except:
1477
1478
  pass
1478
1479
  def close():
1480
+ nonlocal at_end
1479
1481
  try:
1480
1482
  method = getattr(it, "aclose")
1481
1483
  except AttributeError:
1482
1484
  method = getattr(it, "close")
1483
1485
  ret = method()
1484
1486
  if isawaitable(ret):
1485
- run_async(ensure_coroutine(ret))
1487
+ run_async(ret)
1488
+ at_end = True
1486
1489
  async def aclose():
1490
+ nonlocal at_end
1487
1491
  try:
1488
1492
  method = getattr(it, "aclose")
1489
1493
  except AttributeError:
@@ -1491,6 +1495,7 @@ def bytes_iter_to_async_reader(
1491
1495
  ret = method()
1492
1496
  if isawaitable(ret):
1493
1497
  await ret
1498
+ at_end = True
1494
1499
  def peek(n: int = 0, /) -> bytearray:
1495
1500
  if n <= 0:
1496
1501
  return unconsumed[:]
@@ -1789,3 +1794,57 @@ async def progress_bytes_async_iter(
1789
1794
  if callable(close_progress):
1790
1795
  await ensure_async(close_progress)()
1791
1796
 
1797
+
1798
+ def copyfileobj(
1799
+ fsrc,
1800
+ fdst: SupportsWrite[Buffer],
1801
+ /,
1802
+ chunksize: int = COPY_BUFSIZE,
1803
+ ):
1804
+ if chunksize <= 0:
1805
+ chunksize = COPY_BUFSIZE
1806
+ fdst_write = fdst.write
1807
+ fsrc_read = getattr(fsrc, "read", None)
1808
+ fsrc_readinto = getattr(fsrc, "readinto", None)
1809
+ if callable(fsrc_readinto):
1810
+ buf = bytearray(chunksize)
1811
+ view = memoryview(buf)
1812
+ while size := fsrc_readinto(buf):
1813
+ fdst_write(view[:size])
1814
+ elif callable(fsrc_read):
1815
+ while chunk := fsrc_read(chunksize):
1816
+ fdst_write(chunk)
1817
+ else:
1818
+ for chunk in fsrc:
1819
+ if chunk:
1820
+ fdst_write(chunk)
1821
+
1822
+
1823
+ async def copyfileobj_async(
1824
+ fsrc,
1825
+ fdst: SupportsWrite[Buffer],
1826
+ /,
1827
+ chunksize: int = COPY_BUFSIZE,
1828
+ threaded: bool = True,
1829
+ ):
1830
+ if chunksize <= 0:
1831
+ chunksize = COPY_BUFSIZE
1832
+ fdst_write = ensure_async(fdst.write, threaded=threaded)
1833
+ fsrc_read = getattr(fsrc, "read", None)
1834
+ fsrc_readinto = getattr(fsrc, "readinto", None)
1835
+ if callable(fsrc_readinto):
1836
+ fsrc_readinto = ensure_async(fsrc_readinto, threaded=threaded)
1837
+ buf = bytearray(chunksize)
1838
+ view = memoryview(buf)
1839
+ while size := await fsrc_readinto(buf):
1840
+ await fdst_write(view[:size])
1841
+ elif callable(fsrc_read):
1842
+ fsrc_read = ensure_async(fsrc_read, threaded=threaded)
1843
+ while chunk := await fsrc_read(chunksize):
1844
+ await fdst_write(chunk)
1845
+ else:
1846
+ chunkiter = ensure_aiter(fsrc, threaded=threaded)
1847
+ async for chunk in chunkiter:
1848
+ if chunk:
1849
+ await fdst_write(chunk)
1850
+
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-filewrap"
3
- version = "0.2"
3
+ version = "0.2.2"
4
4
  description = "Python file wrappers."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"
@@ -27,7 +27,7 @@ include = [
27
27
 
28
28
  [tool.poetry.dependencies]
29
29
  python = "^3.10"
30
- python-asynctools = ">=0.0.4"
30
+ python-asynctools = ">=0.0.5"
31
31
  python-property = ">=0.0.2"
32
32
 
33
33
  [build-system]
File without changes
File without changes