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 modify it
9 # under the terms of the GNU Lesser General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
13 # Hooke is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
16 # 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 if False: # Queue pickle error debugging code
26 """The Hooke class is passed back from the CommandEngine process
27 to the main process via a :class:`multiprocessing.queues.Queue`,
28 which uses :mod:`pickle` for serialization. There are a number of
29 objects that are unpicklable, and the error messages are not
30 always helpful. This block of code hooks you into the Queue's
31 _feed method so you can print out useful tidbits to help find the
32 particular object that is gumming up the pickle works.
34 import multiprocessing.queues
36 feed = multiprocessing.queues.Queue._feed
37 def new_feed (buffer, notempty, send, writelock, close):
39 print 'SEND:', obj, dir(obj)
41 attr = getattr(obj, a)
42 #print ' ', a, attr, type(attr)
43 if obj.__class__.__name__ == 'Hooke':
44 # Set suspect attributes to None until you resolve the
45 # PicklingError. Then fix whatever is breaking the
54 feed(buffer, notempty, s, writelock, close)
55 multiprocessing.queues.Queue._feed = staticmethod(new_feed)
57 from ConfigParser import NoSectionError
60 import multiprocessing
70 from . import config as config_mod
71 from . import playlist
72 from . import plugin as plugin_mod
73 from . import driver as driver_mod
78 def __init__(self, config=None, debug=0):
80 default_settings = (config_mod.DEFAULT_SETTINGS
81 + plugin_mod.default_settings()
82 + driver_mod.default_settings()
83 + ui.default_settings())
85 config = config_mod.HookeConfigParser(
86 paths=config_mod.DEFAULT_PATHS,
87 default_settings=default_settings)
94 self.engine = engine.CommandEngine()
95 self.playlists = playlist.Playlists()
98 config_file = StringIO.StringIO()
99 self.config.write(config_file)
100 logging.config.fileConfig(StringIO.StringIO(config_file.getvalue()))
101 # Don't attach the logger because it contains an unpicklable
102 # thread.lock. Instead, grab it directly every time you need it.
103 #self.log = logging.getLogger('hooke')
105 def load_plugins(self):
106 self.plugins = plugin_mod.load_graph(
107 plugin_mod.PLUGIN_GRAPH, self.config, include_section='plugins')
108 self.configure_plugins()
110 for plugin in self.plugins:
111 self.commands.extend(plugin.commands())
112 self.command_by_name = dict(
113 [(c.name, c) for c in self.commands])
115 def load_drivers(self):
116 self.drivers = plugin_mod.load_graph(
117 driver_mod.DRIVER_GRAPH, self.config, include_section='drivers')
118 self.configure_drivers()
121 self.ui = ui.load_ui(self.config)
124 def configure_plugins(self):
125 for plugin in self.plugins:
126 self._configure_item(plugin)
128 def configure_drivers(self):
129 for driver in self.drivers:
130 self._configure_item(driver)
132 def configure_ui(self):
133 self._configure_item(self.ui)
135 def _configure_item(self, item):
136 conditions = self.config.items('conditions')
138 item.config = dict(self.config.items(item.setting_section))
139 except NoSectionError:
141 for key,value in conditions:
142 if key not in item.config:
143 item.config[key] = value
145 def close(self, save_config=False):
146 if save_config == True:
147 self.config.write() # Does not preserve original comments
149 def run_command(self, command, arguments):
150 """Run the command named `command` with `arguments` using
151 :meth:`~hooke.engine.CommandEngine.run_command`.
153 Allows for running commands without spawning another process
154 as in :class:`HookeRunner`.
156 self.engine.run_command(self, command, arguments)
159 class HookeRunner (object):
160 def run(self, hooke):
161 """Run Hooke's main execution loop.
163 Spawns a :class:`hooke.engine.CommandEngine` subprocess and
164 then runs the UI, rejoining the `CommandEngine` process after
167 ui_to_command,command_to_ui,command = self._setup_run(hooke)
169 hooke.ui.run(hooke.commands, ui_to_command, command_to_ui)
171 hooke = self._cleanup_run(ui_to_command, command_to_ui, command)
174 def run_lines(self, hooke, lines):
175 """Run the pre-set commands `lines` with the "command line" UI.
177 Allows for non-interactive sessions that are otherwise
178 equivalent to :meth:'.run'.
180 cmdline = ui.load_ui(hooke.config, 'command line')
181 ui_to_command,command_to_ui,command = self._setup_run(hooke)
184 hooke.commands, ui_to_command, command_to_ui, lines)
186 hooke = self._cleanup_run(ui_to_command, command_to_ui, command)
189 def _setup_run(self, hooke):
190 ui_to_command = multiprocessing.Queue()
191 command_to_ui = multiprocessing.Queue()
192 manager = multiprocessing.Manager()
193 command = multiprocessing.Process(name='command engine',
194 target=hooke.engine.run, args=(hooke, ui_to_command, command_to_ui))
196 hooke.engine = None # no more need for the UI-side version.
197 return (ui_to_command, command_to_ui, command)
199 def _cleanup_run(self, ui_to_command, command_to_ui, command):
200 log = logging.getLogger('hooke')
201 log.debug('cleanup sending CloseEngine')
202 ui_to_command.put(engine.CloseEngine())
204 while not isinstance(hooke, Hooke):
205 log.debug('cleanup waiting for Hooke instance from the engine.')
206 hooke = command_to_ui.get(block=True)
207 log.debug('cleanup got %s instance' % type(hooke))
209 for playlist in hooke.playlists: # Curve._hooke is not pickled.
210 for curve in playlist:
211 curve.set_hooke(hooke)
216 p = optparse.OptionParser()
218 '--version', dest='version', default=False, action='store_true',
219 help="Print Hooke's version information and exit.")
221 '-s', '--script', dest='script', metavar='FILE',
222 help='Script of command line Hooke commands to run.')
224 '-c', '--command', dest='commands', metavar='COMMAND',
225 action='append', default=[],
226 help='Add a command line Hooke command to run.')
228 '-p', '--persist', dest='persist', action='store_true', default=False,
229 help="Don't exit after running a script or commands.")
231 '--save-config', dest='save_config',
232 action='store_true', default=False,
233 help="Automatically save a changed configuration on exit.")
235 '--debug', dest='debug', action='store_true', default=False,
236 help="Enable debug logging.")
237 options,arguments = p.parse_args()
238 if len(arguments) > 0:
239 print >> sys.stderr, 'More than 0 arguments to %s: %s' \
240 % (sys.argv[0], arguments)
241 p.print_help(sys.stderr)
244 hooke = Hooke(debug=__debug__)
245 runner = HookeRunner()
247 if options.version == True:
250 if options.debug == True:
252 section='handler_hand1', option='level', value='NOTSET')
254 if options.script != None:
255 with open(os.path.expanduser(options.script), 'r') as f:
256 options.commands.extend(f.readlines())
257 if len(options.commands) > 0:
259 hooke = runner.run_lines(hooke, options.commands)
261 if options.persist == False:
262 hooke.close(save_config=options.save_config)
266 hooke = runner.run(hooke)
268 hooke.close(save_config=options.save_config)