Finished off hooke.hooke and fleshed out hooke.ui.
[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 plugin as plugin_mod
16 from . import driver as driver_mod
17 from . import ui as ui
18
19 class Hooke (object):
20     def __init__(self, config=None, debug=0):
21         self.debug = debug
22         default_settings = (config_mod.DEFAULT_SETTINGS
23                             + plugin_mod.default_settings()
24                             + driver_mod.default_settings()
25                             + ui.default_settings())
26         if config == None:
27             config = config_mod.HookeConfigParser(
28                 paths=config_mod.DEFAULT_PATHS,
29                 default_settings=default_settings)
30             config.read()
31         self.config = config
32         self.load_plugins()
33         self.load_drivers()
34         self.load_ui()
35         self.command = engine.CommandEngine()
36
37     def load_plugins(self):
38         self.plugins = plugin_mod.load_graph(
39             plugin_mod.PLUGIN_GRAPH, self.config, include_section='plugins')
40         self.commands = []
41         for plugin in self.plugins:
42             self.commands.extend(plugin.commands())
43
44     def load_drivers(self):
45         self.drivers = plugin_mod.load_graph(
46             driver_mod.DRIVER_GRAPH, self.config, include_section='drivers')
47
48     def load_ui(self):
49         self.ui = ui.load_ui(self.config)
50
51     def close(self):
52         self.config.write() # Does not preserve original comments
53
54     def run(self):
55         """Run Hooke's main execution loop.
56
57         Spawns a :class:`hooke.engine.CommandEngine` subprocess and
58         then runs the UI, rejoining the `CommandEngine` process after
59         the UI exits.
60         """
61         ui_to_command = multiprocessing.Queue()
62         command_to_ui = multiprocessing.Queue()
63         command = multiprocessing.Process(
64             target=self.command.run, args=(ui_to_command, command_to_ui))
65         command.start()
66         try:
67             self.ui.run(self, ui_to_command, command_to_ui)
68         finally:
69             ui_to_command.put(ui.CloseEngine())
70             command.join()
71
72 def main():
73     app = Hooke(debug=__debug__)
74     try:
75         app.run()
76     finally:
77         app.close()
78
79 if __name__ == '__main__':
80     main()