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 ..config import Setting
26 from ..util.pluggable import IsSubclass, construct_odict
29 from ..license import short_license
30 except ImportError, e:
32 logging.warn('Could not load short_license from hooke.license')
33 from .. import __license__
34 def short_license(extra_info, **kwargs):
38 USER_INTERFACE_MODULES = [
42 """List of user interface modules. TODO: autodiscovery
45 USER_INTERFACE_SETTING_SECTION = 'user interfaces'
46 """Name of the config section which controls UI selection.
50 class UserInterface (object):
51 """A user interface to drive the :class:`hooke.engine.CommandEngine`.
53 def __init__(self, name):
55 self.setting_section = '%s user interface' % (self.name)
58 def default_settings(self):
59 """Return a list of :class:`hooke.config.Setting`\s for any
60 configurable UI settings.
62 The suggested section setting is::
64 Setting(section=self.setting_section, help=self.__doc__)
68 def reload_config(self, config):
69 """Update the user interface for new config settings.
71 Should be called with the new `config` upon recipt of
72 `ReloadUserInterfaceConfig` from the `CommandEngine` or when
73 loading the initial configuration.
76 self.config = dict(config.items(self.setting_section))
77 except configparser.NoSectionError:
80 def run(self, commands, ui_to_command_queue, command_to_ui_queue):
83 # Assorted useful tidbits for subclasses
85 def _submit_command(self, command_message, ui_to_command_queue):
86 log = logging.getLogger('hooke')
87 log.debug('executing %s' % command_message)
88 ui_to_command_queue.put(command_message)
90 def _splash_text(self, extra_info, **kwargs):
96 """ % (version(), short_license(extra_info, **kwargs))).strip()
98 def _playlist_status(self, playlist):
100 return '%s (%s/%s)' % (playlist.name, playlist.index() + 1,
102 return 'The playlist %s does not contain any valid force curve data.' \
106 USER_INTERFACES = construct_odict(
107 this_modname=__name__,
108 submodnames=USER_INTERFACE_MODULES,
109 class_selector=IsSubclass(UserInterface, blacklist=[UserInterface]))
110 """:class:`hooke.compat.odict.odict` of :class:`UserInterface`
111 instances keyed by `.name`.
114 def default_settings():
115 settings = [Setting(USER_INTERFACE_SETTING_SECTION,
116 help='Select the user interface (only one).')]
117 for i,ui in enumerate(USER_INTERFACES.values()):
118 help = ui.__doc__.split('\n', 1)[0]
119 settings.append(Setting(USER_INTERFACE_SETTING_SECTION,
120 ui.name, str(i==0), help=help))
121 # i==0 to enable the first by default
122 for ui in USER_INTERFACES.values():
123 settings.extend(ui.default_settings())
126 def load_ui(config, name=None):
128 uis = [c for c,v in config.items(USER_INTERFACE_SETTING_SECTION) if v == 'True']
129 assert len(uis) == 1, 'Can only select one UI, not %d: %s' % (len(uis),uis)
131 ui = USER_INTERFACES[name]
132 ui.reload_config(config)