Run hooks for DisplayCommandBase

Command base class has provisions to run hooks for a command in
its run() method. We need to do the same for the
DisplayCommandBase class since it does not call super().run()

Change-Id: Ic5481523d4bd919fe7fb10e00330dea2ff688ec4
This commit is contained in:
Rajath Agasthya 2017-06-29 01:09:14 -07:00
parent 0c378ba96f
commit c6d258da1c
2 changed files with 28 additions and 2 deletions

View File

@ -133,12 +133,36 @@ class Command(object):
Return the value returned by :meth:`take_action` or 0.
"""
self._run_before_hooks(parsed_args)
return_code = self.take_action(parsed_args) or 0
self._run_after_hooks(parsed_args, return_code)
return return_code
def _run_before_hooks(self, parsed_args):
"""Calls before() method of the hooks.
This method is intended to be called from the run() method before
take_action() is called.
This method should only be overriden by developers creating new
command base classes and only if it is necessary to have different
hook processing behavior.
"""
for hook in self._hooks:
hook.obj.before(parsed_args)
return_code = self.take_action(parsed_args) or 0
def _run_after_hooks(self, parsed_args, return_code):
"""Calls after() method of the hooks.
This method is intended to be called from the run() method after
take_action() is called.
This method should only be overriden by developers creating new
command base classes and only if it is necessary to have different
hook processing behavior.
"""
for hook in self._hooks:
hook.obj.after(parsed_args, return_code)
return return_code
class _SmartHelpFormatter(_argparse.HelpFormatter):

View File

@ -108,8 +108,10 @@ class DisplayCommandBase(command.Command):
return columns_to_include, selector
def run(self, parsed_args):
self._run_before_hooks(parsed_args)
self.formatter = self._formatter_plugins[parsed_args.formatter].obj
column_names, data = self.take_action(parsed_args)
self._run_after_hooks(parsed_args, (column_names, data))
self.produce_output(parsed_args, column_names, data)
return 0