Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanley Kudrow committed Nov 12, 2024
1 parent 3071526 commit 0faf5b5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
12 changes: 12 additions & 0 deletions src/aiofiles/base.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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
45 changes: 26 additions & 19 deletions src/aiofiles/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
25 changes: 9 additions & 16 deletions src/aiofiles/ospath.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0faf5b5

Please sign in to comment.