Skip to content

Commit

Permalink
all tests passing, removed funky async logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewfulton9 committed Nov 24, 2020
1 parent e3c893f commit 76166f9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
52 changes: 40 additions & 12 deletions upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import urllib
import re
import asyncio
import inspect

import fsspec
from fsspec.asyn import AsyncFileSystem
Expand Down Expand Up @@ -68,6 +69,9 @@ def __getattribute__(self, item):
if fs is not None:
method = getattr(fs, item, None)
if method:
awaitable = inspect.isawaitable(lambda args, kwargs: method(*args, **kwargs))
print(item)
print('is awaitable:', awaitable)
return lambda *args, **kwargs: self.argument_upath_self_to_filepath(method)(*args, **kwargs)
else:
raise NotImplementedError(f'{fs.protocol} filesystem has not attribute {item}')
Expand Down Expand Up @@ -111,6 +115,24 @@ def __new__(cls, *args, **kwargs):
return self


def run_as_async(self, func, *args, **kwargs):
def wrapper(*args, **kwargs):
if isinstance(self.fs, AsyncFileSystem):
result = None
async def async_runner():
async def async_func():
return await func(*args, **kwargs)
coro = async_func()
done, pending = await asyncio.wait({coro})
if coro is done:
result = coro
asyncio.run(async_runner())
return result
else:
return func(*args, **kwargs)
return wrapper


class UniversalPath(UPath, PureUniversalPath):

__slots__ = ('_url', '_kwargs', '_closed', 'fs')
Expand Down Expand Up @@ -146,7 +168,7 @@ def __getattribute__(self, item):
if item in getattr(UniversalPath, 'not_implemented'):
raise NotImplementedError(f'UniversalPath has no attribute {item}')
else:
return super().__getattribute__(item)
return super().__getattribute__(item)

def _format_parsed_parts(self, drv, root, parts):
join_parts = parts[1:] if parts[0] == '/' else parts
Expand Down Expand Up @@ -234,26 +256,32 @@ def touch(self, trunicate=True, **kwargs):
self._accessor.touch(self, trunicate=trunicate, **kwargs)

def unlink(self, missing_ok=False):
async def async_unlink():
async def rm():
try:
await self._accessor.rm_file(self)
except:
await self._accessor.rm(self, recursive=False)
# async def async_unlink():
# async def rm():
# try:
# await self._accessor.rm_file(self)
# except:
# await self._accessor.rm(self, recursive=False)


coro = rm()
done, pending = await asyncio.wait({coro})
if coro is done:
return
# coro = rm()
# done, pending = await asyncio.wait({coro})
# if coro is done:
# return

# print('exists:', self.exists())

if not self.exists():
if not missing_ok:
raise FileNotFoundError
else:
return
asyncio.run(async_unlink())
try:
self._accessor.rm(self, recursive=False)
except:
self._accessor.rm_file(self)

# asyncio.run(async_unlink())

def rmdir(self, recursive=True):
'''Add warning if directory not empty
Expand Down
3 changes: 0 additions & 3 deletions upath/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ def test_glob(self, pathlib_base):
mock_glob = list(self.path.glob('**.txt'))
path_glob = list(pathlib_base.glob('**/*.txt'))

print(mock_glob)
print(path_glob)

assert len(mock_glob) == len(path_glob)
assert all(map(lambda m: m.path in [str(p) for p in path_glob], mock_glob))

Expand Down

0 comments on commit 76166f9

Please sign in to comment.