partially asynchronous msd

This commit is contained in:
Maxim Devaev
2023-03-16 01:32:05 +02:00
parent baf454bef9
commit 4d6da37f40
3 changed files with 35 additions and 28 deletions

View File

@@ -26,6 +26,9 @@ import dataclasses
from typing import Generator
from typing import Optional
import aiofiles
import aiofiles.os
from ....logging import get_logger
from .... import aiohelpers
@@ -85,32 +88,33 @@ class Image(_Image):
except Exception:
return 0.0
def exists(self) -> bool:
return os.path.exists(self.path)
async def exists(self) -> bool:
return (await aiofiles.os.path.exists(self.path))
async def remount_rw(self, rw: bool, fatal: bool=True) -> None:
assert self.__storage
if not self.__adopted:
await self.__storage.remount_rw(rw, fatal)
def remove(self, fatal: bool) -> None:
async def remove(self, fatal: bool) -> None:
assert self.__storage
try:
os.remove(self.path)
await aiofiles.os.remove(self.path)
except FileNotFoundError:
pass
except Exception:
if fatal:
raise
self.set_complete(False)
await self.set_complete(False)
def set_complete(self, flag: bool) -> None:
async def set_complete(self, flag: bool) -> None:
assert self.__storage
if flag:
open(self.__complete_path, "w").close() # pylint: disable=consider-using-with
async with aiofiles.open(self.__complete_path, "w"):
pass
else:
try:
os.remove(self.__complete_path)
await aiofiles.os.remove(self.__complete_path)
except FileNotFoundError:
pass