Skip to content

Commit 76166f9

Browse files
committed
all tests passing, removed funky async logic
1 parent e3c893f commit 76166f9

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

upath/core.py

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import urllib
55
import re
66
import asyncio
7+
import inspect
78

89
import fsspec
910
from fsspec.asyn import AsyncFileSystem
@@ -68,6 +69,9 @@ def __getattribute__(self, item):
6869
if fs is not None:
6970
method = getattr(fs, item, None)
7071
if method:
72+
awaitable = inspect.isawaitable(lambda args, kwargs: method(*args, **kwargs))
73+
print(item)
74+
print('is awaitable:', awaitable)
7175
return lambda *args, **kwargs: self.argument_upath_self_to_filepath(method)(*args, **kwargs)
7276
else:
7377
raise NotImplementedError(f'{fs.protocol} filesystem has not attribute {item}')
@@ -111,6 +115,24 @@ def __new__(cls, *args, **kwargs):
111115
return self
112116

113117

118+
def run_as_async(self, func, *args, **kwargs):
119+
def wrapper(*args, **kwargs):
120+
if isinstance(self.fs, AsyncFileSystem):
121+
result = None
122+
async def async_runner():
123+
async def async_func():
124+
return await func(*args, **kwargs)
125+
coro = async_func()
126+
done, pending = await asyncio.wait({coro})
127+
if coro is done:
128+
result = coro
129+
asyncio.run(async_runner())
130+
return result
131+
else:
132+
return func(*args, **kwargs)
133+
return wrapper
134+
135+
114136
class UniversalPath(UPath, PureUniversalPath):
115137

116138
__slots__ = ('_url', '_kwargs', '_closed', 'fs')
@@ -146,7 +168,7 @@ def __getattribute__(self, item):
146168
if item in getattr(UniversalPath, 'not_implemented'):
147169
raise NotImplementedError(f'UniversalPath has no attribute {item}')
148170
else:
149-
return super().__getattribute__(item)
171+
return super().__getattribute__(item)
150172

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

236258
def unlink(self, missing_ok=False):
237-
async def async_unlink():
238-
async def rm():
239-
try:
240-
await self._accessor.rm_file(self)
241-
except:
242-
await self._accessor.rm(self, recursive=False)
259+
# async def async_unlink():
260+
# async def rm():
261+
# try:
262+
# await self._accessor.rm_file(self)
263+
# except:
264+
# await self._accessor.rm(self, recursive=False)
243265

244266

245-
coro = rm()
246-
done, pending = await asyncio.wait({coro})
247-
if coro is done:
248-
return
267+
# coro = rm()
268+
# done, pending = await asyncio.wait({coro})
269+
# if coro is done:
270+
# return
249271

250272
# print('exists:', self.exists())
273+
251274
if not self.exists():
252275
if not missing_ok:
253276
raise FileNotFoundError
254277
else:
255278
return
256-
asyncio.run(async_unlink())
279+
try:
280+
self._accessor.rm(self, recursive=False)
281+
except:
282+
self._accessor.rm_file(self)
283+
284+
# asyncio.run(async_unlink())
257285

258286
def rmdir(self, recursive=True):
259287
'''Add warning if directory not empty

upath/tests/test_core.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ def test_glob(self, pathlib_base):
5151
mock_glob = list(self.path.glob('**.txt'))
5252
path_glob = list(pathlib_base.glob('**/*.txt'))
5353

54-
print(mock_glob)
55-
print(path_glob)
56-
5754
assert len(mock_glob) == len(path_glob)
5855
assert all(map(lambda m: m.path in [str(p) for p in path_glob], mock_glob))
5956

0 commit comments

Comments
 (0)