def extract_args_from_signature(operation, excluded_params=None):
""" Extracts basic argument data from an operation's signature and docstring
excluded_params: List of params to ignore and not extract. By default we ignore ['self', 'kwargs'].
"""
args = []
try:
# only supported in python3 - falling back to argspec if not available
sig = inspect.signature(operation)
args = sig.parameters
except AttributeError:
sig = inspect.getargspec(operation) # pylint: disable=deprecated-method
args = sig.args
arg_docstring_help = option_descriptions(operation)
excluded_params = excluded_params or ['self', 'kwargs']
for arg_name in [a for a in args if a not in excluded_params]:
try:
# this works in python3
default = args[arg_name].default
required = default == inspect.Parameter.empty # pylint: disable=no-member
except TypeError:
arg_defaults = (dict(zip(sig.args[-len(sig.defaults):], sig.defaults))
if sig.defaults
else {})
default = arg_defaults.get(arg_name)
required = arg_name not in arg_defaults
action = 'store_' + str(not default).lower() if isinstance(default, bool) else None
try:
default = (default
if default != inspect._empty # pylint: disable=protected-access, no-member
else None)
except AttributeError:
pass
options_list = ['--' + arg_name.replace('_', '-')]
help_str = arg_docstring_help.get(arg_name)
yield (arg_name, CLICommandArgument(arg_name,
options_list=options_list,
required=required,
default=default,
help=help_str,
action=action))
评论列表
文章目录