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():
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()