using dataclasses instead of typed namedtuple

This commit is contained in:
Devaev Maxim 2019-06-01 03:54:40 +03:00
parent 187a195011
commit 7037bb0cfa
4 changed files with 23 additions and 17 deletions

View File

@ -23,15 +23,16 @@
import sys
import textwrap
import dataclasses
from typing import List
from typing import NamedTuple
import mako.template
# =====
class _KeyMapping(NamedTuple):
@dataclasses.dataclass(frozen=True)
class _KeyMapping:
kvmd_code: int
arduino_hid_key: str
web_key: str

View File

@ -20,9 +20,10 @@
# ========================================================================== #
import dataclasses
from typing import List
from typing import Dict
from typing import NamedTuple
# =====
@ -31,7 +32,8 @@ class IpmiPasswdError(Exception):
super().__init__("Incorrect IPMI passwd file: " + msg)
class IpmiUserCredentials(NamedTuple):
@dataclasses.dataclass(frozen=True)
class IpmiUserCredentials:
ipmi_user: str
ipmi_passwd: str
kvmd_user: str

View File

@ -50,19 +50,19 @@ class _BaseEvent:
raise NotImplementedError
@dataclasses.dataclass # pylint: disable=abstract-method
@dataclasses.dataclass(frozen=True) # pylint: disable=abstract-method
class _BoolEvent(_BaseEvent):
name: str
state: bool
@dataclasses.dataclass # pylint: disable=abstract-method
@dataclasses.dataclass(frozen=True) # pylint: disable=abstract-method
class _IntEvent(_BaseEvent):
x: int
y: int
@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class _KeyEvent(_BoolEvent):
def __post_init__(self) -> None:
assert self.name in keymap.KEYMAP
@ -75,7 +75,7 @@ class _KeyEvent(_BoolEvent):
return b"\x11" + key_bytes + state_bytes + b"\x00\x00"
@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class _MouseMoveEvent(_IntEvent):
def __post_init__(self) -> None:
assert -32768 <= self.x <= 32767
@ -85,7 +85,7 @@ class _MouseMoveEvent(_IntEvent):
return b"\x12" + struct.pack(">hh", self.x, self.y)
@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class _MouseButtonEvent(_BoolEvent):
def __post_init__(self) -> None:
assert self.name in ["left", "right"]
@ -100,7 +100,7 @@ class _MouseButtonEvent(_BoolEvent):
return b"\x13" + bytes([code]) + b"\x00\x00\x00"
@dataclasses.dataclass
@dataclasses.dataclass(frozen=True)
class _MouseWheelEvent(_IntEvent):
def __post_init__(self) -> None:
assert self.x == 0 # Горизонтальная прокрутка пока не поддерживается

View File

@ -24,10 +24,10 @@ import os
import struct
import asyncio
import asyncio.queues
import dataclasses
import types
from typing import Dict
from typing import NamedTuple
from typing import Callable
from typing import Type
from typing import AsyncGenerator
@ -84,19 +84,22 @@ class MsdIsBusyError(MsdOperationError, aioregion.RegionIsBusyError):
# =====
class _HardwareInfo(NamedTuple):
@dataclasses.dataclass(frozen=True)
class _HardwareInfo:
manufacturer: str
product: str
serial: str
class _ImageInfo(NamedTuple):
@dataclasses.dataclass(frozen=True)
class _ImageInfo:
name: str
size: int
complete: bool
class _MassStorageDeviceInfo(NamedTuple):
@dataclasses.dataclass(frozen=True)
class _MassStorageDeviceInfo:
path: str
real: str
size: int
@ -250,13 +253,13 @@ class MassStorageDevice: # pylint: disable=too-many-instance-attributes
def get_state(self) -> Dict:
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
if online:
if info:
info["hw"] = (info["hw"]._asdict() if info["hw"] else None)
info["image"] = (info["image"]._asdict() if info["image"] else None)
info["hw"] = (dataclasses.asdict(info["hw"]) if info["hw"] else None)
info["image"] = (dataclasses.asdict(info["image"]) if info["image"] else None)
connected_to = ("kvm" if self.__device_info else "server")
return {