mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 01:00:29 +08:00
using dataclasses instead of typed namedtuple
This commit is contained in:
parent
187a195011
commit
7037bb0cfa
@ -23,15 +23,16 @@
|
|||||||
|
|
||||||
import sys
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
|
import dataclasses
|
||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import NamedTuple
|
|
||||||
|
|
||||||
import mako.template
|
import mako.template
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
class _KeyMapping(NamedTuple):
|
@dataclasses.dataclass(frozen=True)
|
||||||
|
class _KeyMapping:
|
||||||
kvmd_code: int
|
kvmd_code: int
|
||||||
arduino_hid_key: str
|
arduino_hid_key: str
|
||||||
web_key: str
|
web_key: str
|
||||||
|
|||||||
@ -20,9 +20,10 @@
|
|||||||
# ========================================================================== #
|
# ========================================================================== #
|
||||||
|
|
||||||
|
|
||||||
|
import dataclasses
|
||||||
|
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import NamedTuple
|
|
||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
@ -31,7 +32,8 @@ class IpmiPasswdError(Exception):
|
|||||||
super().__init__("Incorrect IPMI passwd file: " + msg)
|
super().__init__("Incorrect IPMI passwd file: " + msg)
|
||||||
|
|
||||||
|
|
||||||
class IpmiUserCredentials(NamedTuple):
|
@dataclasses.dataclass(frozen=True)
|
||||||
|
class IpmiUserCredentials:
|
||||||
ipmi_user: str
|
ipmi_user: str
|
||||||
ipmi_passwd: str
|
ipmi_passwd: str
|
||||||
kvmd_user: str
|
kvmd_user: str
|
||||||
|
|||||||
@ -50,19 +50,19 @@ class _BaseEvent:
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass # pylint: disable=abstract-method
|
@dataclasses.dataclass(frozen=True) # pylint: disable=abstract-method
|
||||||
class _BoolEvent(_BaseEvent):
|
class _BoolEvent(_BaseEvent):
|
||||||
name: str
|
name: str
|
||||||
state: bool
|
state: bool
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass # pylint: disable=abstract-method
|
@dataclasses.dataclass(frozen=True) # pylint: disable=abstract-method
|
||||||
class _IntEvent(_BaseEvent):
|
class _IntEvent(_BaseEvent):
|
||||||
x: int
|
x: int
|
||||||
y: int
|
y: int
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass(frozen=True)
|
||||||
class _KeyEvent(_BoolEvent):
|
class _KeyEvent(_BoolEvent):
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
assert self.name in keymap.KEYMAP
|
assert self.name in keymap.KEYMAP
|
||||||
@ -75,7 +75,7 @@ class _KeyEvent(_BoolEvent):
|
|||||||
return b"\x11" + key_bytes + state_bytes + b"\x00\x00"
|
return b"\x11" + key_bytes + state_bytes + b"\x00\x00"
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass(frozen=True)
|
||||||
class _MouseMoveEvent(_IntEvent):
|
class _MouseMoveEvent(_IntEvent):
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
assert -32768 <= self.x <= 32767
|
assert -32768 <= self.x <= 32767
|
||||||
@ -85,7 +85,7 @@ class _MouseMoveEvent(_IntEvent):
|
|||||||
return b"\x12" + struct.pack(">hh", self.x, self.y)
|
return b"\x12" + struct.pack(">hh", self.x, self.y)
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass(frozen=True)
|
||||||
class _MouseButtonEvent(_BoolEvent):
|
class _MouseButtonEvent(_BoolEvent):
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
assert self.name in ["left", "right"]
|
assert self.name in ["left", "right"]
|
||||||
@ -100,7 +100,7 @@ class _MouseButtonEvent(_BoolEvent):
|
|||||||
return b"\x13" + bytes([code]) + b"\x00\x00\x00"
|
return b"\x13" + bytes([code]) + b"\x00\x00\x00"
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass(frozen=True)
|
||||||
class _MouseWheelEvent(_IntEvent):
|
class _MouseWheelEvent(_IntEvent):
|
||||||
def __post_init__(self) -> None:
|
def __post_init__(self) -> None:
|
||||||
assert self.x == 0 # Горизонтальная прокрутка пока не поддерживается
|
assert self.x == 0 # Горизонтальная прокрутка пока не поддерживается
|
||||||
|
|||||||
@ -24,10 +24,10 @@ import os
|
|||||||
import struct
|
import struct
|
||||||
import asyncio
|
import asyncio
|
||||||
import asyncio.queues
|
import asyncio.queues
|
||||||
|
import dataclasses
|
||||||
import types
|
import types
|
||||||
|
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import NamedTuple
|
|
||||||
from typing import Callable
|
from typing import Callable
|
||||||
from typing import Type
|
from typing import Type
|
||||||
from typing import AsyncGenerator
|
from typing import AsyncGenerator
|
||||||
@ -84,19 +84,22 @@ class MsdIsBusyError(MsdOperationError, aioregion.RegionIsBusyError):
|
|||||||
|
|
||||||
|
|
||||||
# =====
|
# =====
|
||||||
class _HardwareInfo(NamedTuple):
|
@dataclasses.dataclass(frozen=True)
|
||||||
|
class _HardwareInfo:
|
||||||
manufacturer: str
|
manufacturer: str
|
||||||
product: str
|
product: str
|
||||||
serial: str
|
serial: str
|
||||||
|
|
||||||
|
|
||||||
class _ImageInfo(NamedTuple):
|
@dataclasses.dataclass(frozen=True)
|
||||||
|
class _ImageInfo:
|
||||||
name: str
|
name: str
|
||||||
size: int
|
size: int
|
||||||
complete: bool
|
complete: bool
|
||||||
|
|
||||||
|
|
||||||
class _MassStorageDeviceInfo(NamedTuple):
|
@dataclasses.dataclass(frozen=True)
|
||||||
|
class _MassStorageDeviceInfo:
|
||||||
path: str
|
path: str
|
||||||
real: str
|
real: str
|
||||||
size: int
|
size: int
|
||||||
@ -250,13 +253,13 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
|
|||||||
|
|
||||||
def get_state(self) -> Dict:
|
def get_state(self) -> Dict:
|
||||||
online = (self._enabled and bool(self._device_path))
|
online = (self._enabled and bool(self._device_path))
|
||||||
info = (self.__saved_device_info._asdict() if self.__saved_device_info else None)
|
info = (dataclasses.asdict(self.__saved_device_info) if self.__saved_device_info else None)
|
||||||
connected_to: Optional[str] = None
|
connected_to: Optional[str] = None
|
||||||
|
|
||||||
if online:
|
if online:
|
||||||
if info:
|
if info:
|
||||||
info["hw"] = (info["hw"]._asdict() if info["hw"] else None)
|
info["hw"] = (dataclasses.asdict(info["hw"]) if info["hw"] else None)
|
||||||
info["image"] = (info["image"]._asdict() if info["image"] else None)
|
info["image"] = (dataclasses.asdict(info["image"]) if info["image"] else None)
|
||||||
connected_to = ("kvm" if self.__device_info else "server")
|
connected_to = ("kvm" if self.__device_info else "server")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user