From 16269ff01edfa985d9c0cbea5fb254531eb4cc04 Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Tue, 9 Jun 2020 20:29:51 +0200 Subject: [PATCH] [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 --- bumblebee_status/core/input.py | 10 +++++++--- tests/core/test_module.py | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/bumblebee_status/core/input.py b/bumblebee_status/core/input.py index b775091..a2ebaa8 100644 --- a/bumblebee_status/core/input.py +++ b/bumblebee_status/core/input.py @@ -36,9 +36,13 @@ def __event_id(obj_id, button): return "{}::{}".format(obj_id, button_name(button)) -def __execute(cmd, wait=False): +def __execute(event, cmd, wait=False): 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: logging.error("failed to invoke callback: {}".format(e)) @@ -49,7 +53,7 @@ def register(obj, button=None, cmd=None, wait=False): if callable(cmd): core.event.register(event_id, cmd) else: - core.event.register(event_id, lambda _: __execute(cmd, wait)) + core.event.register(event_id, lambda event: __execute(event, cmd, wait)) def trigger(event): diff --git a/tests/core/test_module.py b/tests/core/test_module.py index 7628aac..307277e 100644 --- a/tests/core/test_module.py +++ b/tests/core/test_module.py @@ -18,6 +18,7 @@ class TestModule(core.module.Module): class module(unittest.TestCase): def setUp(self): + core.event.clear() self.invalidModuleName = "invalid-module-name" self.validModuleName = "test" 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) + 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