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),
37 """List of plugin modules and whether they should be included by
38 default. TODO: autodiscovery
44 """List of builtin modules. TODO: autodiscovery
47 PLUGIN_SETTING_SECTION = 'plugins'
48 """Name of the config section which controls plugin selection.
52 # Plugins and settings
54 class Plugin (object):
55 """A pluggable collection of Hooke commands.
57 Fulfills the same role for Hooke that a software package does for
60 def __init__(self, name):
62 self.setting_section = '%s plugin' % self.name
65 def dependencies(self):
66 """Return a list of :class:`Plugin`\s we require."""
69 def default_settings(self):
70 """Return a list of :class:`hooke.config.Setting`\s for any
71 configurable plugin settings.
73 The suggested section setting is::
75 Setting(section=self.setting_section, help=self.__doc__)
80 """Return a list of :class:`hooke.command.Command`\s provided.
84 class Builtin (Plugin):
85 """A required collection of Hooke commands.
87 These "core" plugins provide essential administrative commands
88 (playlist handling, etc.).
92 # Construct plugin dependency graph and load plugin instances.
94 def construct_graph(this_modname, submodnames, class_selector,
95 assert_name_match=True):
96 """Search the submodules `submodnames` of a module `this_modname`
97 for class objects for which `class_selector(class)` returns
98 `True`. These classes are instantiated, and the `instance.name`
99 is compared to the `submodname` (if `assert_name_match` is
102 The instances are further arranged into a dependency
103 :class:`hooke.util.graph.Graph` according to their
104 `instance.dependencies()` values. The topologically sorted graph
108 for submodname in submodnames:
109 count = len([s for s in submodnames if s == submodname])
110 assert count > 0, 'No %s entries: %s' % (submodname, submodnames)
111 assert count == 1, 'Multiple (%d) %s entries: %s' \
112 % (count, submodname, submodnames)
113 this_mod = __import__(this_modname, fromlist=[submodname])
114 submod = getattr(this_mod, submodname)
115 for objname in dir(submod):
116 obj = getattr(submod, objname)
117 if class_selector(obj):
119 if assert_name_match == True and instance.name != submodname:
121 'Instance name %s does not match module name %s'
122 % (instance.name, submodname))
123 instances[instance.name] = instance
124 graph = Graph([Node([instances[name] for name in i.dependencies()],
126 for i in instances.values()])
127 graph.topological_sort()
130 class IsSubclass (object):
131 """A safe subclass comparator.
136 >>> class A (object):
141 >>> is_subclass = IsSubclass(A)
144 >>> is_subclass = IsSubclass(A, blacklist=[A])
152 def __init__(self, base_class, blacklist=None):
153 self.base_class = base_class
154 if blacklist == None:
156 self.blacklist = blacklist
157 def __call__(self, other):
159 subclass = issubclass(other, self.base_class)
162 if other in self.blacklist:
166 PLUGIN_GRAPH = construct_graph(
167 this_modname=__name__,
168 submodnames=[name for name,include in PLUGIN_MODULES] + BUILTIN_MODULES,
169 class_selector=IsSubclass(Plugin, blacklist=[Plugin, Builtin]))
170 """Topologically sorted list of all possible :class:`Plugin`\s and
174 def default_settings():
175 settings = [Setting(PLUGIN_SETTING_SECTION,
176 help='Enable/disable default plugins.')]
177 for pnode in PLUGIN_GRAPH:
178 if pnode.data.name in BUILTIN_MODULES:
179 continue # builtin inclusion is not optional
181 default_include = [di for mod_name,di in PLUGIN_MODULES
182 if mod_name == plugin.name][0]
183 help = 'Commands: ' + ', '.join([c.name for c in plugin.commands()])
184 settings.append(Setting(
185 section=PLUGIN_SETTING_SECTION,
187 value=str(default_include),
190 for pnode in PLUGIN_GRAPH:
192 settings.extend(plugin.default_settings())
195 def load_graph(graph, config, include_section):
200 include = config.getboolean(include_section, item.name)
201 except configparser.NoOptionError:
202 include = True # non-optional include (e.g. a Builtin)
205 item.config = dict(config.items(item.setting_section))
206 except configparser.NoSectionError: