From ea537e738fddb19b55a3ec72edf58130a6e57f8f Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 16 May 2010 09:53:53 -0400 Subject: [PATCH] Broke out Hooke.run and Hooke.run_lines into HookeRunner. This allows the main process access to the command process' possibly altered Hooke instance. For example: >>> h = Hooke() >>> r = HookeRunner() >>> h = r.run_lines(h, ['load_playlist test/data/test']) >>> h = r.run_lines(h, ['curve_info --all=True']) With the old implementation, second run_lines() call would fail because the playlist was only loaded in the first command process' Hooke instance, not the Hooke instance that the main (UI) process held. --- hooke/engine.py | 1 + hooke/hooke.py | 41 ++++++++++++++++++++++++++--------------- test/load_playlist.py | 5 +++-- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/hooke/engine.py b/hooke/engine.py index de1ae70..a973730 100644 --- a/hooke/engine.py +++ b/hooke/engine.py @@ -40,6 +40,7 @@ class CommandEngine (object): while True: msg = ui_to_command_queue.get() if isinstance(msg, CloseEngine): + command_to_ui_queue.put(hooke) break assert isinstance(msg, CommandMessage), type(msg) msg.command.run(hooke, ui_to_command_queue, command_to_ui_queue, diff --git a/hooke/hooke.py b/hooke/hooke.py index 72fab31..e8a0050 100644 --- a/hooke/hooke.py +++ b/hooke/hooke.py @@ -73,44 +73,51 @@ class Hooke (object): def close(self): self.config.write() # Does not preserve original comments - def run(self): +class HookeRunner (object): + def run(self, hooke): """Run Hooke's main execution loop. Spawns a :class:`hooke.engine.CommandEngine` subprocess and then runs the UI, rejoining the `CommandEngine` process after the UI exits. """ - ui_to_command,command_to_ui,command = self._setup_run() + ui_to_command,command_to_ui,command = self._setup_run(hooke) try: - self.ui.run(self.commands, ui_to_command, command_to_ui) + self.ui.run(hooke.commands, ui_to_command, command_to_ui) finally: - self._cleanup_command(ui_to_command, command_to_ui, command) + hooke = self._cleanup_run(ui_to_command, command_to_ui, command) + return hooke - def run_lines(self, lines): + def run_lines(self, hooke, lines): """Run the pre-set commands `lines` with the "command line" UI. Allows for non-interactive sessions that are otherwise equivalent to :meth:'.run'. """ - cmdline = ui.load_ui(self.config, 'command line') - ui_to_command,command_to_ui,command = self._setup_run() + cmdline = ui.load_ui(hooke.config, 'command line') + ui_to_command,command_to_ui,command = self._setup_run(hooke) try: cmdline.run_lines( - self.commands, ui_to_command, command_to_ui, lines) + hooke.commands, ui_to_command, command_to_ui, lines) finally: - self._cleanup_command(ui_to_command, command_to_ui, command) + hooke = self._cleanup_run(ui_to_command, command_to_ui, command) + return hooke - def _setup_run(self): + def _setup_run(self, hooke): ui_to_command = multiprocessing.Queue() command_to_ui = multiprocessing.Queue() + manager = multiprocessing.Manager() command = multiprocessing.Process(name='command engine', - target=self.command.run, args=(self, ui_to_command, command_to_ui)) + target=hooke.command.run, args=(hooke, ui_to_command, command_to_ui)) command.start() return (ui_to_command, command_to_ui, command) def _cleanup_run(self, ui_to_command, command_to_ui, command): ui_to_command.put(ui.CloseEngine()) + hooke = command_to_ui.get() + assert isinstance(hooke, Hooke) command.join() + return hooke def main(): @@ -129,20 +136,24 @@ def main(): print >> sys.stderr, p.help() sys.exit(1) - app = Hooke(debug=__debug__) + hooke = Hooke(debug=__debug__) + runner = HookeRunner() if options.script != None: f = open(os.path.expanduser(options.script), 'r') options.commands.extend(f.readlines()) f.close if len(options.commands) > 0: - app.run_lines(options.commands) + try: + hooke = runner.run_lines(hooke, options.commands) + finally: + hooke.close() sys.exit(0) try: - app.run() + hooke = runner.run(hooke) finally: - app.close() + hooke.close() if __name__ == '__main__': main() diff --git a/test/load_playlist.py b/test/load_playlist.py index 46af92e..18d8295 100644 --- a/test/load_playlist.py +++ b/test/load_playlist.py @@ -17,9 +17,10 @@ # . """ ->>> from hooke.hooke import Hooke +>>> from hooke.hooke import Hooke, HookeRunner >>> h = Hooke() ->>> h.run_lines(['load_playlist test/data/test']) # doctest: +ELLIPSIS +>>> r = HookeRunner() +>>> h = r.run_lines(h, ['load_playlist test/data/test']) # doctest: +ELLIPSIS Success -- 2.26.2