support to add macro functions with optional parameters eventually

xkb
sezanzeb 4 years ago committed by sezanzeb
parent ab56263c3b
commit 7f3a010128

@ -120,13 +120,13 @@ class _Macro:
self.running = False self.running = False
def press_key(self): def press_key(self):
"""Tell this and all child macros that the key was pressed down.""" """The user pressed the key down."""
self.holding = True self.holding = True
for macro in self.child_macros: for macro in self.child_macros:
macro.press_key() macro.press_key()
def release_key(self): def release_key(self):
"""Tell all child macros that the key was released.""" """The user released the key."""
self.holding = False self.holding = False
for macro in self.child_macros: for macro in self.child_macros:
macro.release_key() macro.release_key()
@ -349,17 +349,18 @@ def _parse_recurse(macro, macro_instance=None, depth=0):
# fail here. # fail here.
logger.warn('"%s" doesn\'t write any keys (using k)', macro) logger.warn('"%s" doesn\'t write any keys (using k)', macro)
# available functions in the macro and the number of their # available functions in the macro and the minimum and maximum number
# parameters # of their parameters
functions = { functions = {
'm': (macro_instance.modify, 2), 'm': (macro_instance.modify, 2, 2),
'r': (macro_instance.repeat, 2), 'r': (macro_instance.repeat, 2, 2),
'k': (macro_instance.keycode, 1), 'k': (macro_instance.keycode, 1, 1),
'w': (macro_instance.wait, 1), 'w': (macro_instance.wait, 1, 1),
'h': (macro_instance.hold, 1) 'h': (macro_instance.hold, 1, 1)
} }
if functions.get(call) is None: function = functions.get(call)
if function is None:
raise Exception(f'Unknown function {call}') raise Exception(f'Unknown function {call}')
# get all the stuff inbetween # get all the stuff inbetween
@ -378,13 +379,19 @@ def _parse_recurse(macro, macro_instance=None, depth=0):
logger.spam('%sadd call to %s with %s', space, call, params) logger.spam('%sadd call to %s with %s', space, call, params)
if len(params) != functions[call][1]: if len(params) < function[1] or len(params) > function[2]:
if function[1] != function[2]:
raise ValueError( raise ValueError(
f'{call} takes {functions[call][1]}, not {len(params)} ' f'{call} takes between {function[1]} and {function[2]}, '
'parameters' f'not {len(params)} parameters'
)
else:
raise ValueError(
f'{call} takes {function[1]}, '
f'not {len(params)} parameters'
) )
functions[call][0](*params) function[0](*params)
# is after this another call? Chain it to the macro_instance # is after this another call? Chain it to the macro_instance
if len(macro) > position and macro[position] == '.': if len(macro) > position and macro[position] == '.':

@ -128,10 +128,12 @@ class TestMacros(unittest.TestCase):
self.assertIn('bracket', error) self.assertIn('bracket', error)
error = parse('k((1).k)', return_errors=True) error = parse('k((1).k)', return_errors=True)
self.assertIsNotNone(error) self.assertIsNotNone(error)
error = parse('k()', return_errors=True)
self.assertIsNotNone(error)
error = parse('r(a, k(1))', return_errors=True) error = parse('r(a, k(1))', return_errors=True)
self.assertIsNotNone(error) self.assertIsNotNone(error)
error = parse('k()', return_errors=True)
self.assertIsNotNone(error)
error = parse('k(1)', return_errors=True)
self.assertIsNone(error)
error = parse('k(1, 1)', return_errors=True) error = parse('k(1, 1)', return_errors=True)
self.assertIsNotNone(error) self.assertIsNotNone(error)
error = parse('h(1, 1)', return_errors=True) error = parse('h(1, 1)', return_errors=True)

Loading…
Cancel
Save