Pass Hooke instance to CommandEngine.run(), not UserInterface.run().
[hooke.git] / hooke / hooke.py
1 #!/usr/bin/env python
2
3 '''
4 Hooke - A force spectroscopy review & analysis tool.
5
6 A free, open source data analysis platform
7
8 COPYRIGHT
9 '''
10
11 import multiprocessing
12
13 from . import engine as engine
14 from . import config as config_mod
15 from . import playlist as playlist
16 from . import plugin as plugin_mod
17 from . import driver as driver_mod
18 from . import ui as ui
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.PlaylistManager()
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 main():
76     app = Hooke(debug=__debug__)
77     try:
78         app.run()
79     finally:
80         app.close()
81
82 if __name__ == '__main__':
83     main()