From f262080e7854431c7ff41a31a53f1a48b68424fd Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Mon, 30 Mar 2020 21:18:28 +0200 Subject: [PATCH] [modules/shell] Small refactoring Use threads a bit differently do make do with fewer helper functions. --- bumblebee-status | 1 + modules/contrib/shell.py | 20 +++++++------------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/bumblebee-status b/bumblebee-status index c3541c6..cf057b0 100755 --- a/bumblebee-status +++ b/bumblebee-status @@ -64,6 +64,7 @@ def main(): core.event.trigger('stop') if __name__ == "__main__": + main() try: main() except Exception as e: diff --git a/modules/contrib/shell.py b/modules/contrib/shell.py index 87f6bee..9b4c236 100644 --- a/modules/contrib/shell.py +++ b/modules/contrib/shell.py @@ -40,15 +40,17 @@ class Module(core.module.Module): self.__command = self.parameter('command') self.__async = util.format.asbool(self.parameter('async')) - self.__output = '' if self.__async: self.__output = 'please wait...' - self.__current_thread = None + self.__current_thread = threading.Thread() # LMB and RMB will update output regardless of timer core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.update) core.input.register(self, button=core.input.RIGHT_MOUSE, cmd=self.update) + def set_output(self, value): + self.__output = value + def get_output(self, _): return self.__output @@ -59,22 +61,14 @@ class Module(core.module.Module): return # if previous thread didn't end yet then don't do anything - if self.__current_thread: + if self.__current_thread.is_alive(): return # spawn new thread to execute command and pass callback method to get output from it self.__current_thread = threading.Thread( - target=self.__run, - args=(self.__command, self.__output_function) + target=lambda obj, cmd: obj.set_output(util.cli.execute(cmd, ignore_errors=True)), + args=(self, self.__command) ) self.__current_thread.start() - def __run(self, command, output_callback): - output_callback(util.cli.execute(command, ignore_errors=True)) - - def __output_function(self, text): - self.__output = text - # clear this thread data, so next update will spawn a new one - self._current_thread = None - # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4