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
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General 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 `plugin` module provides optional submodules that add new Hooke
22 All of the science happens in here.
25 import ConfigParser as configparser
27 from ..config import Setting
28 from ..util.pluggable import IsSubclass, construct_graph
37 # ('flatfilts-rolf', True),
41 # ('massanalysis', True),
42 # ('multidistance', True),
45 # ('procplots', True),
47 # ('showconvoluted', True),
48 # ('superimpose', True),
53 """List of plugin modules and whether they should be included by
54 default. TODO: autodiscovery
67 """List of builtin modules. TODO: autodiscovery
70 PLUGIN_SETTING_SECTION = 'plugins'
71 """Name of the config section which controls plugin selection.
75 # Plugins and settings
77 class Plugin (object):
78 """A pluggable collection of Hooke commands.
80 Fulfills the same role for Hooke that a software package does for
83 def __init__(self, name):
85 self.setting_section = '%s plugin' % self.name
89 def dependencies(self):
90 """Return a list of names of :class:`Plugin`\s we require."""
93 def default_settings(self):
94 """Return a list of :class:`hooke.config.Setting`\s for any
95 configurable plugin settings.
97 The suggested section setting is::
99 Setting(section=self.setting_section, help=self.__doc__)
104 """Return a list of :class:`hooke.command.Command`\s provided.
106 return list(self._commands)
108 class Builtin (Plugin):
109 """A required collection of Hooke commands.
111 These "core" plugins provide essential administrative commands
112 (playlist handling, etc.).
116 # Plugin utility functions
118 def argument_to_setting(section_name, argument):
119 """Convert an :class:`~hooke.command.Argument` to a
120 `~hooke.conf.Setting`.
122 This is a lossy transition, because
123 :class:`~hooke.command.Argument`\s store more information than
124 `~hooke.conf.Setting`\s.
126 return Setting(section_name, option=argument.name, value=argument.default,
129 # Construct plugin dependency graph and load plugin instances.
131 PLUGIN_GRAPH = construct_graph(
132 this_modname=__name__,
133 submodnames=[name for name,include in PLUGIN_MODULES] + BUILTIN_MODULES,
134 class_selector=IsSubclass(Plugin, blacklist=[Plugin, Builtin]))
135 """Topologically sorted list of all possible :class:`Plugin`\s and
139 def default_settings():
140 settings = [Setting(PLUGIN_SETTING_SECTION,
141 help='Enable/disable default plugins.')]
142 for pnode in PLUGIN_GRAPH:
143 if pnode.data.name in BUILTIN_MODULES:
144 continue # builtin inclusion is not optional
146 default_include = [di for mod_name,di in PLUGIN_MODULES
147 if mod_name == plugin.name][0]
148 help = 'Commands: ' + ', '.join([c.name for c in plugin.commands()])
149 settings.append(Setting(
150 section=PLUGIN_SETTING_SECTION,
152 value=str(default_include),
155 for pnode in PLUGIN_GRAPH:
157 settings.extend(plugin.default_settings())
160 def load_graph(graph, config, include_section):
165 include = config.getboolean(include_section, item.name)
166 except configparser.NoOptionError:
167 include = True # non-optional include (e.g. a Builtin)
170 item.config = dict(config.items(item.setting_section))
171 except configparser.NoSectionError: