diff --git a/src/aiofiles/base.py b/src/aiofiles/base.py index 35b26d2..26951f0 100644 --- a/src/aiofiles/base.py +++ b/src/aiofiles/base.py @@ -1,6 +1,7 @@ from asyncio import get_running_loop from collections.abc import Awaitable from contextlib import AbstractAsyncContextManager +from functools import partial, wraps class AsyncBase: @@ -65,3 +66,14 @@ async def __aexit__(self, exc_type, exc_val, exc_tb): None, self._obj._file.__exit__, exc_type, exc_val, exc_tb ) self._obj = None + + +def wrap(func): + @wraps(func) + async def run(*args, loop=None, executor=None, **kwargs): + if loop is None: + loop = get_running_loop() + pfunc = partial(func, *args, **kwargs) + return await loop.run_in_executor(executor, pfunc) + + return run diff --git a/src/aiofiles/os.py b/src/aiofiles/os.py index 92243fa..ceeff14 100644 --- a/src/aiofiles/os.py +++ b/src/aiofiles/os.py @@ -2,8 +2,9 @@ import os -from . import ospath as path -from .ospath import wrap +from aiofiles.base import wrap +from aiofiles import ospath as path + __all__ = [ "path", @@ -25,34 +26,40 @@ "wrap", "getcwd", ] -if hasattr(os, "link"): - __all__ += ["link"] -if hasattr(os, "sendfile"): - __all__ += ["sendfile"] -if hasattr(os, "statvfs"): - __all__ += ["statvfs"] -stat = wrap(os.stat) +access = wrap(os.access) + +getcwd = wrap(os.getcwd) + +listdir = wrap(os.listdir) + +makedirs = wrap(os.makedirs) +mkdir = wrap(os.mkdir) + +readlink = wrap(os.readlink) +remove = wrap(os.remove) +removedirs = wrap(os.removedirs) rename = wrap(os.rename) renames = wrap(os.renames) replace = wrap(os.replace) -remove = wrap(os.remove) -unlink = wrap(os.unlink) -mkdir = wrap(os.mkdir) -makedirs = wrap(os.makedirs) rmdir = wrap(os.rmdir) -removedirs = wrap(os.removedirs) -symlink = wrap(os.symlink) -readlink = wrap(os.readlink) -listdir = wrap(os.listdir) + scandir = wrap(os.scandir) -access = wrap(os.access) -getcwd = wrap(os.getcwd) +stat = wrap(os.stat) +symlink = wrap(os.symlink) + +unlink = wrap(os.unlink) + +walk = wrap(os.walk) + if hasattr(os, "link"): + __all__ += ["link"] link = wrap(os.link) if hasattr(os, "sendfile"): + __all__ += ["sendfile"] sendfile = wrap(os.sendfile) if hasattr(os, "statvfs"): + __all__ += ["statvfs"] statvfs = wrap(os.statvfs) diff --git a/src/aiofiles/ospath.py b/src/aiofiles/ospath.py index 387d68d..35ff218 100644 --- a/src/aiofiles/ospath.py +++ b/src/aiofiles/ospath.py @@ -1,30 +1,23 @@ """Async executor versions of file functions from the os.path module.""" -import asyncio -from functools import partial, wraps from os import path +from aiofiles.base import wrap -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 +abspath = wrap(path.abspath) exists = wrap(path.exists) -isfile = wrap(path.isfile) -isdir = wrap(path.isdir) -islink = wrap(path.islink) -ismount = wrap(path.ismount) + getsize = wrap(path.getsize) getmtime = wrap(path.getmtime) getatime = wrap(path.getatime) getctime = wrap(path.getctime) + +isfile = wrap(path.isfile) +isdir = wrap(path.isdir) +islink = wrap(path.islink) +ismount = wrap(path.ismount) + samefile = wrap(path.samefile) sameopenfile = wrap(path.sameopenfile) -abspath = wrap(path.abspath)