mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-02-02 19:11:54 +08:00
new typing style
This commit is contained in:
@@ -20,8 +20,6 @@
|
||||
# ========================================================================== #
|
||||
|
||||
|
||||
from typing import Set
|
||||
|
||||
from ....yamlconf import Section
|
||||
|
||||
from .base import BaseInfoSubmanager
|
||||
@@ -45,7 +43,7 @@ class InfoManager:
|
||||
"fan": FanInfoSubmanager(**config.kvmd.info.fan._unpack()),
|
||||
}
|
||||
|
||||
def get_subs(self) -> Set[str]:
|
||||
def get_subs(self) -> set[str]:
|
||||
return set(self.__subs)
|
||||
|
||||
def get_submanager(self, name: str) -> BaseInfoSubmanager:
|
||||
|
||||
@@ -20,8 +20,6 @@
|
||||
# ========================================================================== #
|
||||
|
||||
|
||||
from typing import Dict
|
||||
|
||||
from .base import BaseInfoSubmanager
|
||||
|
||||
|
||||
@@ -30,5 +28,5 @@ class AuthInfoSubmanager(BaseInfoSubmanager):
|
||||
def __init__(self, enabled: bool) -> None:
|
||||
self.__enabled = enabled
|
||||
|
||||
async def get_state(self) -> Dict:
|
||||
async def get_state(self) -> dict:
|
||||
return {"enabled": self.__enabled}
|
||||
|
||||
@@ -20,11 +20,7 @@
|
||||
# ========================================================================== #
|
||||
|
||||
|
||||
from typing import Dict
|
||||
from typing import Optional
|
||||
|
||||
|
||||
# =====
|
||||
class BaseInfoSubmanager:
|
||||
async def get_state(self) -> Optional[Dict]:
|
||||
async def get_state(self) -> (dict | None):
|
||||
raise NotImplementedError
|
||||
|
||||
@@ -24,9 +24,6 @@ import os
|
||||
import re
|
||||
import asyncio
|
||||
|
||||
from typing import Dict
|
||||
from typing import Optional
|
||||
|
||||
from ....logging import get_logger
|
||||
|
||||
from ....yamlconf import Section
|
||||
@@ -45,7 +42,7 @@ class ExtrasInfoSubmanager(BaseInfoSubmanager):
|
||||
def __init__(self, global_config: Section) -> None:
|
||||
self.__global_config = global_config
|
||||
|
||||
async def get_state(self) -> Optional[Dict]:
|
||||
async def get_state(self) -> (dict | None):
|
||||
try:
|
||||
sui = sysunit.SystemdUnitInfo()
|
||||
await sui.open()
|
||||
@@ -53,7 +50,7 @@ class ExtrasInfoSubmanager(BaseInfoSubmanager):
|
||||
get_logger(0).error("Can't open systemd bus to get extras state: %s", tools.efmt(err))
|
||||
sui = None
|
||||
try:
|
||||
extras: Dict[str, Dict] = {}
|
||||
extras: dict[str, dict] = {}
|
||||
for extra in (await asyncio.gather(*[
|
||||
self.__read_extra(sui, name)
|
||||
for name in os.listdir(self.__get_extras_path())
|
||||
@@ -71,7 +68,7 @@ class ExtrasInfoSubmanager(BaseInfoSubmanager):
|
||||
def __get_extras_path(self, *parts: str) -> str:
|
||||
return os.path.join(self.__global_config.kvmd.info.extras, *parts)
|
||||
|
||||
async def __read_extra(self, sui: Optional[sysunit.SystemdUnitInfo], name: str) -> Dict:
|
||||
async def __read_extra(self, sui: (sysunit.SystemdUnitInfo | None), name: str) -> dict:
|
||||
try:
|
||||
extra = await aiotools.run_async(load_yaml_file, self.__get_extras_path(name, "manifest.yaml"))
|
||||
await self.__rewrite_app_daemon(sui, extra)
|
||||
@@ -81,7 +78,7 @@ class ExtrasInfoSubmanager(BaseInfoSubmanager):
|
||||
get_logger(0).exception("Can't read extra %r", name)
|
||||
return {}
|
||||
|
||||
async def __rewrite_app_daemon(self, sui: Optional[sysunit.SystemdUnitInfo], extra: Dict) -> None:
|
||||
async def __rewrite_app_daemon(self, sui: (sysunit.SystemdUnitInfo | None), extra: dict) -> None:
|
||||
daemon = extra.get("daemon", "")
|
||||
if isinstance(daemon, str) and daemon.strip():
|
||||
extra["enabled"] = extra["started"] = False
|
||||
@@ -91,7 +88,7 @@ class ExtrasInfoSubmanager(BaseInfoSubmanager):
|
||||
except Exception as err:
|
||||
get_logger(0).error("Can't get info about the service %r: %s", daemon, tools.efmt(err))
|
||||
|
||||
def __rewrite_app_port(self, extra: Dict) -> None:
|
||||
def __rewrite_app_port(self, extra: dict) -> None:
|
||||
port_path = extra.get("port", "")
|
||||
if isinstance(port_path, str) and port_path.strip():
|
||||
extra["port"] = 0
|
||||
|
||||
@@ -23,9 +23,7 @@
|
||||
import copy
|
||||
import asyncio
|
||||
|
||||
from typing import Dict
|
||||
from typing import AsyncGenerator
|
||||
from typing import Optional
|
||||
|
||||
import aiohttp
|
||||
|
||||
@@ -55,15 +53,15 @@ class FanInfoSubmanager(BaseInfoSubmanager):
|
||||
self.__timeout = timeout
|
||||
self.__state_poll = state_poll
|
||||
|
||||
async def get_state(self) -> Dict:
|
||||
async def get_state(self) -> dict:
|
||||
monitored = await self.__get_monitored()
|
||||
return {
|
||||
"monitored": monitored,
|
||||
"state": ((await self.__get_fan_state() if monitored else None)),
|
||||
}
|
||||
|
||||
async def poll_state(self) -> AsyncGenerator[Dict, None]:
|
||||
prev_state: Dict = {}
|
||||
async def poll_state(self) -> AsyncGenerator[dict, None]:
|
||||
prev_state: dict = {}
|
||||
while True:
|
||||
if self.__unix_path:
|
||||
pure = state = await self.get_state()
|
||||
@@ -93,7 +91,7 @@ class FanInfoSubmanager(BaseInfoSubmanager):
|
||||
get_logger(0).error("Can't get info about the service %r: %s", self.__daemon, tools.efmt(err))
|
||||
return False
|
||||
|
||||
async def __get_fan_state(self) -> Optional[Dict]:
|
||||
async def __get_fan_state(self) -> (dict | None):
|
||||
try:
|
||||
async with self.__make_http_session() as session:
|
||||
async with session.get("http://localhost/state") as response:
|
||||
@@ -104,7 +102,7 @@ class FanInfoSubmanager(BaseInfoSubmanager):
|
||||
return None
|
||||
|
||||
def __make_http_session(self) -> aiohttp.ClientSession:
|
||||
kwargs: Dict = {
|
||||
kwargs: dict = {
|
||||
"headers": {
|
||||
"User-Agent": htclient.make_user_agent("KVMD"),
|
||||
},
|
||||
|
||||
@@ -23,12 +23,9 @@
|
||||
import os
|
||||
import asyncio
|
||||
|
||||
from typing import List
|
||||
from typing import Dict
|
||||
from typing import Callable
|
||||
from typing import AsyncGenerator
|
||||
from typing import TypeVar
|
||||
from typing import Optional
|
||||
|
||||
from ....logging import get_logger
|
||||
|
||||
@@ -48,16 +45,16 @@ _RetvalT = TypeVar("_RetvalT")
|
||||
class HwInfoSubmanager(BaseInfoSubmanager):
|
||||
def __init__(
|
||||
self,
|
||||
vcgencmd_cmd: List[str],
|
||||
vcgencmd_cmd: list[str],
|
||||
state_poll: float,
|
||||
) -> None:
|
||||
|
||||
self.__vcgencmd_cmd = vcgencmd_cmd
|
||||
self.__state_poll = state_poll
|
||||
|
||||
self.__dt_cache: Dict[str, str] = {}
|
||||
self.__dt_cache: dict[str, str] = {}
|
||||
|
||||
async def get_state(self) -> Dict:
|
||||
async def get_state(self) -> dict:
|
||||
(model, serial, cpu_temp, throttling) = await asyncio.gather(
|
||||
self.__read_dt_file("model"),
|
||||
self.__read_dt_file("serial-number"),
|
||||
@@ -78,8 +75,8 @@ class HwInfoSubmanager(BaseInfoSubmanager):
|
||||
},
|
||||
}
|
||||
|
||||
async def poll_state(self) -> AsyncGenerator[Dict, None]:
|
||||
prev_state: Dict = {}
|
||||
async def poll_state(self) -> AsyncGenerator[dict, None]:
|
||||
prev_state: dict = {}
|
||||
while True:
|
||||
state = await self.get_state()
|
||||
if state != prev_state:
|
||||
@@ -89,7 +86,7 @@ class HwInfoSubmanager(BaseInfoSubmanager):
|
||||
|
||||
# =====
|
||||
|
||||
async def __read_dt_file(self, name: str) -> Optional[str]:
|
||||
async def __read_dt_file(self, name: str) -> (str | None):
|
||||
if name not in self.__dt_cache:
|
||||
path = os.path.join(f"{env.PROCFS_PREFIX}/proc/device-tree", name)
|
||||
try:
|
||||
@@ -99,7 +96,7 @@ class HwInfoSubmanager(BaseInfoSubmanager):
|
||||
return None
|
||||
return self.__dt_cache[name]
|
||||
|
||||
async def __get_cpu_temp(self) -> Optional[float]:
|
||||
async def __get_cpu_temp(self) -> (float | None):
|
||||
temp_path = f"{env.SYSFS_PREFIX}/sys/class/thermal/thermal_zone0/temp"
|
||||
try:
|
||||
return int((await aiofs.read(temp_path)).strip()) / 1000
|
||||
@@ -107,7 +104,7 @@ class HwInfoSubmanager(BaseInfoSubmanager):
|
||||
get_logger(0).error("Can't read CPU temp from %s: %s", temp_path, err)
|
||||
return None
|
||||
|
||||
async def __get_throttling(self) -> Optional[Dict]:
|
||||
async def __get_throttling(self) -> (dict | None):
|
||||
# https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=147781&start=50#p972790
|
||||
flags = await self.__parse_vcgencmd(
|
||||
arg="get_throttled",
|
||||
@@ -133,7 +130,7 @@ class HwInfoSubmanager(BaseInfoSubmanager):
|
||||
}
|
||||
return None
|
||||
|
||||
async def __parse_vcgencmd(self, arg: str, parser: Callable[[str], _RetvalT]) -> Optional[_RetvalT]:
|
||||
async def __parse_vcgencmd(self, arg: str, parser: Callable[[str], _RetvalT]) -> (_RetvalT | None):
|
||||
cmd = [*self.__vcgencmd_cmd, arg]
|
||||
try:
|
||||
text = (await aioproc.read_process(cmd, err_to_null=True))[1]
|
||||
|
||||
@@ -20,9 +20,6 @@
|
||||
# ========================================================================== #
|
||||
|
||||
|
||||
from typing import Dict
|
||||
from typing import Optional
|
||||
|
||||
from ....logging import get_logger
|
||||
|
||||
from ....yamlconf.loader import load_yaml_file
|
||||
@@ -37,7 +34,7 @@ class MetaInfoSubmanager(BaseInfoSubmanager):
|
||||
def __init__(self, meta_path: str) -> None:
|
||||
self.__meta_path = meta_path
|
||||
|
||||
async def get_state(self) -> Optional[Dict]:
|
||||
async def get_state(self) -> (dict | None):
|
||||
try:
|
||||
return ((await aiotools.run_async(load_yaml_file, self.__meta_path)) or {})
|
||||
except Exception:
|
||||
|
||||
@@ -24,9 +24,6 @@ import os
|
||||
import asyncio
|
||||
import platform
|
||||
|
||||
from typing import List
|
||||
from typing import Dict
|
||||
|
||||
from ....logging import get_logger
|
||||
|
||||
from .... import aioproc
|
||||
@@ -38,10 +35,10 @@ from .base import BaseInfoSubmanager
|
||||
|
||||
# =====
|
||||
class SystemInfoSubmanager(BaseInfoSubmanager):
|
||||
def __init__(self, streamer_cmd: List[str]) -> None:
|
||||
def __init__(self, streamer_cmd: list[str]) -> None:
|
||||
self.__streamer_cmd = streamer_cmd
|
||||
|
||||
async def get_state(self) -> Dict:
|
||||
async def get_state(self) -> dict:
|
||||
streamer_info = await self.__get_streamer_info()
|
||||
uname_info = platform.uname() # Uname using the internal cache
|
||||
return {
|
||||
@@ -55,9 +52,9 @@ class SystemInfoSubmanager(BaseInfoSubmanager):
|
||||
|
||||
# =====
|
||||
|
||||
async def __get_streamer_info(self) -> Dict:
|
||||
async def __get_streamer_info(self) -> dict:
|
||||
version = ""
|
||||
features: Dict[str, bool] = {}
|
||||
features: dict[str, bool] = {}
|
||||
try:
|
||||
path = self.__streamer_cmd[0]
|
||||
((_, version), (_, features_text)) = await asyncio.gather(
|
||||
|
||||
Reference in New Issue
Block a user