mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 17:20:30 +08:00
unified aioregion logic
This commit is contained in:
parent
73ec9d853e
commit
0a7eafa256
@ -9,14 +9,15 @@ class RegionIsBusyError(Exception):
|
||||
|
||||
|
||||
class AioExclusiveRegion:
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, exc_type: Type[RegionIsBusyError]) -> None:
|
||||
self.__exc_type = exc_type
|
||||
self.__busy = False
|
||||
|
||||
def enter(self) -> None:
|
||||
if not self.__busy:
|
||||
self.__busy = True
|
||||
return
|
||||
raise RegionIsBusyError()
|
||||
raise self.__exc_type()
|
||||
|
||||
def exit(self) -> None:
|
||||
self.__busy = False
|
||||
|
||||
@ -9,6 +9,11 @@ from . import gpio
|
||||
|
||||
|
||||
# =====
|
||||
class AtxIsBusy(aioregion.RegionIsBusyError):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Performing another operation, please try again later")
|
||||
|
||||
|
||||
class Atx:
|
||||
def __init__(
|
||||
self,
|
||||
@ -29,7 +34,7 @@ class Atx:
|
||||
self.__click_delay = click_delay
|
||||
self.__long_click_delay = long_click_delay
|
||||
|
||||
self.__region = aioregion.AioExclusiveRegion()
|
||||
self.__region = aioregion.AioExclusiveRegion(AtxIsBusy)
|
||||
|
||||
def get_state(self) -> Dict:
|
||||
return {
|
||||
|
||||
@ -49,7 +49,7 @@ class IsNotConnectedToKvmError(MassStorageOperationError):
|
||||
super().__init__("Mass-storage is not connected to KVM")
|
||||
|
||||
|
||||
class IsBusyError(MassStorageOperationError):
|
||||
class IsBusyError(MassStorageOperationError, aioregion.RegionIsBusyError):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Mass-storage is busy (write in progress)")
|
||||
|
||||
@ -155,8 +155,6 @@ def _explore_device(device_path: str) -> Optional[_MassStorageDeviceInfo]:
|
||||
|
||||
def _msd_operated(method: Callable) -> Callable:
|
||||
async def wrap(self: "MassStorageDevice", *args: Any, **kwargs: Any) -> Any:
|
||||
if self._device_file: # pylint: disable=protected-access
|
||||
raise IsBusyError()
|
||||
if not self._device_path: # pylint: disable=protected-access
|
||||
IsNotOperationalError()
|
||||
return (await method(self, *args, **kwargs))
|
||||
@ -179,7 +177,7 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
||||
self.__loop = loop
|
||||
|
||||
self.__device_info: Optional[_MassStorageDeviceInfo] = None
|
||||
self.__region = aioregion.AioExclusiveRegion()
|
||||
self.__region = aioregion.AioExclusiveRegion(IsBusyError)
|
||||
self._device_file: Optional[aiofiles.base.AiofilesContextManager] = None
|
||||
self.__written = 0
|
||||
|
||||
@ -238,11 +236,11 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
||||
|
||||
@_msd_operated
|
||||
async def __aenter__(self) -> "MassStorageDevice":
|
||||
self.__region.enter()
|
||||
if not self.__device_info:
|
||||
raise IsNotConnectedToKvmError()
|
||||
self._device_file = await aiofiles.open(self.__device_info.path, mode="w+b", buffering=0)
|
||||
self.__written = 0
|
||||
self.__region.enter()
|
||||
return self
|
||||
|
||||
async def write_image_info(self, name: str, complete: bool) -> None:
|
||||
|
||||
@ -58,23 +58,15 @@ class BadRequest(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class PerformingAnotherOperation(Exception):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Performing another operation, please try again later")
|
||||
|
||||
|
||||
def _wrap_exceptions_for_web(msg: str) -> Callable:
|
||||
def make_wrapper(method: Callable) -> Callable:
|
||||
async def wrap(self: "Server", request: aiohttp.web.Request) -> aiohttp.web.Response:
|
||||
try:
|
||||
try:
|
||||
return (await method(self, request))
|
||||
except RegionIsBusyError:
|
||||
raise PerformingAnotherOperation()
|
||||
return (await method(self, request))
|
||||
except RegionIsBusyError as err:
|
||||
return _json_exception(msg, err, 409)
|
||||
except (BadRequest, MassStorageOperationError) as err:
|
||||
return _json_exception(msg, err, 400)
|
||||
except PerformingAnotherOperation as err:
|
||||
return _json_exception(msg, err, 409)
|
||||
return wrap
|
||||
return make_wrapper
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user