[modules/shell] Small refactoring

Use threads a bit differently do make do with fewer helper functions.
This commit is contained in:
tobi-wan-kenobi 2020-03-30 21:18:28 +02:00
parent 422a9986b4
commit f262080e78
2 changed files with 8 additions and 13 deletions

View file

@ -64,6 +64,7 @@ def main():
core.event.trigger('stop') core.event.trigger('stop')
if __name__ == "__main__": if __name__ == "__main__":
main()
try: try:
main() main()
except Exception as e: except Exception as e:

View file

@ -40,15 +40,17 @@ class Module(core.module.Module):
self.__command = self.parameter('command') self.__command = self.parameter('command')
self.__async = util.format.asbool(self.parameter('async')) self.__async = util.format.asbool(self.parameter('async'))
self.__output = ''
if self.__async: if self.__async:
self.__output = 'please wait...' self.__output = 'please wait...'
self.__current_thread = None self.__current_thread = threading.Thread()
# LMB and RMB will update output regardless of timer # 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.LEFT_MOUSE, cmd=self.update)
core.input.register(self, button=core.input.RIGHT_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, _): def get_output(self, _):
return self.__output return self.__output
@ -59,22 +61,14 @@ class Module(core.module.Module):
return return
# if previous thread didn't end yet then don't do anything # if previous thread didn't end yet then don't do anything
if self.__current_thread: if self.__current_thread.is_alive():
return return
# spawn new thread to execute command and pass callback method to get output from it # spawn new thread to execute command and pass callback method to get output from it
self.__current_thread = threading.Thread( self.__current_thread = threading.Thread(
target=self.__run, target=lambda obj, cmd: obj.set_output(util.cli.execute(cmd, ignore_errors=True)),
args=(self.__command, self.__output_function) args=(self, self.__command)
) )
self.__current_thread.start() 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 # vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4