3 """The `ui` module provides :class:`UserInterface` and various subclasses.
6 import ConfigParser as configparser
9 from ..compat.odict import odict
10 from ..config import Setting
11 from ..util.pluggable import IsSubclass
14 USER_INTERFACE_MODULES = [
18 """List of user interface modules. TODO: autodiscovery
21 USER_INTERFACE_SETTING_SECTION = 'user interfaces'
22 """Name of the config section which controls UI selection.
26 class QueueMessage (object):
28 return self.__class__.__name__
30 class CloseEngine (QueueMessage):
33 class CommandMessage (QueueMessage):
34 """A message storing a command to run, `command` should be a
35 :class:`hooke.command.Command` instance, and `arguments` should be
36 a :class:`dict` with `argname` keys and `value` values to be
37 passed to the command.
39 def __init__(self, command, arguments):
40 self.command = command
41 self.arguments = arguments
43 class UserInterface (object):
44 """A user interface to drive the :class:`hooke.engine.CommandEngine`.
46 def __init__(self, name):
48 self.setting_section = '%s user interface' % (self.name)
51 def default_settings(self):
52 """Return a list of :class:`hooke.config.Setting`\s for any
53 configurable UI settings.
55 The suggested section setting is::
57 Setting(section=self.setting_section, help=self.__doc__)
61 def reload_config(self, config):
62 """Update the user interface for new config settings.
64 Should be called with the new `config` upon recipt of
65 `ReloadUserInterfaceConfig` from the `CommandEngine`.
68 self.config = dict(config.items(self.setting_section))
69 except configparser.NoSectionError:
72 def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
75 # Assorted useful tidbits for subclasses
77 def _splash_text(self):
83 """ % version()).strip()
85 def _playlist_status(self, playlist):
87 return '%s (%s/%s)' % (playlist.name, playlist._index + 1,
89 return 'The playlist %s does not contain any valid force curve data.' \
92 def construct_odict(this_modname, submodnames, class_selector):
93 """Search the submodules `submodnames` of a module `this_modname`
94 for class objects for which `class_selector(class)` returns
95 `True`. These classes are instantiated and stored in the returned
96 :class:`hooke.compat.odict.odict` in the order in which they were
100 for submodname in submodnames:
101 count = len([s for s in submodnames if s == submodname])
102 assert count > 0, 'No %s entries: %s' % (submodname, submodnames)
103 assert count == 1, 'Multiple (%d) %s entries: %s' \
104 % (count, submodname, submodnames)
105 this_mod = __import__(this_modname, fromlist=[submodname])
106 submod = getattr(this_mod, submodname)
107 for objname in dir(submod):
108 obj = getattr(submod, objname)
109 if class_selector(obj):
111 instances[instance.name] = instance
114 USER_INTERFACES = construct_odict(
115 this_modname=__name__,
116 submodnames=USER_INTERFACE_MODULES,
117 class_selector=IsSubclass(UserInterface, blacklist=[UserInterface]))
118 """:class:`hooke.compat.odict.odict` of :class:`UserInterface`
119 instances keyed by `.name`.
122 def default_settings():
123 settings = [Setting(USER_INTERFACE_SETTING_SECTION,
124 help='Select the user interface (only one).')]
125 for i,ui in enumerate(USER_INTERFACES.values()):
126 help = ui.__doc__.split('\n', 1)[0]
127 settings.append(Setting(USER_INTERFACE_SETTING_SECTION,
128 ui.name, str(i==0), help=help))
129 # i==0 to enable the first by default
130 for ui in USER_INTERFACES.values():
131 settings.extend(ui.default_settings())
134 def load_ui(config, name=None):
136 uis = [c for c,v in config.items(USER_INTERFACE_SETTING_SECTION) if v == 'True']
137 assert len(uis) == 1, 'Can only select one UI, not %d: %s' % (len(uis),uis)
139 ui = USER_INTERFACES[name]
141 ui.config = dict(config.items(ui.setting_section))
142 except configparser.NoSectionError: