minimize HookCaller attributes: avoid passing in hookrelay to HookCallers

--HG--
branch : plugin_no_pytest
This commit is contained in:
holger krekel 2015-04-24 13:02:49 +02:00
parent feb4b2249a
commit 237ac8562f
1 changed files with 14 additions and 14 deletions

View File

@ -166,14 +166,15 @@ class PluginManager(object):
assert not hasattr(self, "_wrapping") assert not hasattr(self, "_wrapping")
self._wrapping = True self._wrapping = True
hooktrace = self.hook.trace
def _docall(self, methods, kwargs): def _docall(self, methods, kwargs):
trace = self.hookrelay.trace hooktrace.root.indent += 1
trace.root.indent += 1 hooktrace(self.name, kwargs)
trace(self.name, kwargs)
box = yield box = yield
if box.excinfo is None: if box.excinfo is None:
trace("finish", self.name, "-->", box.result) hooktrace("finish", self.name, "-->", box.result)
trace.root.indent -= 1 hooktrace.root.indent -= 1
return add_method_wrapper(HookCaller, _docall) return add_method_wrapper(HookCaller, _docall)
@ -181,7 +182,7 @@ class PluginManager(object):
caller = getattr(self.hook, name) caller = getattr(self.hook, name)
methods = self.listattr(name, plugins=plugins) methods = self.listattr(name, plugins=plugins)
if methods: if methods:
return HookCaller(self.hook, caller.name, caller.firstresult, return HookCaller(caller.name, caller.firstresult,
argnames=caller.argnames, methods=methods) argnames=caller.argnames, methods=methods)
return caller return caller
@ -208,7 +209,7 @@ class PluginManager(object):
self._plugins.append(plugin) self._plugins.append(plugin)
# rescan all methods for the hookcallers we found # rescan all methods for the hookcallers we found
for hookcaller in hookcallers: for hookcaller in hookcallers:
hookcaller.scan_methods() self._scan_methods(hookcaller)
return True return True
def unregister(self, plugin): def unregister(self, plugin):
@ -220,7 +221,7 @@ class PluginManager(object):
del self._name2plugin[name] del self._name2plugin[name]
hookcallers = self._plugin2hookcallers.pop(plugin) hookcallers = self._plugin2hookcallers.pop(plugin)
for hookcaller in hookcallers: for hookcaller in hookcallers:
hookcaller.scan_methods() self._scan_methods(hookcaller)
def addhooks(self, module_or_class): def addhooks(self, module_or_class):
""" add new hook definitions from the given module_or_class using """ add new hook definitions from the given module_or_class using
@ -231,7 +232,7 @@ class PluginManager(object):
if name.startswith(self._prefix): if name.startswith(self._prefix):
method = module_or_class.__dict__[name] method = module_or_class.__dict__[name]
firstresult = getattr(method, 'firstresult', False) firstresult = getattr(method, 'firstresult', False)
hc = HookCaller(self.hook, name, firstresult=firstresult, hc = HookCaller(name, firstresult=firstresult,
argnames=varnames(method, startindex=isclass)) argnames=varnames(method, startindex=isclass))
setattr(self.hook, name, hc) setattr(self.hook, name, hc)
names.append(name) names.append(name)
@ -282,6 +283,9 @@ class PluginManager(object):
l.extend(wrappers) l.extend(wrappers)
return l return l
def _scan_methods(self, hookcaller):
hookcaller.methods = self.listattr(hookcaller.name)
def call_plugin(self, plugin, methname, kwargs): def call_plugin(self, plugin, methname, kwargs):
return MultiCall(methods=self.listattr(methname, plugins=[plugin]), return MultiCall(methods=self.listattr(methname, plugins=[plugin]),
kwargs=kwargs, firstresult=True).execute() kwargs=kwargs, firstresult=True).execute()
@ -394,8 +398,7 @@ class HookRelay:
class HookCaller: class HookCaller:
def __init__(self, hookrelay, name, firstresult, argnames, methods=()): def __init__(self, name, firstresult, argnames, methods=()):
self.hookrelay = hookrelay
self.name = name self.name = name
self.firstresult = firstresult self.firstresult = firstresult
self.argnames = ["__multicall__"] self.argnames = ["__multicall__"]
@ -406,9 +409,6 @@ class HookCaller:
def __repr__(self): def __repr__(self):
return "<HookCaller %r>" %(self.name,) return "<HookCaller %r>" %(self.name,)
def scan_methods(self):
self.methods = self.hookrelay._pm.listattr(self.name)
def __call__(self, **kwargs): def __call__(self, **kwargs):
return self._docall(self.methods, kwargs) return self._docall(self.methods, kwargs)