d70bd41425334c4c0b75bac1ff9ccde3fe03f7f3
[hooke.git] / hooke / ui / __init__.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 """The `ui` module provides :class:`UserInterface` and various subclasses.
20 """
21
22 import ConfigParser as configparser
23
24 from .. import version
25 from ..license import short_license
26 from ..config import Setting
27 from ..util.pluggable import IsSubclass, construct_odict
28
29
30 USER_INTERFACE_MODULES = [
31     'commandline',
32     'gui',
33     ]
34 """List of user interface modules.  TODO: autodiscovery
35 """
36
37 USER_INTERFACE_SETTING_SECTION = 'user interfaces'
38 """Name of the config section which controls UI selection.
39 """
40
41
42 class QueueMessage (object):
43     def __str__(self):
44         return self.__class__.__name__
45
46 class CloseEngine (QueueMessage):
47     pass
48
49 class CommandMessage (QueueMessage):
50     """A message storing a command to run, `command` should be a
51     :class:`hooke.command.Command` instance, and `arguments` should be
52     a :class:`dict` with `argname` keys and `value` values to be
53     passed to the command.
54     """
55     def __init__(self, command, arguments=None):
56         self.command = command
57         if arguments == None:
58             arguments = {}
59         self.arguments = arguments
60
61 class UserInterface (object):
62     """A user interface to drive the :class:`hooke.engine.CommandEngine`.
63     """
64     def __init__(self, name):
65         self.name = name
66         self.setting_section = '%s user interface' % (self.name)
67         self.config = {}
68
69     def default_settings(self):
70         """Return a list of :class:`hooke.config.Setting`\s for any
71         configurable UI settings.
72
73         The suggested section setting is::
74
75             Setting(section=self.setting_section, help=self.__doc__)
76         """
77         return []
78
79     def reload_config(self, config):
80         """Update the user interface for new config settings.
81
82         Should be called with the new `config` upon recipt of
83         `ReloadUserInterfaceConfig` from the `CommandEngine` or when
84         loading the initial configuration.
85         """
86         try:
87             self.config = dict(config.items(self.setting_section))
88         except configparser.NoSectionError:
89             self.config = {}
90
91     def run(self, commands, ui_to_command_queue, command_to_ui_queue):
92         return
93
94     # Assorted useful tidbits for subclasses
95
96     def _splash_text(self, extra_info, **kwargs):
97         return ("""
98 Hooke version %s
99
100 %s
101 ----
102 """ % (version(), short_license(extra_info, **kwargs))).strip()
103
104     def _playlist_status(self, playlist):
105         if len(playlist) > 0:
106             return '%s (%s/%s)' % (playlist.name, playlist.index() + 1,
107                                    len(playlist))
108         return 'The playlist %s does not contain any valid force curve data.' \
109             % self.name
110
111
112 USER_INTERFACES = construct_odict(
113     this_modname=__name__,
114     submodnames=USER_INTERFACE_MODULES,
115     class_selector=IsSubclass(UserInterface, blacklist=[UserInterface]))
116 """:class:`hooke.compat.odict.odict` of :class:`UserInterface`
117 instances keyed by `.name`.
118 """
119
120 def default_settings():
121     settings = [Setting(USER_INTERFACE_SETTING_SECTION,
122                         help='Select the user interface (only one).')]
123     for i,ui in enumerate(USER_INTERFACES.values()):
124         help = ui.__doc__.split('\n', 1)[0]
125         settings.append(Setting(USER_INTERFACE_SETTING_SECTION,
126                                 ui.name, str(i==0), help=help))
127         # i==0 to enable the first by default
128     for ui in USER_INTERFACES.values():
129         settings.extend(ui.default_settings())
130     return settings
131
132 def load_ui(config, name=None):
133     if name == None:
134         uis = [c for c,v in config.items(USER_INTERFACE_SETTING_SECTION) if v == 'True']
135         assert len(uis) == 1, 'Can only select one UI, not %d: %s' % (len(uis),uis)
136         name = uis[0]
137     ui = USER_INTERFACES[name]
138     ui.reload_config(config)
139     return ui