[core/input] add variables to custom cli calls

when specifying event bindings via CLI (e.g. disk.left-click=thunar),
allow for variables ({instance} and {name}, in particular).

fixes #650
This commit is contained in:
tobi-wan-kenobi 2020-06-09 20:29:51 +02:00
parent 60cdbab76e
commit 16269ff01e
2 changed files with 32 additions and 3 deletions

View file

@ -36,9 +36,13 @@ def __event_id(obj_id, button):
return "{}::{}".format(obj_id, button_name(button)) return "{}::{}".format(obj_id, button_name(button))
def __execute(cmd, wait=False): def __execute(event, cmd, wait=False):
try: try:
util.cli.execute(cmd, wait=wait, shell=True) util.cli.execute(
cmd.format(instance=event.get("instance", ""), name=event.get("name", ""),),
wait=wait,
shell=True,
)
except Exception as e: except Exception as e:
logging.error("failed to invoke callback: {}".format(e)) logging.error("failed to invoke callback: {}".format(e))
@ -49,7 +53,7 @@ def register(obj, button=None, cmd=None, wait=False):
if callable(cmd): if callable(cmd):
core.event.register(event_id, cmd) core.event.register(event_id, cmd)
else: else:
core.event.register(event_id, lambda _: __execute(cmd, wait)) core.event.register(event_id, lambda event: __execute(event, cmd, wait))
def trigger(event): def trigger(event):

View file

@ -18,6 +18,7 @@ class TestModule(core.module.Module):
class module(unittest.TestCase): class module(unittest.TestCase):
def setUp(self): def setUp(self):
core.event.clear()
self.invalidModuleName = "invalid-module-name" self.invalidModuleName = "invalid-module-name"
self.validModuleName = "test" self.validModuleName = "test"
self.someWidget = core.widget.Widget("randomeWidgetContent", name="A") self.someWidget = core.widget.Widget("randomeWidgetContent", name="A")
@ -154,5 +155,29 @@ class module(unittest.TestCase):
cli.execute.assert_called_once_with(cmd, wait=False, shell=True) cli.execute.assert_called_once_with(cmd, wait=False, shell=True)
def test_configured_callbacks_with_parameters(self):
cfg = core.config.Config([])
module = TestModule(config=cfg, widgets=[self.someWidget])
cmd = "sample-tool {instance} {name}"
module.set("left-click", cmd)
module.register_callbacks()
with unittest.mock.patch("core.input.util.cli") as cli:
cli.execute.return_value = ""
core.input.trigger(
{
"button": core.input.LEFT_MOUSE,
"instance": module.id,
"name": "sample-name",
}
)
cli.execute.assert_called_once_with(
cmd.format(instance=module.id, name="sample-name"),
wait=False,
shell=True,
)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4