aiofiles icon indicating copy to clipboard operation
aiofiles copied to clipboard

Add more os calls like os.stat

Open clayg opened this issue 8 years ago • 12 comments

I'm just poking at this - came to it through a reference on https://github.com/MagicStack/uvloop/issues/1#issuecomment-157537973

Would you be interested in adding support to the project for more calls from the os module like os.stat?

e.g.

  • os.close
  • os.fstat
  • os.read
  • os.write
  • os.unlink
  • os.listdir
  • os.path.exists
  • os.rmdir

clayg avatar May 12 '16 18:05 clayg

If someone were to submit quality pull requests (with tests, of course) I wouldn't be opposed to merging them in. :)

Tinche avatar May 13 '16 08:05 Tinche

@Tinche Do you have a preference for what aiofiles module os.path functions should go in?

cjerdonek avatar Jun 24 '17 16:06 cjerdonek

@cjerdonek I guess aiofiles.os.path, so we mirror the sync versions closely.

Tinche avatar Jul 02 '17 16:07 Tinche

havn't fully tested yet, but should this snippet work?

import asyncio
import inspect
from functools import wraps, partial


def wrap(func):
    @wraps(func)
    async def run(*args, loop=None, executor=None, **kwargs):
        if loop is None:
            loop = asyncio.get_event_loop()
        pfunc = partial(func, *args, **kwargs)
        return await loop.run_in_executor(executor, pfunc)

    return run


class Wrapper:
    pass


def aiowrap(obj):
    if callable(obj):
        return wrap(obj)
    elif inspect.ismodule(obj) or inspect.isclass(obj):
        wrapped_obj = Wrapper()
        if getattr(obj, '__all__'):
            attrnames = obj.__all__
        else:
            attrnames = dir(obj)
        for attrname in attrnames:
            if attrname.startswith('__'):
                continue
            original_obj = getattr(obj, attrname)
            setattr(wrapped_obj, attrname, aiowrap(original_obj))
        return wrapped_obj
    else:
        return obj

Usage

import os
from aiowrap import aiowrap

aios = aiowrap(os)

async def main():
    print(await aios.path.exists('/'))

if __name__ == '__main__':
    import asyncio as aio
    loop = aio.get_event_loop()
    loop.run_until_complete(main())

perkfly avatar May 19 '18 05:05 perkfly

Support for os.mkdir() would also be nice.

supriyo-biswas avatar Sep 30 '18 13:09 supriyo-biswas

How do you even use aiofiles.os, when I try I get AttributeError: module 'aiofiles' has no attribute 'os'

Qwerty-Space avatar Feb 01 '20 22:02 Qwerty-Space

@Qwerty-Space Use from aiofiles import os instead

conraddd avatar Apr 24 '20 15:04 conraddd

makedirs would also be nice, as it is quite a useful shortcut :-)

debuglevel avatar Aug 01 '21 22:08 debuglevel

would be nice to add support for os.listdir Tx for the library

nono-london avatar Jan 11 '22 18:01 nono-london

os.lstat is also missing

jgarvin avatar Aug 08 '23 17:08 jgarvin

os.fstat is missing

machsix avatar Dec 21 '23 20:12 machsix