mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 09:10:30 +08:00
refactoring
This commit is contained in:
parent
7667834b6d
commit
cd3c28e97f
@ -179,7 +179,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
for name in list(storage["images"]):
|
for name in list(storage["images"]):
|
||||||
del storage["images"][name]["path"]
|
del storage["images"][name]["path"]
|
||||||
del storage["images"][name]["in_storage"]
|
del storage["images"][name]["in_storage"]
|
||||||
del storage["images"][name]["storage"]
|
|
||||||
|
|
||||||
storage["downloading"] = (self.__reader.get_state() if self.__reader else None)
|
storage["downloading"] = (self.__reader.get_state() if self.__reader else None)
|
||||||
|
|
||||||
@ -197,7 +196,6 @@ class Plugin(BaseMsd): # pylint: disable=too-many-instance-attributes
|
|||||||
vd = dataclasses.asdict(self.__state.vd)
|
vd = dataclasses.asdict(self.__state.vd)
|
||||||
if vd["image"]:
|
if vd["image"]:
|
||||||
del vd["image"]["path"]
|
del vd["image"]["path"]
|
||||||
del vd["image"]["storage"]
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"enabled": True,
|
"enabled": True,
|
||||||
|
|||||||
@ -38,24 +38,30 @@ from .. import MsdError
|
|||||||
class _Image:
|
class _Image:
|
||||||
name: str
|
name: str
|
||||||
path: str
|
path: str
|
||||||
storage: Optional["Storage"] = dataclasses.field(compare=False)
|
|
||||||
|
|
||||||
in_storage: bool = dataclasses.field(init=False)
|
in_storage: bool = dataclasses.field(init=False)
|
||||||
|
|
||||||
complete: bool = dataclasses.field(init=False, compare=False)
|
complete: bool = dataclasses.field(init=False, compare=False)
|
||||||
size: int = dataclasses.field(init=False, compare=False)
|
size: int = dataclasses.field(init=False, compare=False)
|
||||||
mod_ts: float = dataclasses.field(init=False, compare=False)
|
mod_ts: float = dataclasses.field(init=False, compare=False)
|
||||||
|
|
||||||
|
|
||||||
class Image(_Image):
|
class Image(_Image):
|
||||||
|
def __init__(self, name: str, path: str, storage: Optional["Storage"]) -> None:
|
||||||
|
super().__init__(name, path)
|
||||||
|
self.__storage = storage
|
||||||
|
self.__complete_path = os.path.join(
|
||||||
|
os.path.dirname(path),
|
||||||
|
".__" + os.path.basename(path) + ".complete",
|
||||||
|
)
|
||||||
|
self.__adopted = (storage._is_adopted(self) if storage else True)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def in_storage(self) -> bool:
|
def in_storage(self) -> bool:
|
||||||
return (self.storage is not None)
|
return bool(self.__storage)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def complete(self) -> bool:
|
def complete(self) -> bool:
|
||||||
if self.storage is not None:
|
if self.__storage:
|
||||||
return os.path.exists(self.__get_complete_path())
|
return os.path.exists(self.__complete_path)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -76,12 +82,12 @@ class Image(_Image):
|
|||||||
return os.path.exists(self.path)
|
return os.path.exists(self.path)
|
||||||
|
|
||||||
async def remount_rw(self, rw: bool, fatal: bool=True) -> None:
|
async def remount_rw(self, rw: bool, fatal: bool=True) -> None:
|
||||||
assert self.storage
|
assert self.__storage
|
||||||
if self.storage._is_mounted(self): # pylint: disable=protected-access
|
if not self.__adopted:
|
||||||
await self.storage.remount_rw(rw, fatal)
|
await self.__storage.remount_rw(rw, fatal)
|
||||||
|
|
||||||
def remove(self, fatal: bool) -> None:
|
def remove(self, fatal: bool) -> None:
|
||||||
assert self.storage is not None
|
assert self.__storage
|
||||||
try:
|
try:
|
||||||
os.remove(self.path)
|
os.remove(self.path)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
@ -92,22 +98,15 @@ class Image(_Image):
|
|||||||
self.set_complete(False)
|
self.set_complete(False)
|
||||||
|
|
||||||
def set_complete(self, flag: bool) -> None:
|
def set_complete(self, flag: bool) -> None:
|
||||||
assert self.storage is not None
|
assert self.__storage
|
||||||
path = self.__get_complete_path()
|
|
||||||
if flag:
|
if flag:
|
||||||
open(path, "w").close() # pylint: disable=consider-using-with
|
open(self.__complete_path, "w").close() # pylint: disable=consider-using-with
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
os.remove(path)
|
os.remove(self.__complete_path)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __get_complete_path(self) -> str:
|
|
||||||
return os.path.join(
|
|
||||||
os.path.dirname(self.path),
|
|
||||||
".__" + os.path.basename(self.path) + ".complete",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
@dataclasses.dataclass(frozen=True)
|
||||||
class StorageSpace:
|
class StorageSpace:
|
||||||
@ -173,11 +172,15 @@ class Storage:
|
|||||||
free=(st.f_bavail * st.f_frsize),
|
free=(st.f_bavail * st.f_frsize),
|
||||||
)
|
)
|
||||||
|
|
||||||
def _is_mounted(self, image: Image) -> bool:
|
def _is_adopted(self, image: Image) -> bool:
|
||||||
|
# True, если образ находится вне хранилища
|
||||||
|
# или в другой точке монтирования под ним
|
||||||
|
if not image.in_storage:
|
||||||
|
return True
|
||||||
path = image.path
|
path = image.path
|
||||||
while not os.path.ismount(path):
|
while not os.path.ismount(path):
|
||||||
path = os.path.dirname(path)
|
path = os.path.dirname(path)
|
||||||
return (path == self.__path)
|
return (self.__path != path)
|
||||||
|
|
||||||
async def remount_rw(self, rw: bool, fatal: bool=True) -> None:
|
async def remount_rw(self, rw: bool, fatal: bool=True) -> None:
|
||||||
if not (await aiohelpers.remount("MSD", self.__remount_cmd, rw)):
|
if not (await aiohelpers.remount("MSD", self.__remount_cmd, rw)):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user