Debian Codegraph @

lib/ansible/module_utils/_internal/_event_utils.py

raw
1 from __future__ import annotations as _annotations
2
3 import typing as _t
4
5 from ansible.module_utils._internal import _text_utils, _messages
6
7
8 def deduplicate_message_parts(message_parts: list[str]) -> str:
9 """Format the given list of messages into a brief message, while deduplicating repeated elements."""
10 message_parts = list(reversed(message_parts))
11
12 message = message_parts.pop(0)
13
14 for message_part in message_parts:
15 # avoid duplicate messages where the cause was already concatenated to the exception message
16 if message_part.endswith(message):
17 message = message_part
18 else:
19 message = _text_utils.concat_message(message_part, message)
20
21 return message
22
23
24 def format_event_brief_message(event: _messages.Event) -> str:
25 """
26 Format an event into a brief message.
27 Help text, contextual information and sub-events will be omitted.
28 """
29 message_parts: list[str] = []
30
31 while True:
32 message_parts.append(event.msg)
33
34 if not event.chain or not event.chain.follow:
35 break
36
37 event = event.chain.event
38
39 return deduplicate_message_parts(message_parts)
40
41
42 def deprecation_as_dict(deprecation: _messages.DeprecationSummary) -> _t.Dict[str, _t.Any]:
43 """Returns a dictionary representation of the deprecation object in the format exposed to playbooks."""
44 from ansible.module_utils._internal._deprecator import INDETERMINATE_DEPRECATOR # circular import from messages
45
46 if deprecation.deprecator and deprecation.deprecator != INDETERMINATE_DEPRECATOR:
47 collection_name = '.'.join(deprecation.deprecator.resolved_name.split('.')[:2])
48 else:
49 collection_name = None
50
51 result = dict(
52 msg=format_event_brief_message(deprecation.event),
53 collection_name=collection_name,
54 )
55
56 if deprecation.date:
57 result.update(date=deprecation.date)
58 else:
59 result.update(version=deprecation.version)
60
61 return result