Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more os calls like os.stat #4

Open
clayg opened this issue May 12, 2016 · 12 comments
Open

Add more os calls like os.stat #4

clayg opened this issue May 12, 2016 · 12 comments

Comments

@clayg
Copy link

clayg commented May 12, 2016

I'm just poking at this - came to it through a reference on MagicStack/uvloop#1 (comment)

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
@Tinche
Copy link
Owner

Tinche commented May 13, 2016

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

@cjerdonek
Copy link
Contributor

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

@Tinche
Copy link
Owner

Tinche commented Jul 2, 2017

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

@perklet
Copy link

perklet commented May 19, 2018

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())

@supriyo-biswas
Copy link

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

@Qwerty-Space
Copy link

Qwerty-Space commented Feb 1, 2020

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

@conraddd
Copy link

@Qwerty-Space Use from aiofiles import os instead

@debuglevel
Copy link

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

@nono-london
Copy link

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

@jgarvin
Copy link

jgarvin commented Aug 8, 2023

os.lstat is also missing

@machsix
Copy link

machsix commented Dec 21, 2023

os.fstat is missing

@OndrejIT
Copy link

OndrejIT commented Mar 2, 2024

Bump..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests