def import_callable(ctx, param, value):
if value is None:
return None
delim = ':'
if delim not in value:
raise click.BadParameter(
'string to import should have the form '
'pkg.module:callable_attribute')
mod, identifier = value.rsplit(delim, 1)
try:
func_or_cls = getattr(importlib.import_module(mod), identifier)
except AttributeError:
raise click.BadParameter('{} does not exist in {}'
.format(identifier, mod))
if callable(func_or_cls):
return func_or_cls
raise RuntimeError('{} is not callable'.format(value))
评论列表
文章目录