Update hooks from oslo-incubator copy

Collapse identical methods for pre/post into one method. Copy
over documentation as well. We are getting rid of oslo-incubator's
copy of hooks.py in the following change id, so from now on
nova has the only copy of this module

If55b37ccb20d9bc520909490f4f00de4ed430638

Change-Id: Ie43913fc847ee7ce247862f53c14a2daec433224
This commit is contained in:
Davanum Srinivas 2014-10-27 16:21:29 -04:00 committed by Davanum Srinivas (dims)
parent 43094a98e0
commit 62712d9fb0
1 changed files with 51 additions and 33 deletions

View File

@ -46,7 +46,7 @@ import functools
import stevedore import stevedore
from nova.i18n import _LE from nova.i18n import _, _LE
from nova.openstack.common import log as logging from nova.openstack.common import log as logging
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -65,50 +65,68 @@ class FatalHookException(Exception):
class HookManager(stevedore.hook.HookManager): class HookManager(stevedore.hook.HookManager):
def __init__(self, name): def __init__(self, name):
# invoke_on_load creates an instance of the Hook class """Invoke_on_load creates an instance of the Hook class
:param name: The name of the hooks to load.
:type name: str
"""
super(HookManager, self).__init__(NS, name, invoke_on_load=True) super(HookManager, self).__init__(NS, name, invoke_on_load=True)
def run_pre(self, name, args, kwargs, f=None): def _run(self, name, method_type, args, kwargs, func=None):
if method_type not in ('pre', 'post'):
msg = _("Wrong type of hook method. "
"Only 'pre' and 'post' type allowed")
raise ValueError(msg)
for e in self.extensions: for e in self.extensions:
obj = e.obj obj = e.obj
pre = getattr(obj, 'pre', None) hook_method = getattr(obj, method_type, None)
if pre: if hook_method:
LOG.debug("Running %(name)s pre-hook: %(obj)s", LOG.debug("Running %(name)s %(type)s-hook: %(obj)s",
{'name': name, 'obj': obj}) {'name': name, 'type': method_type, 'obj': obj})
try: try:
if f: if func:
pre(f, *args, **kwargs) hook_method(func, *args, **kwargs)
else: else:
pre(*args, **kwargs) hook_method(*args, **kwargs)
except FatalHookException: except FatalHookException:
msg = _LE("Fatal Exception running %(name)s " msg = _LE("Fatal Exception running %(name)s "
"pre-hook: %(obj)s") "%(type)s-hook: %(obj)s")
LOG.exception(msg, {'name': name, 'obj': obj}) LOG.exception(msg, {'name': name, 'type': method_type,
'obj': obj})
raise raise
except Exception: except Exception:
msg = _LE("Exception running %(name)s pre-hook: %(obj)s") msg = _LE("Exception running %(name)s "
LOG.exception(msg, {'name': name, 'obj': obj}) "%(type)s-hook: %(obj)s")
LOG.exception(msg, {'name': name, 'type': method_type,
'obj': obj})
def run_pre(self, name, args, kwargs, f=None):
"""Execute optional pre methods of loaded hooks.
:param name: The name of the loaded hooks.
:param args: Positional arguments which would be transmitted into
all pre methods of loaded hooks.
:param kwargs: Keyword args which would be transmitted into all pre
methods of loaded hooks.
:param f: Target function.
"""
self._run(name=name, method_type='pre', args=args, kwargs=kwargs,
func=f)
def run_post(self, name, rv, args, kwargs, f=None): def run_post(self, name, rv, args, kwargs, f=None):
for e in reversed(self.extensions): """Execute optional post methods of loaded hooks.
obj = e.obj
post = getattr(obj, 'post', None) :param name: The name of the loaded hooks.
if post: :param rv: Return values of target method call.
LOG.debug("Running %(name)s post-hook: %(obj)s", :param args: Positional arguments which would be transmitted into
{'name': name, 'obj': obj}) all post methods of loaded hooks.
try: :param kwargs: Keyword args which would be transmitted into all post
if f: methods of loaded hooks.
post(f, rv, *args, **kwargs) :param f: Target function.
else: """
post(rv, *args, **kwargs) self._run(name=name, method_type='post', args=(rv,) + args,
except FatalHookException: kwargs=kwargs, func=f)
msg = _LE("Fatal Exception running %(name)s "
"post-hook: %(obj)s")
LOG.exception(msg, {'name': name, 'obj': obj})
raise
except Exception:
msg = _LE("Exception running %(name)s post-hook: %(obj)s")
LOG.exception(msg, {'name': name, 'obj': obj})
def add_hook(name, pass_function=False): def add_hook(name, pass_function=False):