1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 # This file is part of Hooke.
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.
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.
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/>.
19 """The `ui` module provides :class:`UserInterface` and various subclasses.
22 import ConfigParser as configparser
24 from .. import version
25 from ..license import short_license
26 from ..config import Setting
27 from ..util.pluggable import IsSubclass, construct_odict
30 USER_INTERFACE_MODULES = [
34 """List of user interface modules. TODO: autodiscovery
37 USER_INTERFACE_SETTING_SECTION = 'user interfaces'
38 """Name of the config section which controls UI selection.
42 class QueueMessage (object):
44 return self.__class__.__name__
46 class CloseEngine (QueueMessage):
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.
55 def __init__(self, command, arguments=None):
56 self.command = command
59 self.arguments = arguments
61 class UserInterface (object):
62 """A user interface to drive the :class:`hooke.engine.CommandEngine`.
64 def __init__(self, name):
66 self.setting_section = '%s user interface' % (self.name)
69 def default_settings(self):
70 """Return a list of :class:`hooke.config.Setting`\s for any
71 configurable UI settings.
73 The suggested section setting is::
75 Setting(section=self.setting_section, help=self.__doc__)
79 def reload_config(self, config):
80 """Update the user interface for new config settings.
82 Should be called with the new `config` upon recipt of
83 `ReloadUserInterfaceConfig` from the `CommandEngine` or when
84 loading the initial configuration.
87 self.config = dict(config.items(self.setting_section))
88 except configparser.NoSectionError:
91 def run(self, commands, ui_to_command_queue, command_to_ui_queue):
94 # Assorted useful tidbits for subclasses
96 def _splash_text(self, extra_info, **kwargs):
102 """ % (version(), short_license(extra_info, **kwargs))).strip()
104 def _playlist_status(self, playlist):
105 if len(playlist) > 0:
106 return '%s (%s/%s)' % (playlist.name, playlist._index + 1,
108 return 'The playlist %s does not contain any valid force curve data.' \
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`.
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())
132 def load_ui(config, 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)
137 ui = USER_INTERFACES[name]
138 ui.reload_config(config)