METADATA 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. Metadata-Version: 2.3
  2. Name: aiofiles
  3. Version: 24.1.0
  4. Summary: File support for asyncio.
  5. Project-URL: Changelog, https://github.com/Tinche/aiofiles#history
  6. Project-URL: Bug Tracker, https://github.com/Tinche/aiofiles/issues
  7. Project-URL: repository, https://github.com/Tinche/aiofiles
  8. Author-email: Tin Tvrtkovic <tinchester@gmail.com>
  9. License: Apache-2.0
  10. License-File: LICENSE
  11. License-File: NOTICE
  12. Classifier: Development Status :: 5 - Production/Stable
  13. Classifier: Framework :: AsyncIO
  14. Classifier: License :: OSI Approved :: Apache Software License
  15. Classifier: Operating System :: OS Independent
  16. Classifier: Programming Language :: Python :: 3.8
  17. Classifier: Programming Language :: Python :: 3.9
  18. Classifier: Programming Language :: Python :: 3.10
  19. Classifier: Programming Language :: Python :: 3.11
  20. Classifier: Programming Language :: Python :: 3.12
  21. Classifier: Programming Language :: Python :: 3.13
  22. Classifier: Programming Language :: Python :: Implementation :: CPython
  23. Classifier: Programming Language :: Python :: Implementation :: PyPy
  24. Requires-Python: >=3.8
  25. Description-Content-Type: text/markdown
  26. # aiofiles: file support for asyncio
  27. [![PyPI](https://img.shields.io/pypi/v/aiofiles.svg)](https://pypi.python.org/pypi/aiofiles)
  28. [![Build](https://github.com/Tinche/aiofiles/workflows/CI/badge.svg)](https://github.com/Tinche/aiofiles/actions)
  29. [![Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/Tinche/882f02e3df32136c847ba90d2688f06e/raw/covbadge.json)](https://github.com/Tinche/aiofiles/actions/workflows/main.yml)
  30. [![Supported Python versions](https://img.shields.io/pypi/pyversions/aiofiles.svg)](https://github.com/Tinche/aiofiles)
  31. [![Black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
  32. **aiofiles** is an Apache2 licensed library, written in Python, for handling local
  33. disk files in asyncio applications.
  34. Ordinary local file IO is blocking, and cannot easily and portably be made
  35. asynchronous. This means doing file IO may interfere with asyncio applications,
  36. which shouldn't block the executing thread. aiofiles helps with this by
  37. introducing asynchronous versions of files that support delegating operations to
  38. a separate thread pool.
  39. ```python
  40. async with aiofiles.open('filename', mode='r') as f:
  41. contents = await f.read()
  42. print(contents)
  43. 'My file contents'
  44. ```
  45. Asynchronous iteration is also supported.
  46. ```python
  47. async with aiofiles.open('filename') as f:
  48. async for line in f:
  49. ...
  50. ```
  51. Asynchronous interface to tempfile module.
  52. ```python
  53. async with aiofiles.tempfile.TemporaryFile('wb') as f:
  54. await f.write(b'Hello, World!')
  55. ```
  56. ## Features
  57. - a file API very similar to Python's standard, blocking API
  58. - support for buffered and unbuffered binary files, and buffered text files
  59. - support for `async`/`await` ([PEP 492](https://peps.python.org/pep-0492/)) constructs
  60. - async interface to tempfile module
  61. ## Installation
  62. To install aiofiles, simply:
  63. ```bash
  64. $ pip install aiofiles
  65. ```
  66. ## Usage
  67. Files are opened using the `aiofiles.open()` coroutine, which in addition to
  68. mirroring the builtin `open` accepts optional `loop` and `executor`
  69. arguments. If `loop` is absent, the default loop will be used, as per the
  70. set asyncio policy. If `executor` is not specified, the default event loop
  71. executor will be used.
  72. In case of success, an asynchronous file object is returned with an
  73. API identical to an ordinary file, except the following methods are coroutines
  74. and delegate to an executor:
  75. - `close`
  76. - `flush`
  77. - `isatty`
  78. - `read`
  79. - `readall`
  80. - `read1`
  81. - `readinto`
  82. - `readline`
  83. - `readlines`
  84. - `seek`
  85. - `seekable`
  86. - `tell`
  87. - `truncate`
  88. - `writable`
  89. - `write`
  90. - `writelines`
  91. In case of failure, one of the usual exceptions will be raised.
  92. `aiofiles.stdin`, `aiofiles.stdout`, `aiofiles.stderr`,
  93. `aiofiles.stdin_bytes`, `aiofiles.stdout_bytes`, and
  94. `aiofiles.stderr_bytes` provide async access to `sys.stdin`,
  95. `sys.stdout`, `sys.stderr`, and their corresponding `.buffer` properties.
  96. The `aiofiles.os` module contains executor-enabled coroutine versions of
  97. several useful `os` functions that deal with files:
  98. - `stat`
  99. - `statvfs`
  100. - `sendfile`
  101. - `rename`
  102. - `renames`
  103. - `replace`
  104. - `remove`
  105. - `unlink`
  106. - `mkdir`
  107. - `makedirs`
  108. - `rmdir`
  109. - `removedirs`
  110. - `link`
  111. - `symlink`
  112. - `readlink`
  113. - `listdir`
  114. - `scandir`
  115. - `access`
  116. - `getcwd`
  117. - `path.abspath`
  118. - `path.exists`
  119. - `path.isfile`
  120. - `path.isdir`
  121. - `path.islink`
  122. - `path.ismount`
  123. - `path.getsize`
  124. - `path.getatime`
  125. - `path.getctime`
  126. - `path.samefile`
  127. - `path.sameopenfile`
  128. ### Tempfile
  129. **aiofiles.tempfile** implements the following interfaces:
  130. - TemporaryFile
  131. - NamedTemporaryFile
  132. - SpooledTemporaryFile
  133. - TemporaryDirectory
  134. Results return wrapped with a context manager allowing use with async with and async for.
  135. ```python
  136. async with aiofiles.tempfile.NamedTemporaryFile('wb+') as f:
  137. await f.write(b'Line1\n Line2')
  138. await f.seek(0)
  139. async for line in f:
  140. print(line)
  141. async with aiofiles.tempfile.TemporaryDirectory() as d:
  142. filename = os.path.join(d, "file.ext")
  143. ```
  144. ### Writing tests for aiofiles
  145. Real file IO can be mocked by patching `aiofiles.threadpool.sync_open`
  146. as desired. The return type also needs to be registered with the
  147. `aiofiles.threadpool.wrap` dispatcher:
  148. ```python
  149. aiofiles.threadpool.wrap.register(mock.MagicMock)(
  150. lambda *args, **kwargs: aiofiles.threadpool.AsyncBufferedIOBase(*args, **kwargs)
  151. )
  152. async def test_stuff():
  153. write_data = 'data'
  154. read_file_chunks = [
  155. b'file chunks 1',
  156. b'file chunks 2',
  157. b'file chunks 3',
  158. b'',
  159. ]
  160. file_chunks_iter = iter(read_file_chunks)
  161. mock_file_stream = mock.MagicMock(
  162. read=lambda *args, **kwargs: next(file_chunks_iter)
  163. )
  164. with mock.patch('aiofiles.threadpool.sync_open', return_value=mock_file_stream) as mock_open:
  165. async with aiofiles.open('filename', 'w') as f:
  166. await f.write(write_data)
  167. assert f.read() == b'file chunks 1'
  168. mock_file_stream.write.assert_called_once_with(write_data)
  169. ```
  170. ### History
  171. #### 24.1.0 (2024-06-24)
  172. - Import `os.link` conditionally to fix importing on android.
  173. [#175](https://github.com/Tinche/aiofiles/issues/175)
  174. - Remove spurious items from `aiofiles.os.__all__` when running on Windows.
  175. - Switch to more modern async idioms: Remove types.coroutine and make AiofilesContextManager an awaitable instead a coroutine.
  176. - Add `aiofiles.os.path.abspath` and `aiofiles.os.getcwd`.
  177. [#174](https://github.com/Tinche/aiofiles/issues/181)
  178. - _aiofiles_ is now tested on Python 3.13 too.
  179. [#184](https://github.com/Tinche/aiofiles/pull/184)
  180. - Dropped Python 3.7 support. If you require it, use version 23.2.1.
  181. #### 23.2.1 (2023-08-09)
  182. - Import `os.statvfs` conditionally to fix importing on non-UNIX systems.
  183. [#171](https://github.com/Tinche/aiofiles/issues/171) [#172](https://github.com/Tinche/aiofiles/pull/172)
  184. - aiofiles is now also tested on Windows.
  185. #### 23.2.0 (2023-08-09)
  186. - aiofiles is now tested on Python 3.12 too.
  187. [#166](https://github.com/Tinche/aiofiles/issues/166) [#168](https://github.com/Tinche/aiofiles/pull/168)
  188. - On Python 3.12, `aiofiles.tempfile.NamedTemporaryFile` now accepts a `delete_on_close` argument, just like the stdlib version.
  189. - On Python 3.12, `aiofiles.tempfile.NamedTemporaryFile` no longer exposes a `delete` attribute, just like the stdlib version.
  190. - Added `aiofiles.os.statvfs` and `aiofiles.os.path.ismount`.
  191. [#162](https://github.com/Tinche/aiofiles/pull/162)
  192. - Use [PDM](https://pdm.fming.dev/latest/) instead of Poetry.
  193. [#169](https://github.com/Tinche/aiofiles/pull/169)
  194. #### 23.1.0 (2023-02-09)
  195. - Added `aiofiles.os.access`.
  196. [#146](https://github.com/Tinche/aiofiles/pull/146)
  197. - Removed `aiofiles.tempfile.temptypes.AsyncSpooledTemporaryFile.softspace`.
  198. [#151](https://github.com/Tinche/aiofiles/pull/151)
  199. - Added `aiofiles.stdin`, `aiofiles.stdin_bytes`, and other stdio streams.
  200. [#154](https://github.com/Tinche/aiofiles/pull/154)
  201. - Transition to `asyncio.get_running_loop` (vs `asyncio.get_event_loop`) internally.
  202. #### 22.1.0 (2022-09-04)
  203. - Added `aiofiles.os.path.islink`.
  204. [#126](https://github.com/Tinche/aiofiles/pull/126)
  205. - Added `aiofiles.os.readlink`.
  206. [#125](https://github.com/Tinche/aiofiles/pull/125)
  207. - Added `aiofiles.os.symlink`.
  208. [#124](https://github.com/Tinche/aiofiles/pull/124)
  209. - Added `aiofiles.os.unlink`.
  210. [#123](https://github.com/Tinche/aiofiles/pull/123)
  211. - Added `aiofiles.os.link`.
  212. [#121](https://github.com/Tinche/aiofiles/pull/121)
  213. - Added `aiofiles.os.renames`.
  214. [#120](https://github.com/Tinche/aiofiles/pull/120)
  215. - Added `aiofiles.os.{listdir, scandir}`.
  216. [#143](https://github.com/Tinche/aiofiles/pull/143)
  217. - Switched to CalVer.
  218. - Dropped Python 3.6 support. If you require it, use version 0.8.0.
  219. - aiofiles is now tested on Python 3.11.
  220. #### 0.8.0 (2021-11-27)
  221. - aiofiles is now tested on Python 3.10.
  222. - Added `aiofiles.os.replace`.
  223. [#107](https://github.com/Tinche/aiofiles/pull/107)
  224. - Added `aiofiles.os.{makedirs, removedirs}`.
  225. - Added `aiofiles.os.path.{exists, isfile, isdir, getsize, getatime, getctime, samefile, sameopenfile}`.
  226. [#63](https://github.com/Tinche/aiofiles/pull/63)
  227. - Added `suffix`, `prefix`, `dir` args to `aiofiles.tempfile.TemporaryDirectory`.
  228. [#116](https://github.com/Tinche/aiofiles/pull/116)
  229. #### 0.7.0 (2021-05-17)
  230. - Added the `aiofiles.tempfile` module for async temporary files.
  231. [#56](https://github.com/Tinche/aiofiles/pull/56)
  232. - Switched to Poetry and GitHub actions.
  233. - Dropped 3.5 support.
  234. #### 0.6.0 (2020-10-27)
  235. - `aiofiles` is now tested on ppc64le.
  236. - Added `name` and `mode` properties to async file objects.
  237. [#82](https://github.com/Tinche/aiofiles/pull/82)
  238. - Fixed a DeprecationWarning internally.
  239. [#75](https://github.com/Tinche/aiofiles/pull/75)
  240. - Python 3.9 support and tests.
  241. #### 0.5.0 (2020-04-12)
  242. - Python 3.8 support. Code base modernization (using `async/await` instead of `asyncio.coroutine`/`yield from`).
  243. - Added `aiofiles.os.remove`, `aiofiles.os.rename`, `aiofiles.os.mkdir`, `aiofiles.os.rmdir`.
  244. [#62](https://github.com/Tinche/aiofiles/pull/62)
  245. #### 0.4.0 (2018-08-11)
  246. - Python 3.7 support.
  247. - Removed Python 3.3/3.4 support. If you use these versions, stick to aiofiles 0.3.x.
  248. #### 0.3.2 (2017-09-23)
  249. - The LICENSE is now included in the sdist.
  250. [#31](https://github.com/Tinche/aiofiles/pull/31)
  251. #### 0.3.1 (2017-03-10)
  252. - Introduced a changelog.
  253. - `aiofiles.os.sendfile` will now work if the standard `os` module contains a `sendfile` function.
  254. ### Contributing
  255. Contributions are very welcome. Tests can be run with `tox`, please ensure
  256. the coverage at least stays the same before you submit a pull request.