Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
caksoylar committed May 20, 2024
1 parent d3306e0 commit 79d1a64
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
17 changes: 9 additions & 8 deletions keymap_drawer/parse/kanata.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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])
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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}"'
Expand All @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions keymap_drawer/parse/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down

0 comments on commit 79d1a64

Please sign in to comment.