72fab311d59bd49f50c0ae682e7a4eaf29189e1d
[hooke.git] / hooke / hooke.py
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>
5 #
6 # This file is part of Hooke.
7 #
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.
12 #
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.
17 #
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/>.
21
22 """Hooke - A force spectroscopy review & analysis tool.
23 """
24
25 import multiprocessing
26 import optparse
27 import os.path
28 import unittest
29 import sys
30
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
37
38
39 class Hooke (object):
40     def __init__(self, config=None, debug=0):
41         self.debug = debug
42         default_settings = (config_mod.DEFAULT_SETTINGS
43                             + plugin_mod.default_settings()
44                             + driver_mod.default_settings()
45                             + ui.default_settings())
46         if config == None:
47             config = config_mod.HookeConfigParser(
48                 paths=config_mod.DEFAULT_PATHS,
49                 default_settings=default_settings)
50             config.read()
51         self.config = config
52         self.load_plugins()
53         self.load_drivers()
54         self.load_ui()
55         self.command = engine.CommandEngine()
56
57         self.playlists = playlist.NoteIndexList()
58
59     def load_plugins(self):
60         self.plugins = plugin_mod.load_graph(
61             plugin_mod.PLUGIN_GRAPH, self.config, include_section='plugins')
62         self.commands = []
63         for plugin in self.plugins:
64             self.commands.extend(plugin.commands())
65
66     def load_drivers(self):
67         self.drivers = plugin_mod.load_graph(
68             driver_mod.DRIVER_GRAPH, self.config, include_section='drivers')
69
70     def load_ui(self):
71         self.ui = ui.load_ui(self.config)
72
73     def close(self):
74         self.config.write() # Does not preserve original comments
75
76     def run(self):
77         """Run Hooke's main execution loop.
78
79         Spawns a :class:`hooke.engine.CommandEngine` subprocess and
80         then runs the UI, rejoining the `CommandEngine` process after
81         the UI exits.
82         """
83         ui_to_command,command_to_ui,command = self._setup_run()
84         try:
85             self.ui.run(self.commands, ui_to_command, command_to_ui)
86         finally:
87             self._cleanup_command(ui_to_command, command_to_ui, command)
88
89     def run_lines(self, lines):
90         """Run the pre-set commands `lines` with the "command line" UI.
91
92         Allows for non-interactive sessions that are otherwise
93         equivalent to :meth:'.run'.
94         """
95         cmdline = ui.load_ui(self.config, 'command line')
96         ui_to_command,command_to_ui,command = self._setup_run()
97         try:
98             cmdline.run_lines(
99                 self.commands, ui_to_command, command_to_ui, lines)
100         finally:
101             self._cleanup_command(ui_to_command, command_to_ui, command)
102
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))
108         command.start()
109         return (ui_to_command, command_to_ui, command)
110
111     def _cleanup_run(self, ui_to_command, command_to_ui, command):
112         ui_to_command.put(ui.CloseEngine())
113         command.join()
114
115
116 def main():
117     p = optparse.OptionParser()
118     p.add_option(
119         '-s', '--script', dest='script', metavar='FILE',
120         help='Script of command line Hooke commands to run.')
121     p.add_option(
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()
130         sys.exit(1)
131
132     app = Hooke(debug=__debug__)
133
134     if options.script != None:
135         f = open(os.path.expanduser(options.script), 'r')
136         options.commands.extend(f.readlines())
137         f.close
138     if len(options.commands) > 0:
139         app.run_lines(options.commands)
140         sys.exit(0)
141
142     try:
143         app.run()
144     finally:
145         app.close()
146
147 if __name__ == '__main__':
148     main()