| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (c) 2019 Ansible Project |
| 3 |
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 4 |
|
| 5 |
from __future__ import annotations as _annotations |
| 6 |
|
| 7 |
import typing as _t |
| 8 |
|
| 9 |
from ansible.module_utils._internal import _traceback, _deprecator, _event_utils, _messages, _errors |
| 10 |
from ansible.module_utils import _internal |
| 11 |
|
| 12 |
|
| 13 |
def warn( |
| 14 |
warning: str, |
| 15 |
*, |
| 16 |
help_text: str | None = None, |
| 17 |
obj: object | None = None, |
| 18 |
) -> None: |
| 19 |
"""Record a warning to be returned with the module result.""" |
| 20 |
_skip_stackwalk = True |
| 21 |
|
| 22 |
if _internal.is_controller: |
| 23 |
_display = _internal.import_controller_module('ansible.utils.display').Display() |
| 24 |
_display.warning( |
| 25 |
msg=warning, |
| 26 |
help_text=help_text, |
| 27 |
obj=obj, |
| 28 |
) |
| 29 |
|
| 30 |
return |
| 31 |
|
| 32 |
warning = _messages.WarningSummary( |
| 33 |
event=_messages.Event( |
| 34 |
msg=warning, |
| 35 |
help_text=help_text, |
| 36 |
formatted_traceback=_traceback.maybe_capture_traceback(warning, _traceback.TracebackEvent.WARNING), |
| 37 |
), |
| 38 |
) |
| 39 |
|
| 40 |
_global_warnings[warning] = None |
| 41 |
|
| 42 |
|
| 43 |
def error_as_warning( |
| 44 |
msg: str | None, |
| 45 |
exception: BaseException, |
| 46 |
*, |
| 47 |
help_text: str | None = None, |
| 48 |
obj: object = None, |
| 49 |
) -> None: |
| 50 |
"""Display an exception as a warning.""" |
| 51 |
_skip_stackwalk = True |
| 52 |
|
| 53 |
if _internal.is_controller: |
| 54 |
_display = _internal.import_controller_module('ansible.utils.display').Display() |
| 55 |
_display.error_as_warning( |
| 56 |
msg=msg, |
| 57 |
exception=exception, |
| 58 |
help_text=help_text, |
| 59 |
obj=obj, |
| 60 |
) |
| 61 |
|
| 62 |
return |
| 63 |
|
| 64 |
event = _errors.EventFactory.from_exception(exception, _traceback.is_traceback_enabled(_traceback.TracebackEvent.WARNING)) |
| 65 |
|
| 66 |
warning = _messages.WarningSummary( |
| 67 |
event=_messages.Event( |
| 68 |
msg=msg, |
| 69 |
help_text=help_text, |
| 70 |
formatted_traceback=_traceback.maybe_capture_traceback(msg, _traceback.TracebackEvent.WARNING), |
| 71 |
chain=_messages.EventChain( |
| 72 |
msg_reason=_errors.MSG_REASON_DIRECT_CAUSE, |
| 73 |
traceback_reason=_errors.TRACEBACK_REASON_EXCEPTION_DIRECT_WARNING, |
| 74 |
event=event, |
| 75 |
), |
| 76 |
), |
| 77 |
) |
| 78 |
|
| 79 |
_global_warnings[warning] = None |
| 80 |
|
| 81 |
|
| 82 |
def deprecate( |
| 83 |
msg: str, |
| 84 |
version: str | None = None, |
| 85 |
date: str | None = None, |
| 86 |
collection_name: str | None = None, |
| 87 |
*, |
| 88 |
deprecator: _messages.PluginInfo | None = None, |
| 89 |
help_text: str | None = None, |
| 90 |
obj: object | None = None, |
| 91 |
) -> None: |
| 92 |
""" |
| 93 |
Record a deprecation warning. |
| 94 |
The `obj` argument is only useful in a controller context; it is ignored for target-side callers. |
| 95 |
Most callers do not need to provide `collection_name` or `deprecator` -- but provide only one if needed. |
| 96 |
Specify `version` or `date`, but not both. |
| 97 |
If `date` is a string, it must be in the form `YYYY-MM-DD`. |
| 98 |
""" |
| 99 |
_skip_stackwalk = True |
| 100 |
|
| 101 |
deprecator = _deprecator.get_best_deprecator(deprecator=deprecator, collection_name=collection_name) |
| 102 |
|
| 103 |
if _internal.is_controller: |
| 104 |
_display = _internal.import_controller_module('ansible.utils.display').Display() |
| 105 |
_display.deprecated( |
| 106 |
msg=msg, |
| 107 |
version=version, |
| 108 |
date=date, |
| 109 |
help_text=help_text, |
| 110 |
obj=obj, |
| 111 |
# skip passing collection_name; get_best_deprecator already accounted for it when present |
| 112 |
deprecator=deprecator, |
| 113 |
) |
| 114 |
|
| 115 |
return |
| 116 |
|
| 117 |
warning = _messages.DeprecationSummary( |
| 118 |
event=_messages.Event( |
| 119 |
msg=msg, |
| 120 |
help_text=help_text, |
| 121 |
formatted_traceback=_traceback.maybe_capture_traceback(msg, _traceback.TracebackEvent.DEPRECATED), |
| 122 |
), |
| 123 |
version=version, |
| 124 |
date=date, |
| 125 |
deprecator=deprecator, |
| 126 |
) |
| 127 |
|
| 128 |
_global_deprecations[warning] = None |
| 129 |
|
| 130 |
|
| 131 |
def get_warning_messages() -> tuple[str, ...]: |
| 132 |
"""Return a tuple of warning messages accumulated over this run.""" |
| 133 |
# DTFIX7: add future deprecation comment |
| 134 |
return tuple(_event_utils.format_event_brief_message(item.event) for item in _global_warnings) |
| 135 |
|
| 136 |
|
| 137 |
def get_deprecation_messages() -> tuple[dict[str, _t.Any], ...]: |
| 138 |
"""Return a tuple of deprecation warning messages accumulated over this run.""" |
| 139 |
# DTFIX7: add future deprecation comment |
| 140 |
return tuple(_event_utils.deprecation_as_dict(item) for item in _global_deprecations) |
| 141 |
|
| 142 |
|
| 143 |
def get_warnings() -> list[_messages.WarningSummary]: |
| 144 |
"""Return a list of warning messages accumulated over this run.""" |
| 145 |
return list(_global_warnings) |
| 146 |
|
| 147 |
|
| 148 |
def get_deprecations() -> list[_messages.DeprecationSummary]: |
| 149 |
"""Return a list of deprecations accumulated over this run.""" |
| 150 |
return list(_global_deprecations) |
| 151 |
|
| 152 |
|
| 153 |
_global_warnings: dict[_messages.WarningSummary, object] = {} |
| 154 |
"""Global, ordered, de-duplicated storage of accumulated warnings for the current module run.""" |
| 155 |
|
| 156 |
_global_deprecations: dict[_messages.DeprecationSummary, object] = {} |
| 157 |
"""Global, ordered, de-duplicated storage of accumulated deprecations for the current module run.""" |