1 # Copyright (C) 2008-2010 Fabrizio Benedetti
2 # Massimo Sandal <devicerandom@gmail.com>
3 # Rolf Schmidt <rschmidt@alcor.concordia.ca>
4 # W. Trevor King <wking@drexel.edu>
6 # This file is part of Hooke.
8 # Hooke is free software: you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation, either
11 # version 3 of the License, or (at your option) any later version.
13 # Hooke is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with Hooke. If not, see
20 # <http://www.gnu.org/licenses/>.
22 """Hooke - A force spectroscopy review & analysis tool.
25 import multiprocessing
31 from . import engine as engine
32 from . import config as config_mod
33 from . import playlist as playlist
34 from . import plugin as plugin_mod
35 from . import driver as driver_mod
36 from . import ui as ui
40 def __init__(self, config=None, debug=0):
42 default_settings = (config_mod.DEFAULT_SETTINGS
43 + plugin_mod.default_settings()
44 + driver_mod.default_settings()
45 + ui.default_settings())
47 config = config_mod.HookeConfigParser(
48 paths=config_mod.DEFAULT_PATHS,
49 default_settings=default_settings)
55 self.command = engine.CommandEngine()
57 self.playlists = playlist.NoteIndexList()
59 def load_plugins(self):
60 self.plugins = plugin_mod.load_graph(
61 plugin_mod.PLUGIN_GRAPH, self.config, include_section='plugins')
63 for plugin in self.plugins:
64 self.commands.extend(plugin.commands())
66 def load_drivers(self):
67 self.drivers = plugin_mod.load_graph(
68 driver_mod.DRIVER_GRAPH, self.config, include_section='drivers')
71 self.ui = ui.load_ui(self.config)
74 self.config.write() # Does not preserve original comments
77 """Run Hooke's main execution loop.
79 Spawns a :class:`hooke.engine.CommandEngine` subprocess and
80 then runs the UI, rejoining the `CommandEngine` process after
83 ui_to_command,command_to_ui,command = self._setup_run()
85 self.ui.run(self.commands, ui_to_command, command_to_ui)
87 self._cleanup_command(ui_to_command, command_to_ui, command)
89 def run_lines(self, lines):
90 """Run the pre-set commands `lines` with the "command line" UI.
92 Allows for non-interactive sessions that are otherwise
93 equivalent to :meth:'.run'.
95 cmdline = ui.load_ui(self.config, 'command line')
96 ui_to_command,command_to_ui,command = self._setup_run()
99 self.commands, ui_to_command, command_to_ui, lines)
101 self._cleanup_command(ui_to_command, command_to_ui, command)
103 def _setup_run(self):
104 ui_to_command = multiprocessing.Queue()
105 command_to_ui = multiprocessing.Queue()
106 command = multiprocessing.Process(name='command engine',
107 target=self.command.run, args=(self, ui_to_command, command_to_ui))
109 return (ui_to_command, command_to_ui, command)
111 def _cleanup_run(self, ui_to_command, command_to_ui, command):
112 ui_to_command.put(ui.CloseEngine())
117 p = optparse.OptionParser()
119 '-s', '--script', dest='script', metavar='FILE',
120 help='Script of command line Hooke commands to run.')
122 '-c', '--command', dest='commands', metavar='COMMAND',
123 action='append', default=[],
124 help='Add a command line Hooke command to run.')
125 options,arguments = p.parse_args()
126 if len(arguments) > 0:
127 print >> sys.stderr, 'Too many arguments to %s: %d > 0' \
128 % (sys.argv[0], len(arguments))
129 print >> sys.stderr, p.help()
132 app = Hooke(debug=__debug__)
134 if options.script != None:
135 f = open(os.path.expanduser(options.script), 'r')
136 options.commands.extend(f.readlines())
138 if len(options.commands) > 0:
139 app.run_lines(options.commands)
147 if __name__ == '__main__':