1 """The `plugin` module provides optional submodules that add new Hooke
4 All of the science happens in here.
7 import ConfigParser as configparser
9 from ..config import Setting
10 from ..util.graph import Node, Graph
15 # ('curvetools', True),
19 # ('flatfilts-rolf', True),
20 # ('flatfilts', True),
21 # ('generalclamp', True),
22 # ('generaltccd', True),
23 # ('generalvclamp', True),
26 # ('massanalysis', True),
27 # ('multidistance', True),
31 # ('procplots', True),
33 # ('showconvoluted', True),
34 # ('superimpose', True),
38 """List of plugin modules and whether they should be included by
39 default. TODO: autodiscovery
45 """List of builtin modules. TODO: autodiscovery
48 PLUGIN_SETTING_SECTION = 'plugins'
49 """Name of the config section which controls plugin selection.
53 # Plugins and settings
55 class Plugin (object):
56 """A pluggable collection of Hooke commands.
58 Fulfills the same role for Hooke that a software package does for
61 def __init__(self, name):
63 self.setting_section = '%s plugin' % self.name
66 def dependencies(self):
67 """Return a list of :class:`Plugin`\s we require."""
70 def default_settings(self):
71 """Return a list of :class:`hooke.config.Setting`\s for any
72 configurable plugin settings.
74 The suggested section setting is::
76 Setting(section=self.setting_section, help=self.__doc__)
81 """Return a list of :class:`hooke.command.Command`\s provided.
85 class Builtin (Plugin):
86 """A required collection of Hooke commands.
88 These "core" plugins provide essential administrative commands
89 (playlist handling, etc.).
93 # Construct plugin dependency graph and load plugin instances.
95 def construct_graph(this_modname, submodnames, class_selector,
96 assert_name_match=True):
97 """Search the submodules `submodnames` of a module `this_modname`
98 for class objects for which `class_selector(class)` returns
99 `True`. These classes are instantiated, and the `instance.name`
100 is compared to the `submodname` (if `assert_name_match` is
103 The instances are further arranged into a dependency
104 :class:`hooke.util.graph.Graph` according to their
105 `instance.dependencies()` values. The topologically sorted graph
109 for submodname in submodnames:
110 count = len([s for s in submodnames if s == submodname])
111 assert count > 0, 'No %s entries: %s' % (submodname, submodnames)
112 assert count == 1, 'Multiple (%d) %s entries: %s' \
113 % (count, submodname, submodnames)
114 this_mod = __import__(this_modname, fromlist=[submodname])
115 submod = getattr(this_mod, submodname)
116 for objname in dir(submod):
117 obj = getattr(submod, objname)
118 if class_selector(obj):
120 if assert_name_match == True and instance.name != submodname:
122 'Instance name %s does not match module name %s'
123 % (instance.name, submodname))
124 instances[instance.name] = instance
125 graph = Graph([Node([instances[name] for name in i.dependencies()],
127 for i in instances.values()])
128 graph.topological_sort()
131 class IsSubclass (object):
132 """A safe subclass comparator.
137 >>> class A (object):
142 >>> is_subclass = IsSubclass(A)
145 >>> is_subclass = IsSubclass(A, blacklist=[A])
153 def __init__(self, base_class, blacklist=None):
154 self.base_class = base_class
155 if blacklist == None:
157 self.blacklist = blacklist
158 def __call__(self, other):
160 subclass = issubclass(other, self.base_class)
163 if other in self.blacklist:
167 PLUGIN_GRAPH = construct_graph(
168 this_modname=__name__,
169 submodnames=[name for name,include in PLUGIN_MODULES] + BUILTIN_MODULES,
170 class_selector=IsSubclass(Plugin, blacklist=[Plugin, Builtin]))
171 """Topologically sorted list of all possible :class:`Plugin`\s and
175 def default_settings():
176 settings = [Setting(PLUGIN_SETTING_SECTION,
177 help='Enable/disable default plugins.')]
178 for pnode in PLUGIN_GRAPH:
179 if pnode.data.name in BUILTIN_MODULES:
180 continue # builtin inclusion is not optional
182 default_include = [di for mod_name,di in PLUGIN_MODULES
183 if mod_name == plugin.name][0]
184 help = 'Commands: ' + ', '.join([c.name for c in plugin.commands()])
185 settings.append(Setting(
186 section=PLUGIN_SETTING_SECTION,
188 value=str(default_include),
191 for pnode in PLUGIN_GRAPH:
193 settings.extend(plugin.default_settings())
196 def load_graph(graph, config, include_section):
201 include = config.getboolean(include_section, item.name)
202 except configparser.NoOptionError:
203 include = True # non-optional include (e.g. a Builtin)
206 item.config = dict(config.items(item.setting_section))
207 except configparser.NoSectionError: