/paperspace/cli/common.py

https://github.com/Paperspace/paperspace-python
Python | 61 lines | 43 code | 17 blank | 1 comment | 5 complexity | 770cc651cdefb2c0950960d437c5a133 MD5 | raw file
  1. import functools
  2. import getpass
  3. import json
  4. import click
  5. from click_didyoumean import DYMMixin
  6. from click_help_colors import HelpColorsGroup
  7. api_key_option = click.option(
  8. "--apiKey",
  9. "api_key",
  10. help="API key to use this time only",
  11. )
  12. def del_if_value_is_none(dict_):
  13. """Remove all elements with value == None"""
  14. for key, val in list(dict_.items()):
  15. if val is None:
  16. del dict_[key]
  17. def jsonify_dicts(dict_):
  18. json_fields = [
  19. "envVars",
  20. "nodeAttrs"
  21. ]
  22. for field in json_fields:
  23. if field in dict_:
  24. dict_[field] = json.dumps(dict_[field])
  25. class ClickGroup(DYMMixin, HelpColorsGroup):
  26. pass
  27. def prompt_for_secret(prompt):
  28. def callback_fun(ctx, param, value):
  29. if value is None:
  30. value = getpass.getpass(prompt)
  31. return value
  32. return callback_fun
  33. def deprecated(version="1.0.0"):
  34. deprecated_invoke_notice = """DeprecatedWarning: \nWARNING: This command will not be included in version %s .
  35. For more information, please see:
  36. https://docs.paperspace.com
  37. If you depend on functionality not listed there, please file an issue.""" % version
  38. def new_invoke(self, ctx):
  39. click.echo(click.style(deprecated_invoke_notice, fg='red'), err=True)
  40. super(type(self), self).invoke(ctx)
  41. def decorator(f):
  42. f.invoke = functools.partial(new_invoke, f)
  43. return decorator