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.
while True:
msg = ui_to_command_queue.get()
if isinstance(msg, CloseEngine):
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,
break
assert isinstance(msg, CommandMessage), type(msg)
msg.command.run(hooke, ui_to_command_queue, command_to_ui_queue,
def close(self):
self.config.write() # Does not preserve original comments
def close(self):
self.config.write() # Does not preserve original comments
+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.
"""
"""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)
- self.ui.run(self.commands, ui_to_command, command_to_ui)
+ self.ui.run(hooke.commands, ui_to_command, command_to_ui)
- 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'.
"""
"""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)
- self.commands, ui_to_command, command_to_ui, lines)
+ hooke.commands, ui_to_command, command_to_ui, lines)
- 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, hooke):
ui_to_command = multiprocessing.Queue()
command_to_ui = multiprocessing.Queue()
ui_to_command = multiprocessing.Queue()
command_to_ui = multiprocessing.Queue()
+ manager = multiprocessing.Manager()
command = multiprocessing.Process(name='command engine',
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())
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)
print >> sys.stderr, p.help()
sys.exit(1)
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:
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()
+ hooke = runner.run(hooke)
if __name__ == '__main__':
main()
if __name__ == '__main__':
main()
# <http://www.gnu.org/licenses/>.
"""
# <http://www.gnu.org/licenses/>.
"""
->>> from hooke.hooke import Hooke
+>>> from hooke.hooke import Hooke, HookeRunner
->>> h.run_lines(['load_playlist test/data/test']) # doctest: +ELLIPSIS
+>>> r = HookeRunner()
+>>> h = r.run_lines(h, ['load_playlist test/data/test']) # doctest: +ELLIPSIS
<FilePlaylist test.hkp>
Success
<BLANKLINE>
<FilePlaylist test.hkp>
Success
<BLANKLINE>