/lib/ansible/utils/display_functions.py

https://github.com/ajanthanm/ansible · Python · 63 lines · 33 code · 12 blank · 18 comment · 9 complexity · 09254bc853a667611cc4f063075ba168 MD5 · raw file

  1. # (c) 2014, Michael DeHaan <michael.dehaan@gmail.com>
  2. #
  3. # This file is part of Ansible
  4. #
  5. # Ansible is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # Ansible is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
  17. import textwrap
  18. from ansible import constants as C
  19. from ansible import errors
  20. from ansible.callbacks import display
  21. __all__ = ['deprecated', 'warning', 'system_warning']
  22. # list of all deprecation messages to prevent duplicate display
  23. deprecations = {}
  24. warns = {}
  25. def deprecated(msg, version, removed=False):
  26. ''' used to print out a deprecation message.'''
  27. if not removed and not C.DEPRECATION_WARNINGS:
  28. return
  29. if not removed:
  30. if version:
  31. new_msg = "\n[DEPRECATION WARNING]: %s. This feature will be removed in version %s." % (msg, version)
  32. else:
  33. new_msg = "\n[DEPRECATION WARNING]: %s. This feature will be removed in a future release." % (msg)
  34. new_msg = new_msg + " Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.\n\n"
  35. else:
  36. raise errors.AnsibleError("[DEPRECATED]: %s. Please update your playbooks." % msg)
  37. wrapped = textwrap.wrap(new_msg, 79)
  38. new_msg = "\n".join(wrapped) + "\n"
  39. if new_msg not in deprecations:
  40. display(new_msg, color='purple', stderr=True)
  41. deprecations[new_msg] = 1
  42. def warning(msg):
  43. new_msg = "\n[WARNING]: %s" % msg
  44. wrapped = textwrap.wrap(new_msg, 79)
  45. new_msg = "\n".join(wrapped) + "\n"
  46. if new_msg not in warns:
  47. display(new_msg, color='bright purple', stderr=True)
  48. warns[new_msg] = 1
  49. def system_warning(msg):
  50. if C.SYSTEM_WARNINGS:
  51. warning(msg)