|
4 | 4 | import urllib |
5 | 5 | import re |
6 | 6 | import asyncio |
| 7 | +import inspect |
7 | 8 |
|
8 | 9 | import fsspec |
9 | 10 | from fsspec.asyn import AsyncFileSystem |
@@ -68,6 +69,9 @@ def __getattribute__(self, item): |
68 | 69 | if fs is not None: |
69 | 70 | method = getattr(fs, item, None) |
70 | 71 | if method: |
| 72 | + awaitable = inspect.isawaitable(lambda args, kwargs: method(*args, **kwargs)) |
| 73 | + print(item) |
| 74 | + print('is awaitable:', awaitable) |
71 | 75 | return lambda *args, **kwargs: self.argument_upath_self_to_filepath(method)(*args, **kwargs) |
72 | 76 | else: |
73 | 77 | raise NotImplementedError(f'{fs.protocol} filesystem has not attribute {item}') |
@@ -111,6 +115,24 @@ def __new__(cls, *args, **kwargs): |
111 | 115 | return self |
112 | 116 |
|
113 | 117 |
|
| 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 | + |
114 | 136 | class UniversalPath(UPath, PureUniversalPath): |
115 | 137 |
|
116 | 138 | __slots__ = ('_url', '_kwargs', '_closed', 'fs') |
@@ -146,7 +168,7 @@ def __getattribute__(self, item): |
146 | 168 | if item in getattr(UniversalPath, 'not_implemented'): |
147 | 169 | raise NotImplementedError(f'UniversalPath has no attribute {item}') |
148 | 170 | else: |
149 | | - return super().__getattribute__(item) |
| 171 | + return super().__getattribute__(item) |
150 | 172 |
|
151 | 173 | def _format_parsed_parts(self, drv, root, parts): |
152 | 174 | join_parts = parts[1:] if parts[0] == '/' else parts |
@@ -234,26 +256,32 @@ def touch(self, trunicate=True, **kwargs): |
234 | 256 | self._accessor.touch(self, trunicate=trunicate, **kwargs) |
235 | 257 |
|
236 | 258 | 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) |
243 | 265 |
|
244 | 266 |
|
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 |
249 | 271 |
|
250 | 272 | # print('exists:', self.exists()) |
| 273 | + |
251 | 274 | if not self.exists(): |
252 | 275 | if not missing_ok: |
253 | 276 | raise FileNotFoundError |
254 | 277 | else: |
255 | 278 | 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()) |
257 | 285 |
|
258 | 286 | def rmdir(self, recursive=True): |
259 | 287 | '''Add warning if directory not empty |
|
0 commit comments