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:
parent
3403ba4923
commit
af4389e606
@ -72,9 +72,6 @@ class Build:
|
||||
Executes the build command of the kas plugin.
|
||||
"""
|
||||
|
||||
if args.cmd != 'build':
|
||||
return False
|
||||
|
||||
ctx = create_global_context(args)
|
||||
ctx.config = Config(args.config, args.target, args.task)
|
||||
|
||||
@ -108,8 +105,6 @@ class Build:
|
||||
|
||||
macro.run(ctx, args.skip)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class BuildCommand(Command):
|
||||
"""
|
||||
|
12
kas/kas.py
12
kas/kas.py
@ -134,7 +134,7 @@ def kas_get_argparser():
|
||||
for ext_plugin in pkg_resources.iter_entry_points('kas.plugins'):
|
||||
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)
|
||||
setup_parser_common_args(plugin_parser)
|
||||
plugin.setup_parser(plugin_parser)
|
||||
@ -162,11 +162,11 @@ def kas(argv):
|
||||
loop.add_signal_handler(sig, interruption)
|
||||
atexit.register(_atexit_handler)
|
||||
|
||||
for plugin in getattr(kasplugin, 'plugins', []):
|
||||
if plugin().run(args):
|
||||
return 0
|
||||
|
||||
parser.print_help()
|
||||
if args.cmd:
|
||||
plugin = getattr(kasplugin, 'plugins', {})[args.cmd]
|
||||
plugin().run(args)
|
||||
else:
|
||||
parser.print_help()
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -334,5 +334,7 @@ def kasplugin(plugin_class):
|
||||
A decorator that registers kas plugins
|
||||
"""
|
||||
if not hasattr(kasplugin, 'plugins'):
|
||||
setattr(kasplugin, 'plugins', [])
|
||||
getattr(kasplugin, 'plugins').append(plugin_class)
|
||||
setattr(kasplugin, 'plugins', {})
|
||||
getattr(kasplugin, 'plugins').update({
|
||||
plugin_class.name: plugin_class
|
||||
})
|
||||
|
@ -68,9 +68,6 @@ class Shell:
|
||||
Runs this kas plugin
|
||||
"""
|
||||
|
||||
if args.cmd != 'shell':
|
||||
return False
|
||||
|
||||
ctx = create_global_context(args)
|
||||
ctx.config = Config(args.config, None, None)
|
||||
|
||||
@ -108,8 +105,6 @@ class Shell:
|
||||
|
||||
macro.run(ctx, args.skip)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
class ShellCommand(Command):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user