4 Hooke - A force spectroscopy review & analysis tool.
6 A free, open source data analysis platform
11 import multiprocessing
17 from . import engine as engine
18 from . import config as config_mod
19 from . import playlist as playlist
20 from . import plugin as plugin_mod
21 from . import driver as driver_mod
22 from . import ui as ui
26 def __init__(self, config=None, debug=0):
28 default_settings = (config_mod.DEFAULT_SETTINGS
29 + plugin_mod.default_settings()
30 + driver_mod.default_settings()
31 + ui.default_settings())
33 config = config_mod.HookeConfigParser(
34 paths=config_mod.DEFAULT_PATHS,
35 default_settings=default_settings)
41 self.command = engine.CommandEngine()
43 self.playlists = playlist.NoteIndexList()
45 def load_plugins(self):
46 self.plugins = plugin_mod.load_graph(
47 plugin_mod.PLUGIN_GRAPH, self.config, include_section='plugins')
49 for plugin in self.plugins:
50 self.commands.extend(plugin.commands())
52 def load_drivers(self):
53 self.drivers = plugin_mod.load_graph(
54 driver_mod.DRIVER_GRAPH, self.config, include_section='drivers')
57 self.ui = ui.load_ui(self.config)
60 self.config.write() # Does not preserve original comments
63 """Run Hooke's main execution loop.
65 Spawns a :class:`hooke.engine.CommandEngine` subprocess and
66 then runs the UI, rejoining the `CommandEngine` process after
69 ui_to_command = multiprocessing.Queue()
70 command_to_ui = multiprocessing.Queue()
71 command = multiprocessing.Process(
72 target=self.command.run, args=(self, ui_to_command, command_to_ui))
75 self.ui.run(self.commands, ui_to_command, command_to_ui)
77 ui_to_command.put(ui.CloseEngine())
80 def run_lines(self, lines):
81 """Run the pre-set commands `lines` with the "command line" UI.
83 Allows for non-interactive sessions that are otherwise
84 equivalent to :meth:'.run'.
86 cmdline = ui.load_ui(self.config, 'command line')
87 ui_to_command = multiprocessing.Queue()
88 command_to_ui = multiprocessing.Queue()
89 command = multiprocessing.Process(
90 target=self.command.run, args=(self, ui_to_command, command_to_ui))
94 self.commands, ui_to_command, command_to_ui, lines)
96 ui_to_command.put(ui.CloseEngine())
100 p = optparse.OptionParser()
102 '-s', '--script', dest='script', metavar='FILE',
103 help='Script of command line Hooke commands to run.')
105 '-c', '--command', dest='commands', metavar='COMMAND',
106 action='append', default=[],
107 help='Add a command line Hooke command to run.')
108 options,arguments = p.parse_args()
109 if len(arguments) > 0:
110 print >> sys.stderr, 'Too many arguments to %s: %d > 0' \
111 % (sys.argv[0], len(arguments))
112 print >> sys.stderr, p.help()
115 app = Hooke(debug=__debug__)
117 if options.script != None:
118 f = open(os.path.expanduser(options.script), 'r')
119 options.commands.extend(f.readlines())
121 if len(options.commands) > 0:
122 app.run_lines(options.commands)
130 if __name__ == '__main__':