Change CommandMessage.command from Command instance to command's name.
[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 ..config import Setting
26 from ..util.pluggable import IsSubclass, construct_odict
27
28 try:
29     from ..license import short_license
30 except ImportError, e:
31     import logging
32     logging.warn('Could not load short_license from hooke.license')
33     from .. import __license__
34     def short_license(extra_info, **kwargs):
35         return __license__
36
37
38 USER_INTERFACE_MODULES = [
39     'commandline',
40     'gui',
41     ]
42 """List of user interface modules.  TODO: autodiscovery
43 """
44
45 USER_INTERFACE_SETTING_SECTION = 'user interfaces'
46 """Name of the config section which controls UI selection.
47 """
48
49
50 class UserInterface (object):
51     """A user interface to drive the :class:`hooke.engine.CommandEngine`.
52     """
53     def __init__(self, name):
54         self.name = name
55         self.setting_section = '%s user interface' % (self.name)
56         self.config = {}
57
58     def default_settings(self):
59         """Return a list of :class:`hooke.config.Setting`\s for any
60         configurable UI settings.
61
62         The suggested section setting is::
63
64             Setting(section=self.setting_section, help=self.__doc__)
65         """
66         return []
67
68     def reload_config(self, config):
69         """Update the user interface for new config settings.
70
71         Should be called with the new `config` upon recipt of
72         `ReloadUserInterfaceConfig` from the `CommandEngine` or when
73         loading the initial configuration.
74         """
75         try:
76             self.config = dict(config.items(self.setting_section))
77         except configparser.NoSectionError:
78             self.config = {}
79
80     def run(self, commands, ui_to_command_queue, command_to_ui_queue):
81         return
82
83     # Assorted useful tidbits for subclasses
84
85     def _splash_text(self, extra_info, **kwargs):
86         return ("""
87 Hooke version %s
88
89 %s
90 ----
91 """ % (version(), short_license(extra_info, **kwargs))).strip()
92
93     def _playlist_status(self, playlist):
94         if len(playlist) > 0:
95             return '%s (%s/%s)' % (playlist.name, playlist.index() + 1,
96                                    len(playlist))
97         return 'The playlist %s does not contain any valid force curve data.' \
98             % self.name
99
100
101 USER_INTERFACES = construct_odict(
102     this_modname=__name__,
103     submodnames=USER_INTERFACE_MODULES,
104     class_selector=IsSubclass(UserInterface, blacklist=[UserInterface]))
105 """:class:`hooke.compat.odict.odict` of :class:`UserInterface`
106 instances keyed by `.name`.
107 """
108
109 def default_settings():
110     settings = [Setting(USER_INTERFACE_SETTING_SECTION,
111                         help='Select the user interface (only one).')]
112     for i,ui in enumerate(USER_INTERFACES.values()):
113         help = ui.__doc__.split('\n', 1)[0]
114         settings.append(Setting(USER_INTERFACE_SETTING_SECTION,
115                                 ui.name, str(i==0), help=help))
116         # i==0 to enable the first by default
117     for ui in USER_INTERFACES.values():
118         settings.extend(ui.default_settings())
119     return settings
120
121 def load_ui(config, name=None):
122     if name == None:
123         uis = [c for c,v in config.items(USER_INTERFACE_SETTING_SECTION) if v == 'True']
124         assert len(uis) == 1, 'Can only select one UI, not %d: %s' % (len(uis),uis)
125         name = uis[0]
126     ui = USER_INTERFACES[name]
127     ui.reload_config(config)
128     return ui