Refactor action parsers as generators

This commit is contained in:
Kovid Goyal 2021-05-26 13:45:47 +05:30
parent 6c344d4ae2
commit 27a459b0dd
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 13 additions and 10 deletions

View File

@ -8,8 +8,8 @@ import re
from contextlib import contextmanager, suppress from contextlib import contextmanager, suppress
from functools import partial from functools import partial
from typing import ( from typing import (
Any, Callable, Dict, FrozenSet, Generator, Iterable, List, NamedTuple, Any, Callable, Dict, FrozenSet, Generator, Iterable, List, Optional,
Optional, Sequence, Tuple, Type, Union Sequence, Tuple, Type, Union
) )
from . import fast_data_types as defines from . import fast_data_types as defines
@ -343,7 +343,8 @@ def parse_send_text(val: str, key_definitions: List[KeyDefinition]) -> None:
mode, sc = parts[:2] mode, sc = parts[:2]
text = ' '.join(parts[2:]) text = ' '.join(parts[2:])
key_str = '{} send_text {} {}'.format(sc, mode, text) key_str = '{} send_text {} {}'.format(sc, mode, text)
parse_key(key_str, key_definitions) for k in parse_key(key_str):
key_definitions.append(k)
SpecialHandlerFunc = Callable[[str, str, Dict[str, Any]], None] SpecialHandlerFunc = Callable[[str, str, Dict[str, Any]], None]
@ -365,12 +366,14 @@ def deprecated_handler(*names: str) -> Callable[[SpecialHandlerFunc], SpecialHan
@special_handler @special_handler
def handle_map(key: str, val: str, ans: Dict[str, Any]) -> None: def handle_map(key: str, val: str, ans: Dict[str, Any]) -> None:
parse_key(val, ans['key_definitions']) for k in parse_key(val):
ans['key_definitions'].append(k)
@special_handler @special_handler
def handle_mouse_map(key: str, val: str, ans: Dict[str, Any]) -> None: def handle_mouse_map(key: str, val: str, ans: Dict[str, Any]) -> None:
parse_mouse_action(val, ans['mouse_mappings']) for ma in parse_mouse_action(val):
ans['mouse_mappings'].append(ma)
@special_handler @special_handler

View File

@ -489,7 +489,7 @@ class KeyDefinition(BaseDefinition):
self.rest = tuple(map(r, self.rest)) self.rest = tuple(map(r, self.rest))
def parse_key(val: str, key_definitions: List[KeyDefinition]) -> None: def parse_key(val: str) -> Iterable[KeyDefinition]:
parts = val.split(maxsplit=1) parts = val.split(maxsplit=1)
if len(parts) != 2: if len(parts) != 2:
return return
@ -533,13 +533,13 @@ def parse_key(val: str, key_definitions: List[KeyDefinition]) -> None:
if paction is not None: if paction is not None:
if is_sequence: if is_sequence:
if trigger is not None: if trigger is not None:
key_definitions.append(KeyDefinition(True, paction, trigger[0], trigger[1], trigger[2], rest)) yield KeyDefinition(True, paction, trigger[0], trigger[1], trigger[2], rest)
else: else:
assert key is not None assert key is not None
key_definitions.append(KeyDefinition(False, paction, mods, is_native, key)) yield KeyDefinition(False, paction, mods, is_native, key)
def parse_mouse_action(val: str, mouse_mappings: List[MouseMapping]) -> None: def parse_mouse_action(val: str) -> Iterable[MouseMapping]:
parts = val.split(maxsplit=3) parts = val.split(maxsplit=3)
if len(parts) != 4: if len(parts) != 4:
log_error(f'Ignoring invalid mouse action: {val}') log_error(f'Ignoring invalid mouse action: {val}')
@ -578,4 +578,4 @@ def parse_mouse_action(val: str, mouse_mappings: List[MouseMapping]) -> None:
log_error(f'Ignoring unknown mouse action: {action}') log_error(f'Ignoring unknown mouse action: {action}')
return return
for mode in specified_modes: for mode in specified_modes:
mouse_mappings.append(MouseMapping(button, mods, count, mode == 'grabbed', paction)) yield MouseMapping(button, mods, count, mode == 'grabbed', paction)