def get_callable(subcommand):
# type: (config.RcliEntryPoint) -> Union[FunctionType, MethodType]
"""Return a callable object from the subcommand.
Args:
subcommand: A object loaded from an entry point. May be a module,
class, or function.
Returns:
The callable entry point for the subcommand. If the subcommand is a
function, it will be returned unchanged. If the subcommand is a module
or a class, an instance of the command class will be returned.
Raises:
AssertionError: Raised when a module entry point does not have a
callable class named Command.
"""
_LOGGER.debug(
'Creating callable from subcommand "%s".', subcommand.__name__)
if isinstance(subcommand, ModuleType):
_LOGGER.debug('Subcommand is a module.')
assert hasattr(subcommand, 'Command'), (
'Module subcommand must have callable "Command" class definition.')
callable_ = subcommand.Command # type: ignore
else:
callable_ = subcommand
if any(isinstance(callable_, t) for t in six.class_types):
return callable_()
return callable_
评论列表
文章目录