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