Updated copyright blurbs in all files to '# Copyright'
[hooke.git] / hooke / hooke.py
1 # Copyright
2
3 """Hooke - A force spectroscopy review & analysis tool.
4 """
5
6 import multiprocessing
7 import optparse
8 import os.path
9 import unittest
10 import sys
11
12 from . import engine as engine
13 from . import config as config_mod
14 from . import playlist as playlist
15 from . import plugin as plugin_mod
16 from . import driver as driver_mod
17 from . import ui as ui
18
19
20 class Hooke (object):
21     def __init__(self, config=None, debug=0):
22         self.debug = debug
23         default_settings = (config_mod.DEFAULT_SETTINGS
24                             + plugin_mod.default_settings()
25                             + driver_mod.default_settings()
26                             + ui.default_settings())
27         if config == None:
28             config = config_mod.HookeConfigParser(
29                 paths=config_mod.DEFAULT_PATHS,
30                 default_settings=default_settings)
31             config.read()
32         self.config = config
33         self.load_plugins()
34         self.load_drivers()
35         self.load_ui()
36         self.command = engine.CommandEngine()
37
38         self.playlists = playlist.NoteIndexList()
39
40     def load_plugins(self):
41         self.plugins = plugin_mod.load_graph(
42             plugin_mod.PLUGIN_GRAPH, self.config, include_section='plugins')
43         self.commands = []
44         for plugin in self.plugins:
45             self.commands.extend(plugin.commands())
46
47     def load_drivers(self):
48         self.drivers = plugin_mod.load_graph(
49             driver_mod.DRIVER_GRAPH, self.config, include_section='drivers')
50
51     def load_ui(self):
52         self.ui = ui.load_ui(self.config)
53
54     def close(self):
55         self.config.write() # Does not preserve original comments
56
57     def run(self):
58         """Run Hooke's main execution loop.
59
60         Spawns a :class:`hooke.engine.CommandEngine` subprocess and
61         then runs the UI, rejoining the `CommandEngine` process after
62         the UI exits.
63         """
64         ui_to_command = multiprocessing.Queue()
65         command_to_ui = multiprocessing.Queue()
66         command = multiprocessing.Process(
67             target=self.command.run, args=(self, ui_to_command, command_to_ui))
68         command.start()
69         try:
70             self.ui.run(self.commands, ui_to_command, command_to_ui)
71         finally:
72             ui_to_command.put(ui.CloseEngine())
73             command.join()
74
75     def run_lines(self, lines):
76         """Run the pre-set commands `lines` with the "command line" UI.
77
78         Allows for non-interactive sessions that are otherwise
79         equivalent to :meth:'.run'.
80         """
81         cmdline = ui.load_ui(self.config, 'command line')
82         ui_to_command = multiprocessing.Queue()
83         command_to_ui = multiprocessing.Queue()
84         command = multiprocessing.Process(
85             target=self.command.run, args=(self, ui_to_command, command_to_ui))
86         command.start()
87         try:
88             cmdline.run_lines(
89                 self.commands, ui_to_command, command_to_ui, lines)
90         finally:
91             ui_to_command.put(ui.CloseEngine())
92             command.join()
93
94 def main():
95     p = optparse.OptionParser()
96     p.add_option(
97         '-s', '--script', dest='script', metavar='FILE',
98         help='Script of command line Hooke commands to run.')
99     p.add_option(
100         '-c', '--command', dest='commands', metavar='COMMAND',
101         action='append', default=[],
102         help='Add a command line Hooke command to run.')
103     options,arguments = p.parse_args()
104     if len(arguments) > 0:
105         print >> sys.stderr, 'Too many arguments to %s: %d > 0' \
106             % (sys.argv[0], len(arguments))
107         print >> sys.stderr, p.help()
108         sys.exit(1)
109
110     app = Hooke(debug=__debug__)
111
112     if options.script != None:
113         f = open(os.path.expanduser(options.script), 'r')
114         options.commands.extend(f.readlines())
115         f.close
116     if len(options.commands) > 0:
117         app.run_lines(options.commands)
118         sys.exit(0)
119
120     try:
121         app.run()
122     finally:
123         app.close()
124
125 if __name__ == '__main__':
126     main()