def run_hooks(name, *args, **kwargs): """ Run all defined hooks in the namespace. Yields the result of each hook function run. Optional arguments: name The name of the hook function args Any additional args are passed to the hook function kwargs Any kwargs are passed to the hook function Usage: .. code-block:: python from cement.core.hook import run_hook for result in run_hooks('hook_name'): # do something with result from each hook function ... """ if not hooks.has_key(name): raise CementRuntimeError, "Hook name '%s' is not defined!" % name hooks[name].sort() # Will order based on weight for hook in hooks[name]: log.debug("running hook '%s' from %s" % (name, hook[2].__module__)) res = hook[2](*args, **kwargs) # Results are yielded, so you must fun a for loop on it, you can not # simply call run_hooks(). yield res
def __call__(self, func): if not self.name: self.name = func.__name__ log.debug("registering hook func '%s' from %s into hooks['%s']" % \ (func.__name__, func.__module__, self.name)) if not hooks.has_key(self.name): log.warn("Hook name '%s' is not defined!" % self.name) return func # Hooks are as follows: (weight, name, func) hooks[self.name].append((int(self.weight), func.__name__, func)) return func
def __call__(self, func): if not self.name: self.name = func.__name__ log.debug("registering hook func '%s' from %s into hooks['%s']" % \ (func.__name__, func.__module__, self.name)) if not hooks.has_key(self.name): log.warn("Hook name '%s' is not defined!" % self.name) return func # Hooks are as follows: (weight, name, func) hooks[self.name].append( (int(self.weight), func.__name__, func) ) return func
def define_hook(name): """ Define a hook namespace that plugins can register hooks in. Required arguments: name The name of the hook, stored as hooks['name'] Usage: .. code-block:: python from cement.core.hook import define_hook define_hook('myhookname_hook') """ log.debug("defining hook '%s'", name) if hooks.has_key(name): raise CementRuntimeError, "Hook name '%s' already defined!" % name hooks[name] = []