api_utils.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from collections.abc import Callable
  2. from itertools import zip_longest
  3. from typing import List
  4. from datalad.utils import getargspec
  5. from .utils import _NoValue
  6. def get_cmd_displayname(api, cmdname):
  7. dname = api.get(cmdname, {}).get(
  8. 'name',
  9. cmdname.replace('_', ' ').capitalize()
  10. )
  11. dname_parts = dname.split(' ')
  12. if dname_parts[:2] == ['Create', 'sibling']:
  13. dname = f'Create a {" ".join(dname_parts[2:])}' \
  14. f'{" " if len(dname_parts) > 2 else ""}sibling'
  15. return dname
  16. def get_cmd_params(cmd: Callable) -> List:
  17. """Take a callable and return a list of parameter names, and their defaults
  18. Parameter names and defaults are returned as 2-tuples. If a parameter has
  19. no default, the special value `_NoValue` is used.
  20. """
  21. # lifted from setup_parser_for_interface()
  22. args, varargs, varkw, defaults = getargspec(cmd, include_kwonlyargs=True)
  23. if not args:
  24. return []
  25. return list(
  26. zip_longest(
  27. # fuse parameters from the back, to match with their respective
  28. # defaults -- if soem have no defaults, they would be the first
  29. args[::-1],
  30. defaults[::-1],
  31. # pad with a dedicate type, to be able to tell if there was a
  32. # default or not
  33. fillvalue=_NoValue)
  34. # reverse the order again to match the original order in the signature
  35. )[::-1]