Source code for ska_sdp_piper.piper.command.command

import builtins
import logging

from .cli_command_parser import CLIArgument, CLICommandParser

logger = logging.getLogger(__name__)


[docs] class Command: """ Base class for creating CLI commands """ def __init__(self, version=None): """ Instantiate command object """ self.sub_command_registry = {} self.version = version
[docs] def sub_command(self, name: str, *cli_args: CLIArgument, help: str = None): """ Decorator for adding sub commands Parameters ---------- name Name of the sub command *cli_args List of CLI arguments for the sub command help Help text Returns ------- function """ def wrapper(func): """ Wrapper function Parameters ---------- func: function Callback function Returns ------- function """ self.sub_command_registry[name] = dict( subparser_name=name, sub_command=func, cli_args=cli_args, help=help, ) return func return wrapper
def __call__(self): """ Run the pipeline as a CLI command """ command_parser = CLICommandParser(version=self.version) for sub_command_map in self.sub_command_registry.values(): command_parser.create_sub_parser(**sub_command_map) cli_args = command_parser.cli_args_dict try: sub_command = cli_args.pop("sub_command") sub_command(**cli_args) except builtins.BaseException as ex: logger.exception(ex) raise ex