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
53e64fe151
commit
b5344a5f3a
@ -43,16 +43,16 @@ from .logging import get_logger
|
|||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
_MethodT = TypeVar("_MethodT", bound=Callable[..., Any])
|
_FunctionT = TypeVar("_FunctionT", bound=Callable[..., Any])
|
||||||
_RetvalT = TypeVar("_RetvalT")
|
_RetvalT = TypeVar("_RetvalT")
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
def atomic(method: _MethodT) -> _MethodT:
|
def atomic(func: _FunctionT) -> _FunctionT:
|
||||||
@functools.wraps(method)
|
@functools.wraps(func)
|
||||||
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
async def wrapper(*args: Any, **kwargs: Any) -> Any:
|
||||||
return (await asyncio.shield(method(*args, **kwargs)))
|
return (await asyncio.shield(func(*args, **kwargs)))
|
||||||
return typing.cast(_MethodT, wrapper)
|
return typing.cast(_FunctionT, wrapper)
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
@ -109,8 +109,8 @@ async def stop_all_deadly_tasks() -> None:
|
|||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
async def run_async(method: Callable[..., _RetvalT], *args: Any) -> _RetvalT:
|
async def run_async(func: Callable[..., _RetvalT], *args: Any) -> _RetvalT:
|
||||||
return (await asyncio.get_running_loop().run_in_executor(None, method, *args))
|
return (await asyncio.get_running_loop().run_in_executor(None, func, *args))
|
||||||
|
|
||||||
|
|
||||||
def run_sync(coro: Coroutine[Any, Any, _RetvalT]) -> _RetvalT:
|
def run_sync(coro: Coroutine[Any, Any, _RetvalT]) -> _RetvalT:
|
||||||
@ -254,7 +254,7 @@ class AioExclusiveRegion:
|
|||||||
async def run_region_task(
|
async def run_region_task(
|
||||||
msg: str,
|
msg: str,
|
||||||
region: AioExclusiveRegion,
|
region: AioExclusiveRegion,
|
||||||
method: Callable[..., Coroutine[Any, Any, None]],
|
func: Callable[..., Coroutine[Any, Any, None]],
|
||||||
*args: Any,
|
*args: Any,
|
||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -265,7 +265,7 @@ async def run_region_task(
|
|||||||
try:
|
try:
|
||||||
async with region:
|
async with region:
|
||||||
entered.set_result(None)
|
entered.set_result(None)
|
||||||
await method(*args, **kwargs)
|
await func(*args, **kwargs)
|
||||||
except region.get_exc_type():
|
except region.get_exc_type():
|
||||||
raise
|
raise
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
@ -85,12 +85,12 @@ def main(argv: Optional[List[str]]=None) -> None:
|
|||||||
logger = get_logger(0)
|
logger = get_logger(0)
|
||||||
logger.info("Cleaning up ...")
|
logger.info("Cleaning up ...")
|
||||||
|
|
||||||
for method in [
|
for func in [
|
||||||
_kill_streamer,
|
_kill_streamer,
|
||||||
_remove_sockets,
|
_remove_sockets,
|
||||||
]:
|
]:
|
||||||
try:
|
try:
|
||||||
method(config)
|
func(config)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@ -161,7 +161,7 @@ class IpmiServer(BaseIpmiServer): # pylint: disable=too-many-instance-attribute
|
|||||||
code = 0xCC # Invalid request
|
code = 0xCC # Invalid request
|
||||||
session.send_ipmi_response(code=code)
|
session.send_ipmi_response(code=code)
|
||||||
|
|
||||||
def __make_request(self, session: IpmiServerSession, name: str, method_path: str, **kwargs): # type: ignore
|
def __make_request(self, session: IpmiServerSession, name: str, func_path: str, **kwargs): # type: ignore
|
||||||
async def runner(): # type: ignore
|
async def runner(): # type: ignore
|
||||||
logger = get_logger(0)
|
logger = get_logger(0)
|
||||||
credentials = self.__auth_manager.get_credentials(session.username.decode())
|
credentials = self.__auth_manager.get_credentials(session.username.decode())
|
||||||
@ -169,8 +169,8 @@ class IpmiServer(BaseIpmiServer): # pylint: disable=too-many-instance-attribute
|
|||||||
session.sockaddr[0], name, credentials.ipmi_user, credentials.kvmd_user)
|
session.sockaddr[0], name, credentials.ipmi_user, credentials.kvmd_user)
|
||||||
try:
|
try:
|
||||||
async with self.__kvmd.make_session(credentials.kvmd_user, credentials.kvmd_passwd) as kvmd_session:
|
async with self.__kvmd.make_session(credentials.kvmd_user, credentials.kvmd_passwd) as kvmd_session:
|
||||||
method = functools.reduce(getattr, method_path.split("."), kvmd_session)
|
func = functools.reduce(getattr, func_path.split("."), kvmd_session)
|
||||||
return (await method(**kwargs))
|
return (await func(**kwargs))
|
||||||
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
|
except (aiohttp.ClientError, asyncio.TimeoutError) as err:
|
||||||
logger.error("[%s]: Can't perform request %s: %s", session.sockaddr[0], name, err)
|
logger.error("[%s]: Can't perform request %s: %s", session.sockaddr[0], name, err)
|
||||||
raise
|
raise
|
||||||
|
|||||||
@ -182,20 +182,20 @@ class _GpioOutput: # pylint: disable=too-many-instance-attributes
|
|||||||
# =====
|
# =====
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic
|
||||||
async def __run_action(self, wait: bool, name: str, method: Callable, *args: Any) -> None:
|
async def __run_action(self, wait: bool, name: str, func: Callable, *args: Any) -> None:
|
||||||
if wait:
|
if wait:
|
||||||
async with self.__region:
|
async with self.__region:
|
||||||
await method(*args)
|
await func(*args)
|
||||||
else:
|
else:
|
||||||
await aiotools.run_region_task(
|
await aiotools.run_region_task(
|
||||||
f"Can't perform {name} of {self} or operation was not completed",
|
f"Can't perform {name} of {self} or operation was not completed",
|
||||||
self.__region, self.__action_task_wrapper, name, method, *args,
|
self.__region, self.__action_task_wrapper, name, func, *args,
|
||||||
)
|
)
|
||||||
|
|
||||||
@aiotools.atomic
|
@aiotools.atomic
|
||||||
async def __action_task_wrapper(self, name: str, method: Callable, *args: Any) -> None:
|
async def __action_task_wrapper(self, name: str, func: Callable, *args: Any) -> None:
|
||||||
try:
|
try:
|
||||||
return (await method(*args))
|
return (await func(*args))
|
||||||
except GpioDriverOfflineError:
|
except GpioDriverOfflineError:
|
||||||
get_logger(0).error("Can't perform %s of %s or operation was not completed: driver offline", name, self)
|
get_logger(0).error("Can't perform %s of %s or operation was not completed: driver offline", name, self)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user