def handle_exception(prefix, cmd_args, desc, perm, func): @wraps(func) def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except (orchestrator.OrchestratorError, ImportError) as e: # Do not print Traceback for expected errors. return HandleCommandResult(-errno.ENOENT, stderr=str(e)) except NotImplementedError: msg = 'This Orchestrator does not support `{}`'.format(prefix) return HandleCommandResult(-errno.ENOENT, stderr=msg) return CLICommand(prefix, cmd_args, desc, perm)(wrapper)
def __new__(cls, name, bases, dct): # type: ignore klass = super().__new__(cls, name, bases, dct) cls.threaded_benchmark_runner = None for base in bases: for name, func in inspect.getmembers(base, cls.is_public): # However not necessary (CLICommand uses a registry) # save functions to klass._cli_{n}() methods. This # can help on unit testing wrapper = cls.func_wrapper(func) command = CLICommand( **CephCommander(func).to_ceph_signature())( # type: ignore wrapper) setattr(klass, f'_cli_{name}', command) return klass
def handle_exception(prefix: str, perm: str, func: FuncT) -> FuncT: @wraps(func) def wrapper(*args: Any, **kwargs: Any) -> Any: try: return func(*args, **kwargs) except (OrchestratorError, ServiceSpecValidationError) as e: # Do not print Traceback for expected errors. return HandleCommandResult(e.errno, stderr=str(e)) except ImportError as e: return HandleCommandResult(-errno.ENOENT, stderr=str(e)) except NotImplementedError: msg = 'This Orchestrator does not support `{}`'.format(prefix) return HandleCommandResult(-errno.ENOENT, stderr=msg) # misuse lambda to copy `wrapper` wrapper_copy = lambda *l_args, **l_kwargs: wrapper(*l_args, **l_kwargs) # noqa: E731 wrapper_copy._prefix = prefix # type: ignore wrapper_copy._cli_command = CLICommand(prefix, perm) # type: ignore wrapper_copy._cli_command.store_func_metadata(func) # type: ignore wrapper_copy._cli_command.func = wrapper_copy # type: ignore return cast(FuncT, wrapper_copy)