remote-evdev/remote_evdev/config/__init__.py

85 lines
2.4 KiB
Python
Raw Normal View History

2022-07-20 04:41:09 +00:00
from dataclasses import dataclass
import sys
from os import path
2022-07-25 11:52:55 +00:00
from enum import Enum
class DeviceType(Enum):
KEYBOARD = 0
POINTER = 1
TOUCH = 2
OTHER = 3
2022-07-20 04:41:09 +00:00
@dataclass
class DeviceInfo:
path: str
2022-07-25 11:52:55 +00:00
device_type: DeviceType
2022-07-20 04:41:09 +00:00
@dataclass
class NetConfig:
is_server: bool
is_host: bool
port: int
ip_address: str
def get_config() -> tuple[NetConfig, list[DeviceInfo]]:
args = sys.argv
cfg = NetConfig(
is_server=False,
is_host=True,
port=64654,
ip_address="auto"
)
devices_info = []
n = 1
while n < len(args):
match args[n]:
case "-s" | "--server": cfg.is_server = True
case "-c" | "--client": cfg.is_server = False
case "-h" | "--host": cfg.is_host = True
case "-g" | "--guest": cfg.is_host = False
case "-p" | "--port":
n += 1
try:
cfg.port = int(args[n])
except ValueError:
raise ValueError(f"Port must be a integer, not {args[n]}")
case "-a" | "--ip-address":
cfg.ip_address = args[n := n+1]
case "-d" | "--device":
devices_info.append(DeviceInfo(
path="",
device_type="other"
))
case _:
key = "path"
value = ""
match args[n]:
case "--id": value = "/dev/input/by-id/"
case "--path": value = "/dev/input/by-path/"
case "--event": value = "/dev/input/"
case "--full-path": pass
case "--type":
2022-07-25 11:52:55 +00:00
match args[n+1].upper():
case "POINTER" | "KEYBOARD" | "TOUCH" | "OTHER": key = "type"
2022-07-20 04:41:09 +00:00
case _: raise ValueError(f"Invalid device type {args[n+1]}")
n += 1
match key:
case "path":
value = f"{value}{args[n]}"
if path.exists(value):
devices_info[-1].path = value
2022-07-25 11:52:55 +00:00
case "type":
devices_info[-1].device_type = getattr(DeviceType, args[n].upper())
2022-07-20 04:41:09 +00:00
n += 1
if cfg.ip_address == "auto":
raise ValueError(f"Auto IP address not implemented yet")
return cfg, devices_info