METADATA 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. Metadata-Version: 2.1
  2. Name: aiofiles
  3. Version: 0.8.0
  4. Summary: File support for asyncio.
  5. License: Apache-2.0
  6. Author: Tin Tvrtkovic
  7. Author-email: tinchester@gmail.com
  8. Requires-Python: >=3.6,<4.0
  9. Classifier: License :: OSI Approved :: Apache Software License
  10. Classifier: Programming Language :: Python :: 3
  11. Classifier: Programming Language :: Python :: 3.6
  12. Classifier: Programming Language :: Python :: 3.7
  13. Classifier: Programming Language :: Python :: 3.8
  14. Classifier: Programming Language :: Python :: 3.9
  15. Classifier: Programming Language :: Python :: 3.10
  16. Description-Content-Type: text/x-rst
  17. aiofiles: file support for asyncio
  18. ==================================
  19. .. image:: https://img.shields.io/pypi/v/aiofiles.svg
  20. :target: https://pypi.python.org/pypi/aiofiles
  21. .. image:: https://travis-ci.org/Tinche/aiofiles.svg?branch=master
  22. :target: https://travis-ci.org/Tinche/aiofiles
  23. .. image:: https://codecov.io/gh/Tinche/aiofiles/branch/master/graph/badge.svg
  24. :target: https://codecov.io/gh/Tinche/aiofiles
  25. .. image:: https://img.shields.io/pypi/pyversions/aiofiles.svg
  26. :target: https://github.com/Tinche/aiofiles
  27. :alt: Supported Python versions
  28. **aiofiles** is an Apache2 licensed library, written in Python, for handling local
  29. disk files in asyncio applications.
  30. Ordinary local file IO is blocking, and cannot easily and portably made
  31. asynchronous. This means doing file IO may interfere with asyncio applications,
  32. which shouldn't block the executing thread. aiofiles helps with this by
  33. introducing asynchronous versions of files that support delegating operations to
  34. a separate thread pool.
  35. .. code-block:: python
  36. async with aiofiles.open('filename', mode='r') as f:
  37. contents = await f.read()
  38. print(contents)
  39. 'My file contents'
  40. Asynchronous iteration is also supported.
  41. .. code-block:: python
  42. async with aiofiles.open('filename') as f:
  43. async for line in f:
  44. ...
  45. Asynchronous interface to tempfile module.
  46. .. code-block:: python
  47. async with aiofiles.tempfile.TemporaryFile('wb') as f:
  48. await f.write(b'Hello, World!')
  49. Features
  50. --------
  51. - a file API very similar to Python's standard, blocking API
  52. - support for buffered and unbuffered binary files, and buffered text files
  53. - support for ``async``/``await`` (:PEP:`492`) constructs
  54. - async interface to tempfile module
  55. Installation
  56. ------------
  57. To install aiofiles, simply:
  58. .. code-block:: bash
  59. $ pip install aiofiles
  60. Usage
  61. -----
  62. Files are opened using the ``aiofiles.open()`` coroutine, which in addition to
  63. mirroring the builtin ``open`` accepts optional ``loop`` and ``executor``
  64. arguments. If ``loop`` is absent, the default loop will be used, as per the
  65. set asyncio policy. If ``executor`` is not specified, the default event loop
  66. executor will be used.
  67. In case of success, an asynchronous file object is returned with an
  68. API identical to an ordinary file, except the following methods are coroutines
  69. and delegate to an executor:
  70. * ``close``
  71. * ``flush``
  72. * ``isatty``
  73. * ``read``
  74. * ``readall``
  75. * ``read1``
  76. * ``readinto``
  77. * ``readline``
  78. * ``readlines``
  79. * ``seek``
  80. * ``seekable``
  81. * ``tell``
  82. * ``truncate``
  83. * ``writable``
  84. * ``write``
  85. * ``writelines``
  86. In case of failure, one of the usual exceptions will be raised.
  87. The ``aiofiles.os`` module contains executor-enabled coroutine versions of
  88. several useful ``os`` functions that deal with files:
  89. * ``stat``
  90. * ``sendfile``
  91. * ``rename``
  92. * ``replace``
  93. * ``remove``
  94. * ``mkdir``
  95. * ``makedirs``
  96. * ``rmdir``
  97. * ``removedirs``
  98. * ``path.exists``
  99. * ``path.isfile``
  100. * ``path.isdir``
  101. * ``path.getsize``
  102. * ``path.getatime``
  103. * ``path.getctime``
  104. * ``path.samefile``
  105. * ``path.sameopenfile``
  106. Tempfile
  107. ~~~~~~~~
  108. **aiofiles.tempfile** implements the following interfaces:
  109. - TemporaryFile
  110. - NamedTemporaryFile
  111. - SpooledTemporaryFile
  112. - TemporaryDirectory
  113. Results return wrapped with a context manager allowing use with async with and async for.
  114. .. code-block:: python
  115. async with aiofiles.tempfile.NamedTemporaryFile('wb+') as f:
  116. await f.write(b'Line1\n Line2')
  117. await f.seek(0)
  118. async for line in f:
  119. print(line)
  120. async with aiofiles.tempfile.TemporaryDirectory() as d:
  121. filename = os.path.join(d, "file.ext")
  122. Writing tests for aiofiles
  123. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  124. Real file IO can be mocked by patching ``aiofiles.threadpool.sync_open``
  125. as desired. The return type also needs to be registered with the
  126. ``aiofiles.threadpool.wrap`` dispatcher:
  127. .. code-block:: python
  128. aiofiles.threadpool.wrap.register(mock.MagicMock)(
  129. lambda *args, **kwargs: threadpool.AsyncBufferedIOBase(*args, **kwargs))
  130. async def test_stuff():
  131. data = 'data'
  132. mock_file = mock.MagicMock()
  133. with mock.patch('aiofiles.threadpool.sync_open', return_value=mock_file) as mock_open:
  134. async with aiofiles.open('filename', 'w') as f:
  135. await f.write(data)
  136. mock_file.write.assert_called_once_with(data)
  137. History
  138. ~~~~~~~
  139. 0.8.0 (2021-11-27)
  140. ``````````````````
  141. * aiofiles is now tested on Python 3.10.
  142. * Added ``aiofiles.os.replace``.
  143. `#107 <https://github.com/Tinche/aiofiles/pull/107>`_
  144. * Added ``aiofiles.os.{makedirs, removedirs}``.
  145. * Added ``aiofiles.os.path.{exists, isfile, isdir, getsize, getatime, getctime, samefile, sameopenfile}``.
  146. `#63 <https://github.com/Tinche/aiofiles/pull/63>`_
  147. * Added `suffix`, `prefix`, `dir` args to ``aiofiles.tempfile.TemporaryDirectory``.
  148. `#116 <https://github.com/Tinche/aiofiles/pull/116>`_
  149. 0.7.0 (2021-05-17)
  150. ``````````````````
  151. - Added the ``aiofiles.tempfile`` module for async temporary files.
  152. `#56 <https://github.com/Tinche/aiofiles/pull/56>`_
  153. - Switched to Poetry and GitHub actions.
  154. - Dropped 3.5 support.
  155. 0.6.0 (2020-10-27)
  156. ``````````````````
  157. - `aiofiles` is now tested on ppc64le.
  158. - Added `name` and `mode` properties to async file objects.
  159. `#82 <https://github.com/Tinche/aiofiles/pull/82>`_
  160. - Fixed a DeprecationWarning internally.
  161. `#75 <https://github.com/Tinche/aiofiles/pull/75>`_
  162. - Python 3.9 support and tests.
  163. 0.5.0 (2020-04-12)
  164. ``````````````````
  165. - Python 3.8 support. Code base modernization (using ``async/await`` instead of ``asyncio.coroutine``/``yield from``).
  166. - Added ``aiofiles.os.remove``, ``aiofiles.os.rename``, ``aiofiles.os.mkdir``, ``aiofiles.os.rmdir``.
  167. `#62 <https://github.com/Tinche/aiofiles/pull/62>`_
  168. 0.4.0 (2018-08-11)
  169. ``````````````````
  170. - Python 3.7 support.
  171. - Removed Python 3.3/3.4 support. If you use these versions, stick to aiofiles 0.3.x.
  172. 0.3.2 (2017-09-23)
  173. ``````````````````
  174. - The LICENSE is now included in the sdist.
  175. `#31 <https://github.com/Tinche/aiofiles/pull/31>`_
  176. 0.3.1 (2017-03-10)
  177. ``````````````````
  178. - Introduced a changelog.
  179. - ``aiofiles.os.sendfile`` will now work if the standard ``os`` module contains a ``sendfile`` function.
  180. Contributing
  181. ~~~~~~~~~~~~
  182. Contributions are very welcome. Tests can be run with ``tox``, please ensure
  183. the coverage at least stays the same before you submit a pull request.