Use *_SETTING_SECTION in hooke.plugin/.driver/.ui for section name
[hooke.git] / hooke / ui / __init__.py
1 """The `ui` module provides :class:`UserInterface` and various subclasses.
2 """
3
4 import ConfigParser as configparser
5
6 from .. import version
7 from ..compat.odict import odict
8 from ..config import Setting
9 from ..plugin import IsSubclass
10
11
12 USER_INTERFACE_MODULES = [
13     'commandline',
14     #'gui',
15     ]
16 """List of user interface modules.  TODO: autodiscovery
17 """
18
19 USER_INTERFACE_SETTING_SECTION = 'user interfaces'
20 """Name of the config section which controls UI selection.
21 """
22
23 class QueueMessage (object):
24     def __str__(self):
25         return self.__class__.__name__
26
27 class CloseEngine (QueueMessage):
28     pass
29
30 class CommandMessage (QueueMessage):
31     """A message storing a command to run, `command` should be a
32     :class:`hooke.command.Command` instance, and `arguments` should be
33     a :class:`dict` with `argname` keys and `value` values to be
34     passed to the command.
35     """
36     def __init__(self, command, arguments):
37         self.command = command
38         self.arguments = arguments
39
40 class UserInterface (object):
41     """A user interface to drive the :class:`hooke.engine.CommandEngine`.
42     """
43     def __init__(self, name):
44         self.name = name
45         self.setting_section = '%s user interface' % self.name
46         self.config = {}
47     
48     def default_settings(self):
49         """Return a list of :class:`hooke.config.Setting`\s for any
50         configurable UI settings.
51
52         The suggested section setting is::
53
54             Setting(section=self.setting_section, help=self.__doc__)
55         """
56         return []
57
58     def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
59         return
60
61     # Assorted useful tidbits for subclasses
62
63     def _splash_text(self):
64         return ("""
65 This is Hooke, version %s
66
67 COPYRIGHT
68 ----
69 """ % version()).strip()
70
71     def _playlist_status(self, playlist):
72         if len(playlist) > 0:
73             return '%s (%s/%s)' % (playlist.name, playlist._index + 1,
74                                    len(playlist))
75         return 'The playlist %s does not contain any valid force curve data.' \
76             % self.name
77
78 def construct_odict(this_modname, submodnames, class_selector):
79     """Search the submodules `submodnames` of a module `this_modname`
80     for class objects for which `class_selector(class)` returns
81     `True`.  These classes are instantiated and stored in the returned
82     :class:`hooke.compat.odict.odict` in the order in which they were
83     discovered.
84     """
85     instances = odict()
86     for submodname in submodnames:
87         count = len([s for s in submodnames if s == submodname])
88         assert count > 0, 'No %s entries: %s' % (submodname, submodnames)
89         assert count == 1, 'Multiple (%d) %s entries: %s' \
90             % (count, submodname, submodnames)
91         this_mod = __import__(this_modname, fromlist=[submodname])
92         submod = getattr(this_mod, submodname)
93         for objname in dir(submod):
94             obj = getattr(submod, objname)
95             if class_selector(obj):
96                 instance = obj()
97                 instances[instance.name] = instance
98     return instances
99
100 USER_INTERFACES = construct_odict(
101     this_modname=__name__,
102     submodnames=USER_INTERFACE_MODULES,
103     class_selector=IsSubclass(UserInterface, blacklist=[UserInterface]))
104 """:class:`hooke.compat.odict.odict` of :class:`UserInterface`
105 instances keyed by `.name`.
106 """
107
108 def default_settings():
109     settings = [Setting(USER_INTERFACE_SETTING_SECTION,
110                         help='Select the user interface (only one).')]
111     for i,ui in enumerate(USER_INTERFACES.values()):
112         help = ui.__doc__.split('\n', 1)[0]
113         settings.append(Setting(USER_INTERFACE_SETTING_SECTION,
114                                 ui.name, str(i==0), help=help))
115         # i==0 to enable the first by default
116     for ui in USER_INTERFACES.values():
117         settings.extend(ui.default_settings())
118     return settings
119
120 def load_ui(config):
121     uis = [c for c,v in config.items(USER_INTERFACE_SETTING_SECTION) if v == 'True']
122     assert len(uis) == 1, 'Can only select one UI, not %d: %s' % (len(uis),uis)
123     ui_name = uis[0]
124     ui = USER_INTERFACES[ui_name]
125     try:
126         ui.config = dict(config.items(ui.setting_section))
127     except configparser.NoSectionError:
128         pass
129     return ui