improved some logging

This commit is contained in:
Devaev Maxim
2020-05-25 01:39:12 +03:00
parent eeece34312
commit 499cbb0cc5
3 changed files with 40 additions and 43 deletions

View File

@@ -25,7 +25,6 @@ import pkgutil
import functools
from typing import Dict
from typing import Optional
import Xlib.keysymdef
@@ -47,35 +46,32 @@ class SymmapWebKey:
def build_symmap(path: str) -> Dict[int, SymmapWebKey]:
# https://github.com/qemu/qemu/blob/95a9457fd44ad97c518858a4e1586a5498f9773c/ui/keymaps.c
return {
x11_code: web_key
for (x11_code, at1_key) in [
*list(X11_TO_AT1.items()),
*list(_read_keyboard_layout(path).items()),
]
if (web_key := _make_safe_web_key(at1_key)) is not None
}
logger = get_logger()
symmap: Dict[int, SymmapWebKey] = {}
for (src, items) in [
("<builtin>", list(X11_TO_AT1.items())),
(path, list(_read_keyboard_layout(path).items())),
]:
for (code, key) in items:
if (web_name := AT1_TO_WEB.get(key.code)) is not None:
if (
(web_name in ["ShiftLeft", "ShiftRight"] and key.shift) # pylint: disable=too-many-boolean-expressions
or (web_name in ["AltLeft", "AltRight"] and key.altgr)
or (web_name in ["ControlLeft", "ControlRight"] and key.ctrl)
):
logger.error("Invalid modifier key at mapping %s: %s / %s", src, web_name, key)
continue
symmap[code] = SymmapWebKey(
name=web_name,
shift=key.shift,
altgr=key.altgr,
ctrl=key.ctrl,
)
return symmap
# =====
def _make_safe_web_key(key: At1Key) -> Optional[SymmapWebKey]:
if (web_name := AT1_TO_WEB.get(key.code)) is not None:
if (
(web_name in ["ShiftLeft", "ShiftRight"] and key.shift) # pylint: disable=too-many-boolean-expressions
or (web_name in ["AltLeft", "AltRight"] and key.altgr)
or (web_name in ["ControlLeft", "ControlRight"] and key.ctrl)
):
get_logger().error("Invalid modifier key: %s / %s", web_name, key)
return None # Ехал модификатор через модификатор
return SymmapWebKey(
name=web_name,
shift=key.shift,
altgr=key.altgr,
ctrl=key.ctrl,
)
return None
@functools.lru_cache()
def _get_keysyms() -> Dict[str, int]:
keysyms: Dict[str, int] = {}
@@ -117,14 +113,14 @@ def _read_keyboard_layout(path: str) -> Dict[int, At1Key]: # Keysym to evdev (a
parts = line.split()
if len(parts) >= 2:
if (x11_code := _resolve_keysym(parts[0])) != 0:
if (code := _resolve_keysym(parts[0])) != 0:
try:
layout[x11_code] = At1Key(
layout[code] = At1Key(
code=int(parts[1], 16),
shift=("shift" in parts[2:]),
altgr=("altgr" in parts[2:]),
ctrl=("ctrl" in parts[2:]),
)
except ValueError as err:
logger.error("Can't parse layout line #%d: %s", number, str(err))
logger.error("Syntax error at %s:%d: %s", path, number, err)
return layout