diff --git a/Makefile b/Makefile index c58ab5b..4590f98 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,4 @@ test: pdm run pytest -x --ff tests lint: - pdm run flake8 src tests && pdm run black --check --verbose src tests + pdm run flake8 src tests && pdm run black --check src tests diff --git a/src/aiofiles/os.py b/src/aiofiles/os.py index cedd05a..6da4255 100644 --- a/src/aiofiles/os.py +++ b/src/aiofiles/os.py @@ -1,21 +1,31 @@ """Async executor versions of file functions from the os module.""" -import asyncio -from functools import partial, wraps import os - -def wrap(func): - @wraps(func) - async def run(*args, loop=None, executor=None, **kwargs): - if loop is None: - loop = asyncio.get_running_loop() - pfunc = partial(func, *args, **kwargs) - return await loop.run_in_executor(executor, pfunc) - - return run - - from . import ospath as path +from .ospath import wrap + +__all__ = [ + "path", + "stat", + "statvfs", + "rename", + "renames", + "replace", + "remove", + "unlink", + "mkdir", + "makedirs", + "rmdir", + "removedirs", + "link", + "symlink", + "readlink", + "listdir", + "scandir", + "access", + "sendfile", + "wrap", +] stat = wrap(os.stat) diff --git a/src/aiofiles/ospath.py b/src/aiofiles/ospath.py index 2a4bb3c..5f32a43 100644 --- a/src/aiofiles/ospath.py +++ b/src/aiofiles/ospath.py @@ -1,8 +1,20 @@ """Async executor versions of file functions from the os.path module.""" - -from .os import wrap +import asyncio +from functools import partial, wraps from os import path + +def wrap(func): + @wraps(func) + async def run(*args, loop=None, executor=None, **kwargs): + if loop is None: + loop = asyncio.get_running_loop() + pfunc = partial(func, *args, **kwargs) + return await loop.run_in_executor(executor, pfunc) + + return run + + exists = wrap(path.exists) isfile = wrap(path.isfile) isdir = wrap(path.isdir) diff --git a/src/aiofiles/tempfile/temptypes.py b/src/aiofiles/tempfile/temptypes.py index 0e7ddad..1a1b1a8 100644 --- a/src/aiofiles/tempfile/temptypes.py +++ b/src/aiofiles/tempfile/temptypes.py @@ -1,16 +1,12 @@ """Async wrappers for spooled temp files and temp directory objects""" - -# Imports -import asyncio -from types import coroutine +from functools import partial from ..base import AsyncBase from ..threadpool.utils import ( + cond_delegate_to_executor, delegate_to_executor, proxy_property_directly, - cond_delegate_to_executor, ) -from functools import partial @delegate_to_executor("fileno", "rollover") diff --git a/src/aiofiles/threadpool/__init__.py b/src/aiofiles/threadpool/__init__.py index aff956f..a1cc673 100644 --- a/src/aiofiles/threadpool/__init__.py +++ b/src/aiofiles/threadpool/__init__.py @@ -1,28 +1,25 @@ """Handle files using a thread pool executor.""" import asyncio import sys -from types import coroutine - +from functools import partial, singledispatch from io import ( - FileIO, - TextIOBase, + BufferedIOBase, + BufferedRandom, BufferedReader, BufferedWriter, - BufferedRandom, - BufferedIOBase, + FileIO, + TextIOBase, ) -from functools import partial, singledispatch +from types import coroutine +from ..base import AiofilesContextManager from .binary import ( AsyncBufferedIOBase, AsyncBufferedReader, AsyncFileIO, AsyncIndirectBufferedIOBase, - AsyncIndirectBufferedReader, - AsyncIndirectFileIO, ) -from .text import AsyncTextIOWrapper, AsyncTextIndirectIOWrapper -from ..base import AiofilesContextManager +from .text import AsyncTextIndirectIOWrapper, AsyncTextIOWrapper sync_open = open @@ -48,7 +45,7 @@ def open( opener=None, *, loop=None, - executor=None + executor=None, ): return AiofilesContextManager( _open( @@ -78,7 +75,7 @@ def _open( opener=None, *, loop=None, - executor=None + executor=None, ): """Open an asyncio file.""" if loop is None: @@ -126,9 +123,19 @@ def _(file, *, loop=None, executor=None): return AsyncFileIO(file, loop=loop, executor=executor) -stdin = AsyncTextIndirectIOWrapper('sys.stdin', None, None, indirect=lambda: sys.stdin) -stdout = AsyncTextIndirectIOWrapper('sys.stdout', None, None, indirect=lambda: sys.stdout) -stderr = AsyncTextIndirectIOWrapper('sys.stderr', None, None, indirect=lambda: sys.stderr) -stdin_bytes = AsyncIndirectBufferedIOBase('sys.stdin.buffer', None, None, indirect=lambda: sys.stdin.buffer) -stdout_bytes = AsyncIndirectBufferedIOBase('sys.stdout.buffer', None, None, indirect=lambda: sys.stdout.buffer) -stderr_bytes = AsyncIndirectBufferedIOBase('sys.stderr.buffer', None, None, indirect=lambda: sys.stderr.buffer) +stdin = AsyncTextIndirectIOWrapper("sys.stdin", None, None, indirect=lambda: sys.stdin) +stdout = AsyncTextIndirectIOWrapper( + "sys.stdout", None, None, indirect=lambda: sys.stdout +) +stderr = AsyncTextIndirectIOWrapper( + "sys.stderr", None, None, indirect=lambda: sys.stderr +) +stdin_bytes = AsyncIndirectBufferedIOBase( + "sys.stdin.buffer", None, None, indirect=lambda: sys.stdin.buffer +) +stdout_bytes = AsyncIndirectBufferedIOBase( + "sys.stdout.buffer", None, None, indirect=lambda: sys.stdout.buffer +) +stderr_bytes = AsyncIndirectBufferedIOBase( + "sys.stderr.buffer", None, None, indirect=lambda: sys.stderr.buffer +) diff --git a/src/aiofiles/threadpool/binary.py b/src/aiofiles/threadpool/binary.py index 52d0cb3..63fcaff 100644 --- a/src/aiofiles/threadpool/binary.py +++ b/src/aiofiles/threadpool/binary.py @@ -1,9 +1,5 @@ from ..base import AsyncBase, AsyncIndirectBase -from .utils import ( - delegate_to_executor, - proxy_method_directly, - proxy_property_directly, -) +from .utils import delegate_to_executor, proxy_method_directly, proxy_property_directly @delegate_to_executor( diff --git a/src/aiofiles/threadpool/text.py b/src/aiofiles/threadpool/text.py index 1323009..0e62590 100644 --- a/src/aiofiles/threadpool/text.py +++ b/src/aiofiles/threadpool/text.py @@ -1,9 +1,5 @@ from ..base import AsyncBase, AsyncIndirectBase -from .utils import ( - delegate_to_executor, - proxy_method_directly, - proxy_property_directly, -) +from .utils import delegate_to_executor, proxy_method_directly, proxy_property_directly @delegate_to_executor( diff --git a/tests/test_os.py b/tests/test_os.py index 7aaac04..0419c22 100644 --- a/tests/test_os.py +++ b/tests/test_os.py @@ -1,12 +1,14 @@ """Tests for asyncio's os module.""" -import aiofiles.os import asyncio import os +import platform from os import stat -from os.path import join, dirname, exists, isdir +from os.path import dirname, exists, isdir, join from pathlib import Path + import pytest -import platform + +import aiofiles.os @pytest.mark.asyncio @@ -116,8 +118,7 @@ async def test_replace(): reason="sendfile() syscall doesn't allow file->file", ) @pytest.mark.skipif( - platform.system() == "Darwin", - reason="sendfile() doesn't work on mac", + platform.system() == "Darwin", reason="sendfile() doesn't work on mac" ) @pytest.mark.asyncio async def test_sendfile_file(tmpdir): @@ -395,7 +396,7 @@ async def test_listdir_dir_with_a_file_and_a_dir(): async def test_listdir_non_existing_dir(): """Test the listdir call when the dir doesn't exist.""" some_dir = join(dirname(__file__), "resources", "some_dir") - with pytest.raises(FileNotFoundError) as excinfo: + with pytest.raises(FileNotFoundError): await aiofiles.os.listdir(some_dir) @@ -445,7 +446,7 @@ async def test_scandir_dir_with_only_one_dir(): async def test_scandir_non_existing_dir(): """Test the scandir call when the dir doesn't exist.""" some_dir = join(dirname(__file__), "resources", "some_dir") - with pytest.raises(FileNotFoundError) as excinfo: + with pytest.raises(FileNotFoundError): await aiofiles.os.scandir(some_dir) diff --git a/tox.ini b/tox.ini index 28c93ad..54cd0b7 100644 --- a/tox.ini +++ b/tox.ini @@ -4,12 +4,12 @@ python = 3.8: py38 3.9: py39 3.10: py310 - 3.11: py311 + 3.11: py311, lint 3.12: py312 pypy-3.7: pypy3 [tox] -envlist = py37, py38, py39, py310, py311, py312, pypy3 +envlist = py37, py38, py39, py310, py311, py312, pypy3, lint isolated_build = true skipsdist = true @@ -33,3 +33,6 @@ commands = passenv = CI package = wheel wheel_build_env = .pkg + +[flake8] +max-line-length = 88