PageRenderTime 21ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ansible/utils/display_functions.py

https://github.com/ajanthanm/ansible
Python | 63 lines | 33 code | 12 blank | 18 comment | 14 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
18import textwrap
19
20from ansible import constants as C
21from ansible import errors
22from ansible.callbacks import display
23
24__all__ = ['deprecated', 'warning', 'system_warning']
25
26# list of all deprecation messages to prevent duplicate display
27deprecations = {}
28warns = {}
29
30def deprecated(msg, version, removed=False):
31    ''' used to print out a deprecation message.'''
32
33    if not removed and not C.DEPRECATION_WARNINGS:
34        return
35
36    if not removed:
37        if version:
38            new_msg = "\n[DEPRECATION WARNING]: %s. This feature will be removed in version %s." % (msg, version)
39        else:
40            new_msg = "\n[DEPRECATION WARNING]: %s. This feature will be removed in a future release." % (msg)
41        new_msg = new_msg + " Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.\n\n"
42    else:
43        raise errors.AnsibleError("[DEPRECATED]: %s.  Please update your playbooks." % msg)
44
45    wrapped = textwrap.wrap(new_msg, 79)
46    new_msg = "\n".join(wrapped) + "\n"
47
48    if new_msg not in deprecations:
49        display(new_msg, color='purple', stderr=True)
50        deprecations[new_msg] = 1
51
52def warning(msg):
53    new_msg = "\n[WARNING]: %s" % msg
54    wrapped = textwrap.wrap(new_msg, 79)
55    new_msg = "\n".join(wrapped) + "\n"
56    if new_msg not in warns:
57        display(new_msg, color='bright purple', stderr=True)
58        warns[new_msg] = 1
59
60def system_warning(msg):
61    if C.SYSTEM_WARNINGS:
62        warning(msg)
63