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