3 """Hooke - A force spectroscopy review & analysis tool.
12 from . import engine as engine
13 from . import config as config_mod
14 from . import playlist as playlist
15 from . import plugin as plugin_mod
16 from . import driver as driver_mod
17 from . import ui as ui
21 def __init__(self, config=None, debug=0):
23 default_settings = (config_mod.DEFAULT_SETTINGS
24 + plugin_mod.default_settings()
25 + driver_mod.default_settings()
26 + ui.default_settings())
28 config = config_mod.HookeConfigParser(
29 paths=config_mod.DEFAULT_PATHS,
30 default_settings=default_settings)
36 self.command = engine.CommandEngine()
38 self.playlists = playlist.NoteIndexList()
40 def load_plugins(self):
41 self.plugins = plugin_mod.load_graph(
42 plugin_mod.PLUGIN_GRAPH, self.config, include_section='plugins')
44 for plugin in self.plugins:
45 self.commands.extend(plugin.commands())
47 def load_drivers(self):
48 self.drivers = plugin_mod.load_graph(
49 driver_mod.DRIVER_GRAPH, self.config, include_section='drivers')
52 self.ui = ui.load_ui(self.config)
55 self.config.write() # Does not preserve original comments
58 """Run Hooke's main execution loop.
60 Spawns a :class:`hooke.engine.CommandEngine` subprocess and
61 then runs the UI, rejoining the `CommandEngine` process after
64 ui_to_command = multiprocessing.Queue()
65 command_to_ui = multiprocessing.Queue()
66 command = multiprocessing.Process(
67 target=self.command.run, args=(self, ui_to_command, command_to_ui))
70 self.ui.run(self.commands, ui_to_command, command_to_ui)
72 ui_to_command.put(ui.CloseEngine())
75 def run_lines(self, lines):
76 """Run the pre-set commands `lines` with the "command line" UI.
78 Allows for non-interactive sessions that are otherwise
79 equivalent to :meth:'.run'.
81 cmdline = ui.load_ui(self.config, 'command line')
82 ui_to_command = multiprocessing.Queue()
83 command_to_ui = multiprocessing.Queue()
84 command = multiprocessing.Process(
85 target=self.command.run, args=(self, ui_to_command, command_to_ui))
89 self.commands, ui_to_command, command_to_ui, lines)
91 ui_to_command.put(ui.CloseEngine())
95 p = optparse.OptionParser()
97 '-s', '--script', dest='script', metavar='FILE',
98 help='Script of command line Hooke commands to run.')
100 '-c', '--command', dest='commands', metavar='COMMAND',
101 action='append', default=[],
102 help='Add a command line Hooke command to run.')
103 options,arguments = p.parse_args()
104 if len(arguments) > 0:
105 print >> sys.stderr, 'Too many arguments to %s: %d > 0' \
106 % (sys.argv[0], len(arguments))
107 print >> sys.stderr, p.help()
110 app = Hooke(debug=__debug__)
112 if options.script != None:
113 f = open(os.path.expanduser(options.script), 'r')
114 options.commands.extend(f.readlines())
116 if len(options.commands) > 0:
117 app.run_lines(options.commands)
125 if __name__ == '__main__':