kas: Simplify plugin lookup

By storing plugins in a dictionary indexed by plugin name rather than a
list, we can simplify plugin lookup and remove the undocumented need for
a plugin's run() method to return True when it has matched the given
command.

The command will be rejected by the argument parser if it does not match
one of the plugin names so we do not need to handle failure to lookup
the plugin in the dictionary.

Signed-off-by: Paul Barker <pbarker@konsulko.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Paul Barker 2020-10-16 19:31:44 +02:00 committed by Jan Kiszka
parent 3403ba4923
commit af4389e606
4 changed files with 10 additions and 18 deletions

View File

@ -72,9 +72,6 @@ class Build:
Executes the build command of the kas plugin. Executes the build command of the kas plugin.
""" """
if args.cmd != 'build':
return False
ctx = create_global_context(args) ctx = create_global_context(args)
ctx.config = Config(args.config, args.target, args.task) ctx.config = Config(args.config, args.target, args.task)
@ -108,8 +105,6 @@ class Build:
macro.run(ctx, args.skip) macro.run(ctx, args.skip)
return True
class BuildCommand(Command): class BuildCommand(Command):
""" """

View File

@ -134,7 +134,7 @@ def kas_get_argparser():
for ext_plugin in pkg_resources.iter_entry_points('kas.plugins'): for ext_plugin in pkg_resources.iter_entry_points('kas.plugins'):
ext_plugin.load() ext_plugin.load()
for plugin in getattr(kasplugin, 'plugins', []): for plugin in getattr(kasplugin, 'plugins', {}).values():
plugin_parser = subparser.add_parser(plugin.name, help=plugin.helpmsg) plugin_parser = subparser.add_parser(plugin.name, help=plugin.helpmsg)
setup_parser_common_args(plugin_parser) setup_parser_common_args(plugin_parser)
plugin.setup_parser(plugin_parser) plugin.setup_parser(plugin_parser)
@ -162,11 +162,11 @@ def kas(argv):
loop.add_signal_handler(sig, interruption) loop.add_signal_handler(sig, interruption)
atexit.register(_atexit_handler) atexit.register(_atexit_handler)
for plugin in getattr(kasplugin, 'plugins', []): if args.cmd:
if plugin().run(args): plugin = getattr(kasplugin, 'plugins', {})[args.cmd]
return 0 plugin().run(args)
else:
parser.print_help() parser.print_help()
def main(): def main():

View File

@ -334,5 +334,7 @@ def kasplugin(plugin_class):
A decorator that registers kas plugins A decorator that registers kas plugins
""" """
if not hasattr(kasplugin, 'plugins'): if not hasattr(kasplugin, 'plugins'):
setattr(kasplugin, 'plugins', []) setattr(kasplugin, 'plugins', {})
getattr(kasplugin, 'plugins').append(plugin_class) getattr(kasplugin, 'plugins').update({
plugin_class.name: plugin_class
})

View File

@ -68,9 +68,6 @@ class Shell:
Runs this kas plugin Runs this kas plugin
""" """
if args.cmd != 'shell':
return False
ctx = create_global_context(args) ctx = create_global_context(args)
ctx.config = Config(args.config, None, None) ctx.config = Config(args.config, None, None)
@ -108,8 +105,6 @@ class Shell:
macro.run(ctx, args.skip) macro.run(ctx, args.skip)
return True
class ShellCommand(Command): class ShellCommand(Command):
""" """