Ran update_copyright.py, updating all the copyright blurbs and adding AUTHORS.
[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 = multiprocessing.Queue()
84         command_to_ui = multiprocessing.Queue()
85         command = multiprocessing.Process(
86             target=self.command.run, args=(self, ui_to_command, command_to_ui))
87         command.start()
88         try:
89             self.ui.run(self.commands, ui_to_command, command_to_ui)
90         finally:
91             ui_to_command.put(ui.CloseEngine())
92             command.join()
93
94     def run_lines(self, lines):
95         """Run the pre-set commands `lines` with the "command line" UI.
96
97         Allows for non-interactive sessions that are otherwise
98         equivalent to :meth:'.run'.
99         """
100         cmdline = ui.load_ui(self.config, 'command line')
101         ui_to_command = multiprocessing.Queue()
102         command_to_ui = multiprocessing.Queue()
103         command = multiprocessing.Process(
104             target=self.command.run, args=(self, ui_to_command, command_to_ui))
105         command.start()
106         try:
107             cmdline.run_lines(
108                 self.commands, ui_to_command, command_to_ui, lines)
109         finally:
110             ui_to_command.put(ui.CloseEngine())
111             command.join()
112
113 def main():
114     p = optparse.OptionParser()
115     p.add_option(
116         '-s', '--script', dest='script', metavar='FILE',
117         help='Script of command line Hooke commands to run.')
118     p.add_option(
119         '-c', '--command', dest='commands', metavar='COMMAND',
120         action='append', default=[],
121         help='Add a command line Hooke command to run.')
122     options,arguments = p.parse_args()
123     if len(arguments) > 0:
124         print >> sys.stderr, 'Too many arguments to %s: %d > 0' \
125             % (sys.argv[0], len(arguments))
126         print >> sys.stderr, p.help()
127         sys.exit(1)
128
129     app = Hooke(debug=__debug__)
130
131     if options.script != None:
132         f = open(os.path.expanduser(options.script), 'r')
133         options.commands.extend(f.readlines())
134         f.close
135     if len(options.commands) > 0:
136         app.run_lines(options.commands)
137         sys.exit(0)
138
139     try:
140         app.run()
141     finally:
142         app.close()
143
144 if __name__ == '__main__':
145     main()