mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 00:51:53 +08:00
refactoring
This commit is contained in:
@@ -71,6 +71,10 @@ class _DriveImage:
|
|||||||
size: int = dataclasses.field(default=0)
|
size: int = dataclasses.field(default=0)
|
||||||
mod_ts: float = dataclasses.field(default=0)
|
mod_ts: float = dataclasses.field(default=0)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def exists(self) -> bool: # Not exposed as a field
|
||||||
|
return os.path.exists(self.path)
|
||||||
|
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
try:
|
try:
|
||||||
st = os.stat(self.path)
|
st = os.stat(self.path)
|
||||||
@@ -275,14 +279,11 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
assert self.__state.storage
|
assert self.__state.storage
|
||||||
assert self.__state.vd
|
assert self.__state.vd
|
||||||
|
|
||||||
if self.__state.vd.connected or self.__drive.get_image_path():
|
self.__check_disconnected()
|
||||||
raise MsdConnectedError()
|
|
||||||
|
|
||||||
if name is not None:
|
if name is not None:
|
||||||
if name:
|
if name:
|
||||||
image = self.__state.storage.images.get(name)
|
image = self.__get_image(name)
|
||||||
if image is None or not os.path.exists(image.path):
|
|
||||||
raise MsdUnknownImageError()
|
|
||||||
assert image.in_storage
|
assert image.in_storage
|
||||||
self.__state.vd.image = image
|
self.__state.vd.image = image
|
||||||
else:
|
else:
|
||||||
@@ -303,14 +304,14 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
async with self.__state.busy():
|
async with self.__state.busy():
|
||||||
assert self.__state.vd
|
assert self.__state.vd
|
||||||
if connected:
|
if connected:
|
||||||
if self.__state.vd.connected or self.__drive.get_image_path():
|
self.__check_disconnected()
|
||||||
raise MsdConnectedError()
|
|
||||||
if self.__state.vd.image is None:
|
if self.__state.vd.image is None:
|
||||||
raise MsdImageNotSelected()
|
raise MsdImageNotSelected()
|
||||||
|
|
||||||
assert self.__state.vd.image.in_storage
|
assert self.__state.vd.image.in_storage
|
||||||
|
|
||||||
if not os.path.exists(self.__state.vd.image.path):
|
if not self.__state.vd.image.exists:
|
||||||
raise MsdUnknownImageError()
|
raise MsdUnknownImageError()
|
||||||
|
|
||||||
self.__drive.set_rw_flag(self.__state.vd.rw)
|
self.__drive.set_rw_flag(self.__state.vd.rw)
|
||||||
@@ -320,8 +321,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
self.__drive.set_image_path(self.__state.vd.image.path)
|
self.__drive.set_image_path(self.__state.vd.image.path)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if not (self.__state.vd.connected or self.__drive.get_image_path()):
|
self.__check_connected()
|
||||||
raise MsdDisconnectedError()
|
|
||||||
self.__drive.set_image_path("")
|
self.__drive.set_image_path("")
|
||||||
await self.__remount_rw(False, fatal=False)
|
await self.__remount_rw(False, fatal=False)
|
||||||
|
|
||||||
@@ -337,16 +337,13 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
assert self.__state.storage
|
assert self.__state.storage
|
||||||
assert self.__state.vd
|
assert self.__state.vd
|
||||||
|
|
||||||
if self.__state.vd.connected or self.__drive.get_image_path():
|
self.__check_disconnected()
|
||||||
raise MsdConnectedError()
|
|
||||||
|
|
||||||
path = self.__storage.get_image_path(name)
|
image = self.__get_image(name)
|
||||||
if name not in self.__state.storage.images or not os.path.exists(path):
|
|
||||||
raise MsdUnknownImageError()
|
|
||||||
|
|
||||||
self.__reader = await MsdFileReader(
|
self.__reader = await MsdFileReader(
|
||||||
notifier=self.__notifier,
|
notifier=self.__notifier,
|
||||||
path=path,
|
path=image.path,
|
||||||
chunk_size=self.__read_chunk_size,
|
chunk_size=self.__read_chunk_size,
|
||||||
).open()
|
).open()
|
||||||
|
|
||||||
@@ -368,8 +365,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
assert self.__state.storage
|
assert self.__state.storage
|
||||||
assert self.__state.vd
|
assert self.__state.vd
|
||||||
|
|
||||||
if self.__state.vd.connected or self.__drive.get_image_path():
|
self.__check_disconnected()
|
||||||
raise MsdConnectedError()
|
|
||||||
|
|
||||||
path = self.__storage.get_image_path(name)
|
path = self.__storage.get_image_path(name)
|
||||||
if name in self.__state.storage.images or os.path.exists(path):
|
if name in self.__state.storage.images or os.path.exists(path):
|
||||||
@@ -412,12 +408,9 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
assert self.__state.storage
|
assert self.__state.storage
|
||||||
assert self.__state.vd
|
assert self.__state.vd
|
||||||
|
|
||||||
if self.__state.vd.connected or self.__drive.get_image_path():
|
self.__check_disconnected()
|
||||||
raise MsdConnectedError()
|
|
||||||
|
|
||||||
image = self.__state.storage.images.get(name)
|
image = self.__get_image(name)
|
||||||
if image is None or not os.path.exists(image.path):
|
|
||||||
raise MsdUnknownImageError()
|
|
||||||
assert image.in_storage
|
assert image.in_storage
|
||||||
|
|
||||||
if self.__state.vd.image == image:
|
if self.__state.vd.image == image:
|
||||||
@@ -431,6 +424,23 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
|
|
||||||
# =====
|
# =====
|
||||||
|
|
||||||
|
def __check_connected(self) -> None:
|
||||||
|
assert self.__state.vd
|
||||||
|
if not (self.__state.vd.connected or self.__drive.get_image_path()):
|
||||||
|
raise MsdDisconnectedError()
|
||||||
|
|
||||||
|
def __check_disconnected(self) -> None:
|
||||||
|
assert self.__state.vd
|
||||||
|
if self.__state.vd.connected or self.__drive.get_image_path():
|
||||||
|
raise MsdConnectedError()
|
||||||
|
|
||||||
|
def __get_image(self, name: str) -> _DriveImage:
|
||||||
|
assert self.__state.storage
|
||||||
|
image = self.__state.storage.images.get(name)
|
||||||
|
if image is None or not image.exists:
|
||||||
|
raise MsdUnknownImageError()
|
||||||
|
return image
|
||||||
|
|
||||||
async def __close_reader(self) -> None:
|
async def __close_reader(self) -> None:
|
||||||
if self.__reader:
|
if self.__reader:
|
||||||
await self.__reader.close()
|
await self.__reader.close()
|
||||||
@@ -513,7 +523,7 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
self.__state.vd.image
|
self.__state.vd.image
|
||||||
and (not self.__state.vd.image.in_storage or not os.path.exists(self.__state.vd.image.path))
|
and (not self.__state.vd.image.in_storage or not self.__state.vd.image.exists)
|
||||||
):
|
):
|
||||||
# Если только что отключили ручной образ вне хранилища или ранее выбранный образ был удален
|
# Если только что отключили ручной образ вне хранилища или ранее выбранный образ был удален
|
||||||
self.__state.vd.image = None
|
self.__state.vd.image = None
|
||||||
|
|||||||
Reference in New Issue
Block a user