2010-11-06 06:37:31 +08:00
|
|
|
"""
|
2015-04-22 19:31:46 +08:00
|
|
|
PluginManager, basic initialization and tracing.
|
2010-11-06 06:37:31 +08:00
|
|
|
"""
|
2013-10-02 20:32:40 +08:00
|
|
|
import sys
|
2010-10-13 17:12:27 +08:00
|
|
|
import inspect
|
2010-10-12 18:54:32 +08:00
|
|
|
import py
|
2010-10-13 03:59:15 +08:00
|
|
|
|
2014-10-08 17:27:14 +08:00
|
|
|
py3 = sys.version_info > (3,0)
|
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
class TagTracer:
|
2011-07-15 01:11:50 +08:00
|
|
|
def __init__(self):
|
2010-11-06 06:37:31 +08:00
|
|
|
self._tag2proc = {}
|
|
|
|
self.writer = None
|
2010-11-06 16:05:17 +08:00
|
|
|
self.indent = 0
|
2010-11-06 06:37:31 +08:00
|
|
|
|
|
|
|
def get(self, name):
|
|
|
|
return TagTracerSub(self, (name,))
|
|
|
|
|
2012-11-29 17:04:39 +08:00
|
|
|
def format_message(self, tags, args):
|
|
|
|
if isinstance(args[-1], dict):
|
|
|
|
extra = args[-1]
|
|
|
|
args = args[:-1]
|
|
|
|
else:
|
|
|
|
extra = {}
|
|
|
|
|
|
|
|
content = " ".join(map(str, args))
|
|
|
|
indent = " " * self.indent
|
2013-02-04 23:07:51 +08:00
|
|
|
|
2012-11-29 17:04:39 +08:00
|
|
|
lines = [
|
|
|
|
"%s%s [%s]\n" %(indent, content, ":".join(tags))
|
|
|
|
]
|
|
|
|
|
|
|
|
for name, value in extra.items():
|
|
|
|
lines.append("%s %s: %s\n" % (indent, name, value))
|
|
|
|
return lines
|
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
def processmessage(self, tags, args):
|
2012-11-29 17:04:39 +08:00
|
|
|
if self.writer is not None and args:
|
|
|
|
lines = self.format_message(tags, args)
|
|
|
|
self.writer(''.join(lines))
|
2010-11-06 06:37:31 +08:00
|
|
|
try:
|
|
|
|
self._tag2proc[tags](tags, args)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def setwriter(self, writer):
|
|
|
|
self.writer = writer
|
|
|
|
|
|
|
|
def setprocessor(self, tags, processor):
|
|
|
|
if isinstance(tags, str):
|
|
|
|
tags = tuple(tags.split(":"))
|
|
|
|
else:
|
|
|
|
assert isinstance(tags, tuple)
|
|
|
|
self._tag2proc[tags] = processor
|
|
|
|
|
|
|
|
class TagTracerSub:
|
|
|
|
def __init__(self, root, tags):
|
|
|
|
self.root = root
|
|
|
|
self.tags = tags
|
|
|
|
def __call__(self, *args):
|
|
|
|
self.root.processmessage(self.tags, args)
|
|
|
|
def setmyprocessor(self, processor):
|
|
|
|
self.root.setprocessor(self.tags, processor)
|
|
|
|
def get(self, name):
|
|
|
|
return self.__class__(self.root, self.tags + (name,))
|
|
|
|
|
2014-10-07 22:16:47 +08:00
|
|
|
|
2014-10-08 17:27:14 +08:00
|
|
|
def add_method_wrapper(cls, wrapper_func):
|
|
|
|
""" Substitute the function named "wrapperfunc.__name__" at class
|
|
|
|
"cls" with a function that wraps the call to the original function.
|
|
|
|
Return an undo function which can be called to reset the class to use
|
|
|
|
the old method again.
|
2014-10-07 22:16:47 +08:00
|
|
|
|
2014-10-08 17:27:14 +08:00
|
|
|
wrapper_func is called with the same arguments as the method
|
|
|
|
it wraps and its result is used as a wrap_controller for
|
|
|
|
calling the original function.
|
2014-10-07 22:16:47 +08:00
|
|
|
"""
|
2014-10-08 17:27:14 +08:00
|
|
|
name = wrapper_func.__name__
|
2014-10-04 21:49:31 +08:00
|
|
|
oldcall = getattr(cls, name)
|
|
|
|
def wrap_exec(*args, **kwargs):
|
2014-10-08 17:27:14 +08:00
|
|
|
gen = wrapper_func(*args, **kwargs)
|
|
|
|
return wrapped_call(gen, lambda: oldcall(*args, **kwargs))
|
2014-10-07 22:16:47 +08:00
|
|
|
|
2014-10-04 21:49:31 +08:00
|
|
|
setattr(cls, name, wrap_exec)
|
|
|
|
return lambda: setattr(cls, name, oldcall)
|
|
|
|
|
2014-10-09 18:21:01 +08:00
|
|
|
def raise_wrapfail(wrap_controller, msg):
|
|
|
|
co = wrap_controller.gi_code
|
|
|
|
raise RuntimeError("wrap_controller at %r %s:%d %s" %
|
|
|
|
(co.co_name, co.co_filename, co.co_firstlineno, msg))
|
2014-10-04 21:49:31 +08:00
|
|
|
|
2014-10-08 17:27:14 +08:00
|
|
|
def wrapped_call(wrap_controller, func):
|
2014-10-09 18:21:01 +08:00
|
|
|
""" Wrap calling to a function with a generator which needs to yield
|
|
|
|
exactly once. The yield point will trigger calling the wrapped function
|
|
|
|
and return its CallOutcome to the yield point. The generator then needs
|
|
|
|
to finish (raise StopIteration) in order for the wrapped call to complete.
|
2014-10-08 17:27:14 +08:00
|
|
|
"""
|
2014-10-09 02:23:40 +08:00
|
|
|
try:
|
|
|
|
next(wrap_controller) # first yield
|
|
|
|
except StopIteration:
|
2014-10-09 18:21:01 +08:00
|
|
|
raise_wrapfail(wrap_controller, "did not yield")
|
2014-10-08 17:27:14 +08:00
|
|
|
call_outcome = CallOutcome(func)
|
|
|
|
try:
|
|
|
|
wrap_controller.send(call_outcome)
|
2014-10-09 18:21:01 +08:00
|
|
|
raise_wrapfail(wrap_controller, "has second yield")
|
2014-10-08 17:27:14 +08:00
|
|
|
except StopIteration:
|
|
|
|
pass
|
2014-10-09 02:23:40 +08:00
|
|
|
return call_outcome.get_result()
|
2014-10-08 17:27:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CallOutcome:
|
2014-10-09 18:21:01 +08:00
|
|
|
""" Outcome of a function call, either an exception or a proper result.
|
|
|
|
Calling the ``get_result`` method will return the result or reraise
|
|
|
|
the exception raised when the function was called. """
|
2014-10-08 17:27:14 +08:00
|
|
|
excinfo = None
|
|
|
|
def __init__(self, func):
|
|
|
|
try:
|
|
|
|
self.result = func()
|
2015-04-17 17:47:29 +08:00
|
|
|
except BaseException:
|
2014-10-08 17:27:14 +08:00
|
|
|
self.excinfo = sys.exc_info()
|
|
|
|
|
|
|
|
def force_result(self, result):
|
|
|
|
self.result = result
|
|
|
|
self.excinfo = None
|
|
|
|
|
2014-10-09 02:23:40 +08:00
|
|
|
def get_result(self):
|
|
|
|
if self.excinfo is None:
|
|
|
|
return self.result
|
|
|
|
else:
|
|
|
|
ex = self.excinfo
|
|
|
|
if py3:
|
|
|
|
raise ex[1].with_traceback(ex[2])
|
|
|
|
py.builtin._reraise(*ex)
|
|
|
|
|
2014-10-08 17:27:14 +08:00
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
class PluginManager(object):
|
2015-04-22 22:33:20 +08:00
|
|
|
""" Core Pluginmanager class which manages registration
|
|
|
|
of plugin objects and 1:N hook calling.
|
|
|
|
|
|
|
|
You can register new hooks by calling ``addhooks(module_or_class)``.
|
|
|
|
You can register plugin objects (which contain hooks) by calling
|
|
|
|
``register(plugin)``. The Pluginmanager is initialized with a
|
|
|
|
prefix that is searched for in the names of the dict of registered
|
|
|
|
plugin objects. An optional excludefunc allows to blacklist names which
|
|
|
|
are not considered as hooks despite a matching prefix.
|
|
|
|
|
|
|
|
For debugging purposes you can call ``set_tracing(writer)``
|
|
|
|
which will subsequently send debug information to the specified
|
|
|
|
write function.
|
|
|
|
"""
|
|
|
|
|
2015-04-22 19:31:46 +08:00
|
|
|
def __init__(self, prefix, excludefunc=None):
|
|
|
|
self._prefix = prefix
|
|
|
|
self._excludefunc = excludefunc
|
2010-10-12 18:54:32 +08:00
|
|
|
self._name2plugin = {}
|
2010-10-13 17:12:27 +08:00
|
|
|
self._plugins = []
|
2014-10-09 16:47:32 +08:00
|
|
|
self._plugin2hookcallers = {}
|
2010-11-06 16:05:17 +08:00
|
|
|
self.trace = TagTracer().get("pluginmanage")
|
2015-04-22 19:31:46 +08:00
|
|
|
self.hook = HookRelay(pm=self)
|
2013-09-30 19:14:14 +08:00
|
|
|
|
2014-10-02 21:25:42 +08:00
|
|
|
def set_tracing(self, writer):
|
2015-04-22 22:33:20 +08:00
|
|
|
""" turn on tracing to the given writer method and
|
|
|
|
return an undo function. """
|
2014-10-02 21:25:42 +08:00
|
|
|
self.trace.root.setwriter(writer)
|
2014-10-04 21:49:31 +08:00
|
|
|
# reconfigure HookCalling to perform tracing
|
|
|
|
assert not hasattr(self, "_wrapping")
|
|
|
|
self._wrapping = True
|
|
|
|
|
2015-04-24 19:02:49 +08:00
|
|
|
hooktrace = self.hook.trace
|
|
|
|
|
2014-10-04 21:49:31 +08:00
|
|
|
def _docall(self, methods, kwargs):
|
2015-04-24 19:02:49 +08:00
|
|
|
hooktrace.root.indent += 1
|
|
|
|
hooktrace(self.name, kwargs)
|
2014-10-08 17:27:14 +08:00
|
|
|
box = yield
|
|
|
|
if box.excinfo is None:
|
2015-04-24 19:02:49 +08:00
|
|
|
hooktrace("finish", self.name, "-->", box.result)
|
|
|
|
hooktrace.root.indent -= 1
|
2014-10-04 21:49:31 +08:00
|
|
|
|
2015-04-22 22:33:20 +08:00
|
|
|
return add_method_wrapper(HookCaller, _docall)
|
2013-10-04 17:36:45 +08:00
|
|
|
|
2015-04-22 19:31:46 +08:00
|
|
|
def make_hook_caller(self, name, plugins):
|
|
|
|
caller = getattr(self.hook, name)
|
2015-04-25 17:29:11 +08:00
|
|
|
hc = HookCaller(caller.name, plugins, firstresult=caller.firstresult,
|
|
|
|
argnames=caller.argnames)
|
|
|
|
for plugin in hc.plugins:
|
|
|
|
meth = getattr(plugin, name, None)
|
|
|
|
if meth is not None:
|
2015-04-25 17:29:11 +08:00
|
|
|
hc.add_method(meth)
|
2015-04-25 17:29:11 +08:00
|
|
|
return hc
|
2015-04-22 19:31:46 +08:00
|
|
|
|
2015-04-22 19:33:01 +08:00
|
|
|
def register(self, plugin, name=None):
|
2015-04-22 22:42:41 +08:00
|
|
|
""" Register a plugin with the given name and ensure that all its
|
|
|
|
hook implementations are integrated. If the name is not specified
|
|
|
|
we use the ``__name__`` attribute of the plugin object or, if that
|
|
|
|
doesn't exist, the id of the plugin. This method will raise a
|
|
|
|
ValueError if the eventual name is already registered. """
|
2015-04-22 20:54:28 +08:00
|
|
|
name = name or self._get_canonical_name(plugin)
|
2012-06-17 03:29:04 +08:00
|
|
|
if self._name2plugin.get(name, None) == -1:
|
|
|
|
return
|
2015-04-22 20:54:28 +08:00
|
|
|
if self.hasplugin(name):
|
2013-09-30 19:14:14 +08:00
|
|
|
raise ValueError("Plugin already registered: %s=%s\n%s" %(
|
|
|
|
name, plugin, self._name2plugin))
|
2010-12-06 23:54:42 +08:00
|
|
|
#self.trace("registering", name, plugin)
|
2015-04-22 20:54:28 +08:00
|
|
|
# allow subclasses to intercept here by calling a helper
|
|
|
|
return self._do_register(plugin, name)
|
|
|
|
|
|
|
|
def _do_register(self, plugin, name):
|
2015-04-25 17:29:11 +08:00
|
|
|
self._plugin2hookcallers[plugin] = self._scan_plugin(plugin)
|
2014-10-01 18:20:11 +08:00
|
|
|
self._name2plugin[name] = plugin
|
2015-04-22 19:33:01 +08:00
|
|
|
self._plugins.append(plugin)
|
2010-10-12 18:54:32 +08:00
|
|
|
return True
|
|
|
|
|
2014-10-09 16:47:32 +08:00
|
|
|
def unregister(self, plugin):
|
2015-04-22 22:42:41 +08:00
|
|
|
""" unregister the plugin object and all its contained hook implementations
|
|
|
|
from internal data structures. """
|
2015-04-22 19:33:01 +08:00
|
|
|
self._plugins.remove(plugin)
|
2010-10-12 18:54:32 +08:00
|
|
|
for name, value in list(self._name2plugin.items()):
|
|
|
|
if value == plugin:
|
|
|
|
del self._name2plugin[name]
|
2014-10-09 16:47:32 +08:00
|
|
|
hookcallers = self._plugin2hookcallers.pop(plugin)
|
|
|
|
for hookcaller in hookcallers:
|
2015-04-25 17:29:11 +08:00
|
|
|
hookcaller.remove_plugin(plugin)
|
2010-10-12 18:54:32 +08:00
|
|
|
|
2015-04-22 19:31:46 +08:00
|
|
|
def addhooks(self, module_or_class):
|
2015-04-22 22:42:41 +08:00
|
|
|
""" add new hook definitions from the given module_or_class using
|
|
|
|
the prefix/excludefunc with which the PluginManager was initialized. """
|
2015-04-22 19:31:46 +08:00
|
|
|
isclass = int(inspect.isclass(module_or_class))
|
|
|
|
names = []
|
|
|
|
for name in dir(module_or_class):
|
|
|
|
if name.startswith(self._prefix):
|
2015-04-25 17:29:11 +08:00
|
|
|
specfunc = module_or_class.__dict__[name]
|
|
|
|
firstresult = getattr(specfunc, 'firstresult', False)
|
|
|
|
hc = getattr(self.hook, name, None)
|
|
|
|
argnames = varnames(specfunc, startindex=isclass)
|
|
|
|
if hc is None:
|
|
|
|
hc = HookCaller(name, [], firstresult=firstresult,
|
2015-04-25 17:29:11 +08:00
|
|
|
argnames=argnames)
|
2015-04-25 17:29:11 +08:00
|
|
|
setattr(self.hook, name, hc)
|
|
|
|
else:
|
|
|
|
# plugins registered this hook without knowing the spec
|
|
|
|
hc.setspec(firstresult=firstresult, argnames=argnames)
|
|
|
|
for plugin in hc.plugins:
|
|
|
|
self._verify_hook(hc, specfunc, plugin)
|
2015-04-25 17:29:11 +08:00
|
|
|
hc.add_method(getattr(plugin, name))
|
2015-04-22 19:31:46 +08:00
|
|
|
names.append(name)
|
|
|
|
if not names:
|
|
|
|
raise ValueError("did not find new %r hooks in %r"
|
|
|
|
%(self._prefix, module_or_class))
|
2010-10-12 18:54:32 +08:00
|
|
|
|
|
|
|
def getplugins(self):
|
2015-04-22 22:42:41 +08:00
|
|
|
""" return the complete list of registered plugins. NOTE that
|
|
|
|
you will get the internal list and need to make a copy if you
|
|
|
|
modify the list."""
|
2015-04-22 19:33:01 +08:00
|
|
|
return self._plugins
|
2010-10-12 18:54:32 +08:00
|
|
|
|
2015-04-22 20:54:28 +08:00
|
|
|
def isregistered(self, plugin):
|
2015-04-22 22:42:41 +08:00
|
|
|
""" Return True if the plugin is already registered under its
|
|
|
|
canonical name. """
|
2015-04-22 20:54:28 +08:00
|
|
|
return self.hasplugin(self._get_canonical_name(plugin)) or \
|
|
|
|
plugin in self._plugins
|
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
def hasplugin(self, name):
|
2015-04-22 22:42:41 +08:00
|
|
|
""" Return True if there is a registered with the given name. """
|
2015-04-22 20:54:28 +08:00
|
|
|
return name in self._name2plugin
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
def getplugin(self, name):
|
2015-04-22 22:42:41 +08:00
|
|
|
""" Return a plugin or None for the given name. """
|
2015-04-22 19:31:46 +08:00
|
|
|
return self._name2plugin.get(name)
|
2010-10-12 18:54:32 +08:00
|
|
|
|
2010-10-13 17:12:27 +08:00
|
|
|
def call_plugin(self, plugin, methname, kwargs):
|
2015-04-25 17:29:11 +08:00
|
|
|
meth = getattr(plugin, methname, None)
|
|
|
|
if meth is not None:
|
|
|
|
return MultiCall(methods=[meth], kwargs=kwargs, firstresult=True).execute()
|
2010-10-13 17:12:27 +08:00
|
|
|
|
2015-04-22 22:42:41 +08:00
|
|
|
def _scan_plugin(self, plugin):
|
2015-04-25 17:29:11 +08:00
|
|
|
hookcallers = []
|
2015-04-22 22:42:41 +08:00
|
|
|
for name in dir(plugin):
|
|
|
|
if name[0] == "_" or not name.startswith(self._prefix):
|
|
|
|
continue
|
|
|
|
hook = getattr(self.hook, name, None)
|
|
|
|
method = getattr(plugin, name)
|
|
|
|
if hook is None:
|
|
|
|
if self._excludefunc is not None and self._excludefunc(name):
|
|
|
|
continue
|
2015-04-25 17:29:11 +08:00
|
|
|
hook = HookCaller(name, [plugin])
|
|
|
|
setattr(self.hook, name, hook)
|
|
|
|
elif hook.pre:
|
|
|
|
# there is only a pre non-specced stub
|
|
|
|
hook.plugins.append(plugin)
|
|
|
|
else:
|
|
|
|
# we have a hook spec, can verify early
|
|
|
|
self._verify_hook(hook, method, plugin)
|
|
|
|
hook.plugins.append(plugin)
|
2015-04-25 17:29:11 +08:00
|
|
|
hook.add_method(method)
|
2015-04-25 17:29:11 +08:00
|
|
|
hookcallers.append(hook)
|
|
|
|
return hookcallers
|
|
|
|
|
|
|
|
def _verify_hook(self, hook, method, plugin):
|
|
|
|
for arg in varnames(method):
|
|
|
|
if arg not in hook.argnames:
|
|
|
|
pluginname = self._get_canonical_name(plugin)
|
|
|
|
raise PluginValidationError(
|
|
|
|
"Plugin %r\nhook %r\nargument %r not available\n"
|
|
|
|
"plugin definition: %s\n"
|
|
|
|
"available hookargs: %s" %(
|
|
|
|
pluginname, hook.name, arg, formatdef(method),
|
|
|
|
", ".join(hook.argnames)))
|
|
|
|
|
|
|
|
def check_pending(self):
|
|
|
|
for name in self.hook.__dict__:
|
|
|
|
if name.startswith(self._prefix):
|
|
|
|
hook = getattr(self.hook, name)
|
|
|
|
if hook.pre:
|
|
|
|
for plugin in hook.plugins:
|
|
|
|
method = getattr(plugin, hook.name)
|
|
|
|
if not getattr(method, "optionalhook", False):
|
|
|
|
raise PluginValidationError(
|
|
|
|
"unknown hook %r in plugin %r" %(name, plugin))
|
2015-04-22 22:42:41 +08:00
|
|
|
|
|
|
|
def _get_canonical_name(self, plugin):
|
|
|
|
return getattr(plugin, "__name__", None) or str(id(plugin))
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
class MultiCall:
|
2010-10-13 17:12:27 +08:00
|
|
|
""" execute a call into multiple python functions/methods. """
|
2014-03-14 19:49:35 +08:00
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
def __init__(self, methods, kwargs, firstresult=False):
|
2015-04-25 17:29:11 +08:00
|
|
|
self.methods = methods
|
2010-10-13 17:12:27 +08:00
|
|
|
self.kwargs = kwargs
|
2014-10-01 20:55:54 +08:00
|
|
|
self.kwargs["__multicall__"] = self
|
2010-10-12 18:54:32 +08:00
|
|
|
self.results = []
|
|
|
|
self.firstresult = firstresult
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
status = "%d results, %d meths" % (len(self.results), len(self.methods))
|
|
|
|
return "<MultiCall %s, kwargs=%r>" %(status, self.kwargs)
|
|
|
|
|
|
|
|
def execute(self):
|
2014-10-08 17:27:14 +08:00
|
|
|
all_kwargs = self.kwargs
|
|
|
|
while self.methods:
|
|
|
|
method = self.methods.pop()
|
|
|
|
args = [all_kwargs[argname] for argname in varnames(method)]
|
|
|
|
if hasattr(method, "hookwrapper"):
|
|
|
|
return wrapped_call(method(*args), self.execute)
|
|
|
|
res = method(*args)
|
|
|
|
if res is not None:
|
|
|
|
self.results.append(res)
|
|
|
|
if self.firstresult:
|
|
|
|
return res
|
|
|
|
if not self.firstresult:
|
|
|
|
return self.results
|
2014-03-14 19:49:35 +08:00
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
|
2014-10-02 21:25:42 +08:00
|
|
|
def varnames(func, startindex=None):
|
2013-11-19 22:33:52 +08:00
|
|
|
""" return argument name tuple for a function, method, class or callable.
|
|
|
|
|
|
|
|
In case of a class, its "__init__" method is considered.
|
|
|
|
For methods the "self" parameter is not included unless you are passing
|
|
|
|
an unbound method with Python3 (which has no supports for unbound methods)
|
|
|
|
"""
|
|
|
|
cache = getattr(func, "__dict__", {})
|
2011-03-08 01:28:45 +08:00
|
|
|
try:
|
2013-11-19 22:33:52 +08:00
|
|
|
return cache["_varnames"]
|
|
|
|
except KeyError:
|
2011-03-08 01:28:45 +08:00
|
|
|
pass
|
2013-11-19 22:33:52 +08:00
|
|
|
if inspect.isclass(func):
|
|
|
|
try:
|
|
|
|
func = func.__init__
|
|
|
|
except AttributeError:
|
|
|
|
return ()
|
2014-10-02 21:25:42 +08:00
|
|
|
startindex = 1
|
2013-11-19 22:33:52 +08:00
|
|
|
else:
|
|
|
|
if not inspect.isfunction(func) and not inspect.ismethod(func):
|
|
|
|
func = getattr(func, '__call__', func)
|
2014-10-02 21:25:42 +08:00
|
|
|
if startindex is None:
|
|
|
|
startindex = int(inspect.ismethod(func))
|
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
rawcode = py.code.getrawcode(func)
|
|
|
|
try:
|
2014-10-02 21:25:42 +08:00
|
|
|
x = rawcode.co_varnames[startindex:rawcode.co_argcount]
|
2010-10-12 18:54:32 +08:00
|
|
|
except AttributeError:
|
2011-03-08 01:28:45 +08:00
|
|
|
x = ()
|
2014-10-01 20:55:54 +08:00
|
|
|
else:
|
|
|
|
defaults = func.__defaults__
|
|
|
|
if defaults:
|
|
|
|
x = x[:-len(defaults)]
|
2013-11-19 22:33:52 +08:00
|
|
|
try:
|
|
|
|
cache["_varnames"] = x
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2011-03-08 01:28:45 +08:00
|
|
|
return x
|
2010-10-12 18:54:32 +08:00
|
|
|
|
2014-10-01 20:55:54 +08:00
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
class HookRelay:
|
2015-04-22 19:31:46 +08:00
|
|
|
def __init__(self, pm):
|
2010-10-13 17:12:27 +08:00
|
|
|
self._pm = pm
|
2010-11-06 16:05:17 +08:00
|
|
|
self.trace = pm.trace.root.get("hook")
|
2014-10-01 18:19:11 +08:00
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
|
|
|
|
class HookCaller:
|
2015-04-25 17:29:11 +08:00
|
|
|
def __init__(self, name, plugins, argnames=None, firstresult=None):
|
2010-10-12 18:54:32 +08:00
|
|
|
self.name = name
|
2015-04-25 17:29:11 +08:00
|
|
|
self.plugins = plugins
|
|
|
|
if argnames is not None:
|
|
|
|
argnames = ["__multicall__"] + list(argnames)
|
|
|
|
self.argnames = argnames
|
2010-10-12 18:54:32 +08:00
|
|
|
self.firstresult = firstresult
|
2015-04-25 17:29:11 +08:00
|
|
|
self.wrappers = []
|
|
|
|
self.nonwrappers = []
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def pre(self):
|
|
|
|
return self.argnames is None
|
|
|
|
|
|
|
|
def setspec(self, argnames, firstresult):
|
|
|
|
assert self.pre
|
2014-10-09 16:47:32 +08:00
|
|
|
assert "self" not in argnames # sanity check
|
2015-04-25 17:29:11 +08:00
|
|
|
self.argnames = ["__multicall__"] + list(argnames)
|
|
|
|
self.firstresult = firstresult
|
2014-10-01 18:20:11 +08:00
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
def remove_plugin(self, plugin):
|
|
|
|
self.plugins.remove(plugin)
|
|
|
|
meth = getattr(plugin, self.name)
|
|
|
|
try:
|
|
|
|
self.nonwrappers.remove(meth)
|
|
|
|
except ValueError:
|
|
|
|
self.wrappers.remove(meth)
|
2015-04-25 17:29:11 +08:00
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
def add_method(self, meth):
|
2015-04-25 17:29:11 +08:00
|
|
|
assert not self.pre
|
|
|
|
if hasattr(meth, 'hookwrapper'):
|
|
|
|
self.wrappers.append(meth)
|
|
|
|
elif hasattr(meth, 'trylast'):
|
|
|
|
self.nonwrappers.insert(0, meth)
|
|
|
|
elif hasattr(meth, 'tryfirst'):
|
|
|
|
self.nonwrappers.append(meth)
|
|
|
|
else:
|
|
|
|
if not self.nonwrappers or not hasattr(self.nonwrappers[-1], "tryfirst"):
|
|
|
|
self.nonwrappers.append(meth)
|
|
|
|
else:
|
|
|
|
for i in reversed(range(len(self.nonwrappers)-1)):
|
|
|
|
if hasattr(self.nonwrappers[i], "tryfirst"):
|
|
|
|
continue
|
|
|
|
self.nonwrappers.insert(i+1, meth)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.nonwrappers.insert(0, meth)
|
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return "<HookCaller %r>" %(self.name,)
|
2009-03-06 05:10:18 +08:00
|
|
|
|
2010-10-12 18:54:32 +08:00
|
|
|
def __call__(self, **kwargs):
|
2015-04-25 17:29:11 +08:00
|
|
|
return self._docall(self.nonwrappers + self.wrappers, kwargs)
|
2010-10-07 17:51:58 +08:00
|
|
|
|
2014-10-01 18:20:11 +08:00
|
|
|
def callextra(self, methods, **kwargs):
|
2015-04-25 17:29:11 +08:00
|
|
|
return self._docall(self.nonwrappers + methods + self.wrappers,
|
|
|
|
kwargs)
|
2014-10-01 18:20:11 +08:00
|
|
|
|
2010-11-07 02:46:24 +08:00
|
|
|
def _docall(self, methods, kwargs):
|
2015-04-25 17:29:11 +08:00
|
|
|
assert not self.pre, self.name
|
2014-10-02 21:25:42 +08:00
|
|
|
return MultiCall(methods, kwargs,
|
|
|
|
firstresult=self.firstresult).execute()
|
2014-10-01 18:20:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
class PluginValidationError(Exception):
|
|
|
|
""" plugin failed validation. """
|
|
|
|
|
|
|
|
|
|
|
|
def formatdef(func):
|
|
|
|
return "%s%s" % (
|
|
|
|
func.__name__,
|
|
|
|
inspect.formatargspec(*inspect.getargspec(func))
|
|
|
|
)
|