python-filewrap 0.2.1__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.1
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
@@ -2,7 +2,7 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 2, 1)
5
+ __version__ = (0, 2, 2)
6
6
  __all__ = [
7
7
  "Buffer", "SupportsRead", "SupportsReadinto", "SupportsWrite", "SupportsSeek",
8
8
  "AsyncBufferedReader", "AsyncTextIOWrapper",
@@ -14,6 +14,7 @@ __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
20
  from asyncio import to_thread, Lock as AsyncLock
@@ -1793,3 +1794,57 @@ async def progress_bytes_async_iter(
1793
1794
  if callable(close_progress):
1794
1795
  await ensure_async(close_progress)()
1795
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.1"
3
+ version = "0.2.2"
4
4
  description = "Python file wrappers."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"
File without changes