X-Git-Url: http://git.tremily.us/?p=hooke.git;a=blobdiff_plain;f=hooke%2Fui%2F__init__.py;h=1b939144b6f2c1c1f693bca8088891507661e982;hp=f46e8937e54d5d3c7cf2dd18b23ed1481cabce3a;hb=45c5d07228fbe9cefd209012849faa86dd7a020f;hpb=1cb1c2ffd2f83b2cb4ae893174b2fad87d2e0d29 diff --git a/hooke/ui/__init__.py b/hooke/ui/__init__.py index f46e893..1b93914 100644 --- a/hooke/ui/__init__.py +++ b/hooke/ui/__init__.py @@ -1,46 +1,61 @@ +# Copyright (C) 2010 W. Trevor King +# +# This file is part of Hooke. +# +# Hooke is free software: you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Hooke is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General +# Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with Hooke. If not, see +# . + """The `ui` module provides :class:`UserInterface` and various subclasses. """ import ConfigParser as configparser from .. import version -from ..compat.odict import odict from ..config import Setting -from ..plugin import IsSubclass +from ..engine import CloseEngine, CommandMessage +from ..util.pluggable import IsSubclass, construct_odict + +try: + from ..license import short_license +except ImportError, e: + import logging + logging.warn('Could not load short_license from hooke.license') + from .. import __license__ + def short_license(extra_info, **kwargs): + return __license__ USER_INTERFACE_MODULES = [ 'commandline', - #'gui', + 'gui', ] """List of user interface modules. TODO: autodiscovery """ -class QueueMessage (object): - def __str__(self): - return self.__class__.__name__ - -class CloseEngine (QueueMessage): - pass +USER_INTERFACE_SETTING_SECTION = 'user interfaces' +"""Name of the config section which controls UI selection. +""" -class CommandMessage (QueueMessage): - """A message storing a command to run, `command` should be a - :class:`hooke.command.Command` instance, and `arguments` should be - a :class:`dict` with `argname` keys and `value` values to be - passed to the command. - """ - def __init__(self, command, arguments): - self.command = command - self.arguments = arguments class UserInterface (object): """A user interface to drive the :class:`hooke.engine.CommandEngine`. """ def __init__(self, name): self.name = name - self.setting_section = '%s ui' % self.name + self.setting_section = '%s user interface' % (self.name) self.config = {} - + def default_settings(self): """Return a list of :class:`hooke.config.Setting`\s for any configurable UI settings. @@ -51,47 +66,38 @@ class UserInterface (object): """ return [] - def run(self, hooke, ui_to_command_queue, command_to_ui_queue): + def reload_config(self, config): + """Update the user interface for new config settings. + + Should be called with the new `config` upon recipt of + `ReloadUserInterfaceConfig` from the `CommandEngine` or when + loading the initial configuration. + """ + try: + self.config = dict(config.items(self.setting_section)) + except configparser.NoSectionError: + self.config = {} + + def run(self, commands, ui_to_command_queue, command_to_ui_queue): return # Assorted useful tidbits for subclasses - def _splash_text(self): + def _splash_text(self, extra_info, **kwargs): return (""" -This is Hooke, version %s +Hooke version %s -COPYRIGHT +%s ---- -""" % version()).strip() +""" % (version(), short_license(extra_info, **kwargs))).strip() def _playlist_status(self, playlist): - if playlist.has_curves(): - return '%s (%s/%s)' % (playlist.name, playlist._index + 1, + if len(playlist) > 0: + return '%s (%s/%s)' % (playlist.name, playlist.index() + 1, len(playlist)) return 'The playlist %s does not contain any valid force curve data.' \ % self.name -def construct_odict(this_modname, submodnames, class_selector): - """Search the submodules `submodnames` of a module `this_modname` - for class objects for which `class_selector(class)` returns - `True`. These classes are instantiated and stored in the returned - :class:`hooke.compat.odict.odict` in the order in which they were - discovered. - """ - instances = odict() - for submodname in submodnames: - count = len([s for s in submodnames if s == submodname]) - assert count > 0, 'No %s entries: %s' % (submodname, submodnames) - assert count == 1, 'Multiple (%d) %s entries: %s' \ - % (count, submodname, submodnames) - this_mod = __import__(this_modname, fromlist=[submodname]) - submod = getattr(this_mod, submodname) - for objname in dir(submod): - obj = getattr(submod, objname) - if class_selector(obj): - instance = obj() - instances[instance.name] = instance - return instances USER_INTERFACES = construct_odict( this_modname=__name__, @@ -102,22 +108,22 @@ instances keyed by `.name`. """ def default_settings(): - settings = [Setting('UIs', help='Select the user interface (only one).')] + settings = [Setting(USER_INTERFACE_SETTING_SECTION, + help='Select the user interface (only one).')] for i,ui in enumerate(USER_INTERFACES.values()): help = ui.__doc__.split('\n', 1)[0] - settings.append(Setting('UIs', ui.name, str(i==0), help=help)) + settings.append(Setting(USER_INTERFACE_SETTING_SECTION, + ui.name, str(i==0), help=help)) # i==0 to enable the first by default for ui in USER_INTERFACES.values(): settings.extend(ui.default_settings()) return settings -def load_ui(config): - uis = [c for c,v in config.items('UIs') if v == 'True'] - assert len(uis) == 1, 'Can only select one UI, not %d: %s' % (len(uis),uis) - ui_name = uis[0] - ui = USER_INTERFACES[ui_name] - try: - ui.config = dict(config.items(ui.setting_section)) - except configparser.NoSectionError: - pass +def load_ui(config, name=None): + if name == None: + uis = [c for c,v in config.items(USER_INTERFACE_SETTING_SECTION) if v == 'True'] + assert len(uis) == 1, 'Can only select one UI, not %d: %s' % (len(uis),uis) + name = uis[0] + ui = USER_INTERFACES[name] + ui.reload_config(config) return ui