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),
30 # ('procplots', True),
32 # ('showconvoluted', True),
33 # ('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
49 # Plugins and settings
51 class Plugin (object):
52 """A pluggable collection of Hooke commands.
54 Fulfills the same role for Hooke that a software package does for
57 def __init__(self, name):
59 self.setting_section = '%s plugin' % self.name
62 def dependencies(self):
63 """Return a list of :class:`Plugin`\s we require."""
66 def default_settings(self):
67 """Return a list of :class:`hooke.config.Setting`\s for any
68 configurable plugin settings.
70 The suggested section setting is::
72 Setting(section=self.setting_section, help=self.__doc__)
77 """Return a list of :class:`hooke.command.Command`\s provided.
81 class Builtin (Plugin):
82 """A required collection of Hooke commands.
84 These "core" plugins provide essential administrative commands
85 (playlist handling, etc.).
89 # Construct plugin dependency graph and load plugin instances.
91 def construct_graph(this_modname, submodnames, class_selector,
92 assert_name_match=True):
93 """Search the submodules `submodnames` of a module `this_modname`
94 for class objects for which `class_selector(class)` returns
95 `True`. These classes are instantiated, and the `instance.name`
96 is compared to the `submodname` (if `assert_name_match` is
99 The instances are further arranged into a dependency
100 :class:`hooke.util.graph.Graph` according to their
101 `instance.dependencies()` values. The topologically sorted graph
105 for submodname in submodnames:
106 count = len([s for s in submodnames if s == submodname])
107 assert count > 0, 'No %s entries: %s' % (submodname, submodnames)
108 assert count == 1, 'Multiple (%d) %s entries: %s' \
109 % (count, submodname, submodnames)
110 this_mod = __import__(this_modname, fromlist=[submodname])
111 submod = getattr(this_mod, submodname)
112 for objname in dir(submod):
113 obj = getattr(submod, objname)
114 if class_selector(obj):
116 if assert_name_match == True and instance.name != submodname:
118 'Instance name %s does not match module name %s'
119 % (instance.name, submodname))
120 instances[instance.name] = instance
121 graph = Graph([Node([instances[name] for name in i.dependencies()],
123 for i in instances.values()])
124 graph.topological_sort()
127 class IsSubclass (object):
128 """A safe subclass comparator.
133 >>> class A (object):
138 >>> is_subclass = IsSubclass(A)
141 >>> is_subclass = IsSubclass(A, blacklist=[A])
149 def __init__(self, base_class, blacklist=None):
150 self.base_class = base_class
151 if blacklist == None:
153 self.blacklist = blacklist
154 def __call__(self, other):
156 subclass = issubclass(other, self.base_class)
159 if other in self.blacklist:
163 PLUGIN_GRAPH = construct_graph(
164 this_modname=__name__,
165 submodnames=[name for name,include in PLUGIN_MODULES] + BUILTIN_MODULES,
166 class_selector=IsSubclass(Plugin, blacklist=[Plugin, Builtin]))
167 """Topologically sorted list of all possible :class:`Plugin`\s and
171 def default_settings():
173 'plugins', help='Enable/disable default plugins.')]
174 for pnode in PLUGIN_GRAPH:
175 if pnode.data.name in BUILTIN_MODULES:
176 continue # builtin inclusion is not optional
178 default_include = [di for mod_name,di in PLUGIN_MODULES
179 if mod_name == plugin.name][0]
180 help = 'Commands: ' + ', '.join([c.name for c in plugin.commands()])
181 settings.append(Setting(
184 value=str(default_include),
187 for pnode in PLUGIN_GRAPH:
189 settings.extend(plugin.default_settings())
192 def load_graph(graph, config, include_section):
197 include = config.getboolean(include_section, item.name)
198 except configparser.NoOptionError:
199 include = True # non-optional include (e.g. a Builtin)
202 item.config = dict(config.items(item.setting_section))
203 except configparser.NoSectionError: