diff --git a/keymap_drawer/parse/kanata.py b/keymap_drawer/parse/kanata.py index e2da72b..dfb5954 100644 --- a/keymap_drawer/parse/kanata.py +++ b/keymap_drawer/parse/kanata.py @@ -1,6 +1,5 @@ """Module containing class to parse devicetree format ZMK keymaps.""" -import re from itertools import chain, islice from pathlib import Path from typing import Sequence @@ -38,10 +37,10 @@ def __init__( layer_names: list[str] | None = None, ): super().__init__(config, columns, base_keymap, layer_names) - self.aliases = {} + self.aliases: dict[str, str | pp.ParseResults] = {} @classmethod - def _parse_cfg(cls, cfg_str: str, file_path: Path | None) -> list[str | pp.ParseResults]: + def _parse_cfg(cls, cfg_str: str, file_path: Path | None) -> list[pp.ParseResults]: parsed = ( pp.nested_expr("(", ")") .ignore(";;" + pp.SkipTo(pp.lineEnd)) @@ -53,7 +52,7 @@ def _parse_cfg(cls, cfg_str: str, file_path: Path | None) -> list[str | pp.Parse includes = [node[1] for node in parsed if isinstance(node, pp.ParseResults) and node[0] == "include"] for include in includes[::-1]: with open(file_path.parent / Path(include), encoding="utf-8") as f: - parsed = cls._parse_cfg(f.read, None) + parsed + parsed = cls._parse_cfg(f.read(), None) + parsed return parsed @classmethod @@ -67,7 +66,6 @@ def _str_to_key( # pylint: disable=too-many-return-statements,too-many-locals,t binding: str | pp.ParseResults, current_layer: int | None, key_positions: Sequence[int], - no_shifted: bool = False, ) -> LayoutKey: binding_str = self._element_to_str(binding) if binding_str in self.raw_binding_map: @@ -91,6 +89,7 @@ def recurse(new_binding): return LayoutKey(tap=binding) assert isinstance(binding, pp.ParseResults) + assert self.layer_names is not None if binding[0].startswith("tap-hold"): tap_key = recurse(binding[3]) @@ -126,7 +125,7 @@ def _get_layers(self, defsrc: list[str], nodes: list[pp.ParseResults]) -> dict[s layers: dict[str, list[LayoutKey]] = {} for layer_ind, (layer_name, layer) in enumerate(layer_nodes.items()): - layers[layer_name] = [""] * len(_DEFSRC_60) + layers[layer_name] = [LayoutKey() for _ in range(len(_DEFSRC_60))] for src_key, layer_key in zip(defsrc, layer): if (key_pos := DEFSRC_TO_POS.get(src_key)) is not None: try: @@ -148,6 +147,8 @@ def batched(iterable, n): while batch := tuple(islice(it, n)): yield batch + assert self.layer_names is not None + combos = [] for combo_def in batched(chords_node, 5): pos_node, action_node, _, _, disabled_layers_node = combo_def @@ -157,7 +158,7 @@ def batched(iterable, n): continue try: - parsed_key = self._str_to_key(action_node, None, key_pos, no_shifted=True) + parsed_key = self._str_to_key(action_node, None, key_pos) # type: ignore except Exception as err: raise ParseError( f'Could not parse binding "{self._element_to_str(action_node)}" in combo node "{combo_def}" with exception "{err}"' @@ -173,7 +174,7 @@ def _parse(self, in_str: str, file_name: str | None = None) -> tuple[dict, Keyma """ Parse a ZMK keymap with its content and path and return the layout spec and KeymapData to be dumped to YAML. """ - nodes = self._parse_cfg(in_str, Path(file_name)) + nodes = self._parse_cfg(in_str, Path(file_name) if file_name else None) defsrc = next(node[1:] for node in nodes if node[0] == "defsrc") try: diff --git a/keymap_drawer/parse/parse.py b/keymap_drawer/parse/parse.py index a0759a9..c5bc457 100644 --- a/keymap_drawer/parse/parse.py +++ b/keymap_drawer/parse/parse.py @@ -54,6 +54,8 @@ def parse_modifier_fns(self, keycode: str) -> tuple[str, list[str]]: return keycode, [] def strip_modifiers(keycode: str, current_mods: list[str] | None = None) -> tuple[str, list[str]]: + assert self._modifier_fn_re is not None + if current_mods is None: current_mods = [] if not (m := self._modifier_fn_re.fullmatch(keycode)):