| 1 |
# (c) 2020 Matt Martz <[email protected]> |
| 2 |
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 3 |
|
| 4 |
""" |
| 5 |
This file provides ease of use shortcuts for loading and dumping YAML, |
| 6 |
preferring the YAML compiled C extensions to reduce duplicated code. |
| 7 |
""" |
| 8 |
|
| 9 |
from __future__ import annotations as _annotations |
| 10 |
|
| 11 |
import collections.abc as _c |
| 12 |
import typing as _t |
| 13 |
|
| 14 |
from functools import partial as _partial |
| 15 |
|
| 16 |
from .._internal import _datatag |
| 17 |
|
| 18 |
HAS_LIBYAML = False |
| 19 |
|
| 20 |
try: |
| 21 |
import yaml as _yaml |
| 22 |
except ImportError: |
| 23 |
HAS_YAML = False |
| 24 |
else: |
| 25 |
HAS_YAML = True |
| 26 |
|
| 27 |
# DTFIX-FUTURE: refactor this to share the implementation with the controller version |
| 28 |
# use an abstract base class, with __init_subclass__ for representer registration, and instance methods for overridable representers |
| 29 |
# then tests can be consolidated instead of having two nearly identical copies |
| 30 |
|
| 31 |
if HAS_YAML: |
| 32 |
try: |
| 33 |
from yaml import CSafeLoader as SafeLoader |
| 34 |
from yaml import CSafeDumper as SafeDumper |
| 35 |
from yaml.representer import SafeRepresenter |
| 36 |
from yaml.cyaml import CParser as Parser # type: ignore[attr-defined] # pylint: disable=unused-import |
| 37 |
|
| 38 |
HAS_LIBYAML = True |
| 39 |
except (ImportError, AttributeError): |
| 40 |
from yaml import SafeLoader # type: ignore[assignment] |
| 41 |
from yaml import SafeDumper # type: ignore[assignment] |
| 42 |
from yaml.representer import SafeRepresenter # type: ignore[assignment] |
| 43 |
from yaml.parser import Parser # type: ignore[assignment] # pylint: disable=unused-import |
| 44 |
|
| 45 |
class _AnsibleDumper(SafeDumper): |
| 46 |
pass |
| 47 |
|
| 48 |
def _represent_ansible_tagged_object(self, data: _datatag.AnsibleTaggedObject) -> _t.Any: |
| 49 |
return self.represent_data(_datatag.AnsibleTagHelper.as_native_type(data)) |
| 50 |
|
| 51 |
def _represent_tripwire(self, data: _datatag.Tripwire) -> _t.NoReturn: |
| 52 |
data.trip() |
| 53 |
|
| 54 |
_AnsibleDumper.add_multi_representer(_datatag.AnsibleTaggedObject, _represent_ansible_tagged_object) |
| 55 |
|
| 56 |
_AnsibleDumper.add_multi_representer(_datatag.Tripwire, _represent_tripwire) |
| 57 |
_AnsibleDumper.add_multi_representer(_c.Mapping, SafeRepresenter.represent_dict) |
| 58 |
_AnsibleDumper.add_multi_representer(_c.Sequence, SafeRepresenter.represent_list) |
| 59 |
|
| 60 |
yaml_load = _partial(_yaml.load, Loader=SafeLoader) |
| 61 |
yaml_load_all = _partial(_yaml.load_all, Loader=SafeLoader) |
| 62 |
|
| 63 |
yaml_dump = _partial(_yaml.dump, Dumper=_AnsibleDumper) |
| 64 |
yaml_dump_all = _partial(_yaml.dump_all, Dumper=_AnsibleDumper) |
| 65 |
else: |
| 66 |
SafeLoader = object # type: ignore[assignment,misc] |
| 67 |
SafeDumper = object # type: ignore[assignment,misc] |
| 68 |
Parser = object # type: ignore[assignment,misc] |
| 69 |
|
| 70 |
yaml_load = None # type: ignore[assignment] |
| 71 |
yaml_load_all = None # type: ignore[assignment] |
| 72 |
yaml_dump = None # type: ignore[assignment] |
| 73 |
yaml_dump_all = None # type: ignore[assignment] |