Skip to content

Commit

Permalink
remove aiopath
Browse files Browse the repository at this point in the history
  • Loading branch information
skshetry committed Sep 26, 2022
1 parent 345178f commit 6481513
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 97 deletions.
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ memfs =
asynclocalfs =
aiofiles==22.1.0
aiofile==3.8.1
aiopath==0.6.10; python_version >= '3.10'
aiopath==0.5.12; python_version < '3.10'
all =
%(memfs)s
%(asynclocalfs)s
Expand Down
105 changes: 10 additions & 95 deletions src/morefs/asyn_local.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import asyncio
import datetime
import errno
import functools
import os
import shutil
import stat

import aiofile
import aiofiles.os
from aiofiles.os import wrap # type: ignore[attr-defined]
from aiopath.scandir import EntryWrapper, scandir_async
from fsspec import AbstractFileSystem
from fsspec.asyn import AbstractBufferedFile, AsyncFileSystem
from fsspec.implementations.local import LocalFileSystem
Expand Down Expand Up @@ -37,119 +36,35 @@ async def copy_asyncfileobj(fsrc, fdst, length=shutil.COPY_BUFSIZE):
await fdst_write(buf)


def sync_info(path, **kwargs):
if isinstance(path, os.DirEntry):
out = path.stat(follow_symlinks=False)
link = path.is_symlink()
if path.is_dir(follow_symlinks=False):
t = "directory"
elif path.is_file(follow_symlinks=False):
t = "file"
else:
t = "other"
path = path.path
else:
out = os.stat(path, follow_symlinks=False)
link = stat.S_ISLNK(out.st_mode)
if link:
out = os.stat(path, follow_symlinks=True)
if stat.S_ISDIR(out.st_mode):
t = "directory"
elif stat.S_ISREG(out.st_mode):
t = "file"
else:
t = "other"
result = {
"name": path,
"size": out.st_size,
"type": t,
"created": out.st_ctime,
"islink": link,
}
for field in ["mode", "uid", "gid", "mtime"]:
result[field] = getattr(out, "st_" + field)
if result["islink"]:
result["destination"] = os.readlink(path)
try:
out2 = os.stat(path, follow_symlinks=True)
result["size"] = out2.st_size
except IOError:
result["size"] = 0
return result


# pylint: disable=arguments-renamed


def wrapped(func):
@functools.wraps(func)
def inner(self, *args, **kwargs):
return func(self, *args, **kwargs)

return inner


class AsyncLocalFileSystem(AsyncFileSystem): # pylint: disable=abstract-method
info = staticmethod(sync_info)
find = wrapped(AbstractFileSystem.find)
walk = wrapped(AbstractFileSystem.walk)
exists = wrapped(AbstractFileSystem.exists)
isdir = wrapped(AbstractFileSystem.isdir)
isfile = wrapped(AbstractFileSystem.isfile)
lexists = staticmethod(os.path.lexists)
lexists = staticmethod(LocalFileSystem.lexists)

ls = wrapped(LocalFileSystem.ls)
info = wrapped(LocalFileSystem.info)
_info = wrap(LocalFileSystem.info)

async def _ls(self, path, detail=True, **kwargs):
if detail:
return [await self._info(f) async for f in scandir_async(path)]
entries = await aiofiles.os.scandir(path)
return [await self._info(f) for f in entries]
return [os.path.join(path, f) for f in await aiofiles.os.listdir(path)]

def ls(self, path, detail=True, **kwargs):
if detail:
with os.scandir(path) as it:
return [sync_info(f) for f in it]
return [os.path.join(path, f) for f in os.listdir(path)]

async def _info(self, path, **kwargs):
if isinstance(path, os.DirEntry):
path = EntryWrapper(path)
if isinstance(path, EntryWrapper):
out = await path.stat(follow_symlinks=False)
link = await path.is_symlink()
if await path.is_dir(follow_symlinks=False):
t = "directory"
elif await path.is_file(follow_symlinks=False):
t = "file"
else:
t = "other"
path = path.path
else:
out = await aiofiles.os.stat(path, follow_symlinks=False)
link = stat.S_ISLNK(out.st_mode)
if link:
out = await aiofiles.os.stat(path, follow_symlinks=True)
if stat.S_ISDIR(out.st_mode):
t = "directory"
elif stat.S_ISREG(out.st_mode):
t = "file"
else:
t = "other"
result = {
"name": path,
"size": out.st_size,
"type": t,
"created": out.st_ctime,
"islink": link,
}
for field in ["mode", "uid", "gid", "mtime"]:
result[field] = getattr(out, "st_" + field)
if result["islink"]:
result["destination"] = await aiofiles.os.readlink(path)
try:
out2 = await aiofiles.os.stat(path, follow_symlinks=True)
result["size"] = out2.st_size
except IOError:
result["size"] = 0
return result

async def _rm_file(self, path, **kwargs):
await aiofiles.os.remove(path)

Expand Down Expand Up @@ -217,7 +132,7 @@ async def _created(self, path):
return datetime.datetime.utcfromtimestamp(info["created"])

async def _modified(self, path):
info = await self.info(path=path)
info = await self._info(path=path)
return datetime.datetime.utcfromtimestamp(info["mtime"])

async def _rm(
Expand Down

0 comments on commit 6481513

Please sign in to comment.