Debian Codegraph @

lib/ansible/module_utils/common/json.py

raw
1 from __future__ import annotations as _annotations
2
3 import enum as _enum
4 import json as _stdlib_json
5 import types as _types
6
7 from ansible.module_utils import _internal
8 from ansible.module_utils._internal import _json
9 from ansible.module_utils._internal._json import _legacy_encoder
10 from ansible.module_utils._internal._json import _profiles
11 from ansible.module_utils._internal._json._profiles import _tagless
12 from ansible.module_utils.common import warnings as _warnings
13
14
15 def __getattr__(name: str) -> object:
16 """Handle dynamic module members which are or will be deprecated."""
17 if name in ('AnsibleJSONEncoder', '_AnsibleJSONEncoder'):
18 # deprecated: description='deprecate legacy encoder' core_version='2.23'
19 # if not name.startswith('_'): # avoid duplicate deprecation warning for imports from ajson
20 # _warnings.deprecate(
21 # msg="The `AnsibleJSONEncoder` type is deprecated.",
22 # version="2.27",
23 # help_text="Use a profile-based encoder instead.", # DTFIX-FUTURE: improve this help text
24 # )
25
26 return _get_legacy_encoder()
27
28 if name in ('AnsibleJSONDecoder', '_AnsibleJSONDecoder'):
29 # deprecated: description='deprecate legacy decoder' core_version='2.23'
30 # if not name.startswith('_'): # avoid duplicate deprecation warning for imports from ajson
31 # _warnings.deprecate(
32 # msg="The `AnsibleJSONDecoder` type is deprecated.",
33 # version="2.27",
34 # help_text="Use a profile-based decoder instead.", # DTFIX-FUTURE: improve this help text
35 # )
36
37 return _tagless.Decoder
38
39 if name == 'json_dump':
40 _warnings.deprecate(
41 msg="The `json_dump` function is deprecated.",
42 version="2.23",
43 help_text="Use `json.dumps` with the appropriate `cls` instead.",
44 )
45
46 return _json_dump
47
48 raise AttributeError(name)
49
50
51 def _get_legacy_encoder() -> type[_stdlib_json.JSONEncoder]:
52 """Compatibility hack: previous module_utils AnsibleJSONEncoder impl did controller-side work, controller plugins require a more fully-featured impl."""
53 if _internal.is_controller:
54 return _internal.import_controller_module('ansible._internal._json._legacy_encoder').LegacyControllerJSONEncoder
55
56 return _legacy_encoder.LegacyTargetJSONEncoder
57
58
59 def _json_dump(structure):
60 """JSON dumping function maintained for temporary backward compatibility."""
61 return _stdlib_json.dumps(structure, cls=_get_legacy_encoder(), sort_keys=True, indent=4)
62
63
64 class Direction(_enum.Enum):
65 """Enumeration used to select a contextually-appropriate JSON profile for module messaging."""
66
67 CONTROLLER_TO_MODULE = _enum.auto()
68 """Encode/decode messages from the Ansible controller to an Ansible module."""
69 MODULE_TO_CONTROLLER = _enum.auto()
70 """Encode/decode messages from an Ansible module to the Ansible controller."""
71
72
73 def get_encoder(profile: str | _types.ModuleType, /) -> type[_stdlib_json.JSONEncoder]:
74 """Return a `JSONEncoder` for the given `profile`."""
75 return _json.get_encoder_decoder(profile, _profiles.AnsibleProfileJSONEncoder)
76
77
78 def get_decoder(profile: str | _types.ModuleType, /) -> type[_stdlib_json.JSONDecoder]:
79 """Return a `JSONDecoder` for the given `profile`."""
80 return _json.get_encoder_decoder(profile, _profiles.AnsibleProfileJSONDecoder)
81
82
83 def get_module_encoder(name: str, direction: Direction, /) -> type[_stdlib_json.JSONEncoder]:
84 """Return a `JSONEncoder` for the module profile specified by `name` and `direction`."""
85 return get_encoder(_json.get_module_serialization_profile_name(name, direction == Direction.CONTROLLER_TO_MODULE))
86
87
88 def get_module_decoder(name: str, direction: Direction, /) -> type[_stdlib_json.JSONDecoder]:
89 """Return a `JSONDecoder` for the module profile specified by `name` and `direction`."""
90 return get_decoder(_json.get_module_serialization_profile_name(name, direction == Direction.CONTROLLER_TO_MODULE))