improved macro parsing error logging

This commit is contained in:
sezanzeb 2020-12-04 14:52:08 +01:00 committed by sezanzeb
parent c2e9e822dd
commit 032ebd09c6
3 changed files with 30 additions and 2 deletions

View File

@ -294,9 +294,12 @@ class KeycodeInjector:
macros = {} macros = {}
for (ev_type, keycode), output in self.mapping: for (ev_type, keycode), output in self.mapping:
if is_this_a_macro(output): if is_this_a_macro(output):
macros[keycode] = parse(output) macro = parse(output)
if macro is None:
continue continue
macros[keycode] = macro
# certain capabilities can have side effects apparently. with an # certain capabilities can have side effects apparently. with an
# EV_ABS capability, EV_REL won't move the mouse pointer anymore. # EV_ABS capability, EV_REL won't move the mouse pointer anymore.
# so don't merge all InputDevices into one UInput device. # so don't merge all InputDevices into one UInput device.

View File

@ -119,6 +119,12 @@ class _Macro:
modifier : str modifier : str
macro : _Macro macro : _Macro
""" """
if not isinstance(macro, _Macro):
raise ValueError(
'Expected the second param for repeat to be '
f'a macro, but got {macro}'
)
modifier = str(modifier) modifier = str(modifier)
code = system_mapping.get(modifier) code = system_mapping.get(modifier)
@ -143,7 +149,14 @@ class _Macro:
repeats : int repeats : int
macro : _Macro macro : _Macro
""" """
if not isinstance(macro, _Macro):
raise ValueError(
'Expected the second param for repeat to be '
f'a macro, but got "{macro}"'
)
repeats = int(repeats) repeats = int(repeats)
for _ in range(repeats): for _ in range(repeats):
self.tasks.append((CHILD_MACRO, macro)) self.tasks.append((CHILD_MACRO, macro))
return self return self
@ -310,9 +323,12 @@ def _parse_recurse(macro, macro_instance=None, depth=0):
# probably a parameter for an outer function # probably a parameter for an outer function
try: try:
# if possible, parse as int
macro = int(macro) macro = int(macro)
except ValueError: except ValueError:
# use as string instead
pass pass
logger.spam('%s%s %s', space, type(macro), macro) logger.spam('%s%s %s', space, type(macro), macro)
return macro return macro
@ -320,6 +336,10 @@ def _parse_recurse(macro, macro_instance=None, depth=0):
def parse(macro): def parse(macro):
"""parse and generate a _Macro that can be run as often as you want. """parse and generate a _Macro that can be run as often as you want.
You need to use set_handler on it before running. If it could not
be parsed, possibly due to syntax errors, will log the error and
return None.
Parameters Parameters
---------- ----------
macro : string macro : string

View File

@ -52,6 +52,11 @@ class TestMacros(unittest.TestCase):
self.loop.run_until_complete(macro.run()) self.loop.run_until_complete(macro.run())
self.assertListEqual(self.result, [(one_code, 1), (one_code, 0)]) self.assertListEqual(self.result, [(one_code, 1), (one_code, 0)])
def test_fails(self):
self.assertIsNone(parse('r(1, a)'))
self.assertIsNone(parse('r(a, k(b))'))
self.assertIsNone(parse('m(a, b)'))
def test_0(self): def test_0(self):
macro = parse('k(1)') macro = parse('k(1)')
macro.set_handler(self.handler) macro.set_handler(self.handler)