Debian Codegraph @

lib/ansible/module_utils/_internal/_plugin_info.py

raw
1 from __future__ import annotations
2
3 import typing as t
4
5 from . import _messages
6
7
8 class HasPluginInfo(t.Protocol):
9 """Protocol to type-annotate and expose PluginLoader-set values."""
10
11 @property
12 def ansible_name(self) -> str | None:
13 """Fully resolved plugin name."""
14
15 @property
16 def plugin_type(self) -> str:
17 """Plugin type name."""
18
19
20 def get_plugin_info(value: HasPluginInfo) -> _messages.PluginInfo:
21 """Utility method that returns a `PluginInfo` from an object implementing the `HasPluginInfo` protocol."""
22 return _messages.PluginInfo(
23 resolved_name=value.ansible_name,
24 type=normalize_plugin_type(value.plugin_type),
25 )
26
27
28 def normalize_plugin_type(value: str) -> _messages.PluginType | None:
29 """Normalize value and return it as a PluginType, or None if the value does match any known plugin type."""
30 value = value.lower()
31
32 if value == 'modules':
33 value = 'module'
34
35 try:
36 return _messages.PluginType(value)
37 except ValueError:
38 return None