mirror of https://github.com/ansible/ansible.git
Abstract away libyaml details into yaml utils (#72607)
* Add yaml utils file and use it * Linting fix and missing import * Abstract a few more details * Parser imports * Don't use CSafeDumper in AnsibleDumper * Move and use convert_yaml_objects_to_native when libyaml is present * yaml_load and yaml_dump, instead of safe_* * re-use HAS_LIBYAML from utils.yaml * add changelog fragment * Address recent changes * Use representer instead of recursive type converter * Restore needed import * move yaml utils to module_utils * Properly guard imports * Update from_yaml(_all)? to handle text wrappers with CSafeLoader * Use yaml utils for legacy_collection_loader * Add HAS_YAML, and ignore pylint issue * oops * GPL->BSDpull/74377/head
parent
6418f368e3
commit
bf7d4ce260
@ -0,0 +1,2 @@
|
||||
minor_changes:
|
||||
- YAML parsing - Create common utils for loading and dumping YAML that prefer the C extensions if available
|
@ -0,0 +1,42 @@
|
||||
# (c) 2020 Matt Martz <matt@sivel.net>
|
||||
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
|
||||
|
||||
"""
|
||||
This file provides ease of use shortcuts for loading and dumping YAML,
|
||||
preferring the YAML compiled C extensions to reduce duplicated code.
|
||||
"""
|
||||
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
from functools import partial as _partial
|
||||
|
||||
HAS_LIBYAML = False
|
||||
try:
|
||||
import yaml as _yaml
|
||||
except ImportError:
|
||||
HAS_YAML = False
|
||||
SafeLoader = None
|
||||
SafeDumper = None
|
||||
Parser = None
|
||||
yaml_load = None
|
||||
yaml_load_all = None
|
||||
yaml_dump = None
|
||||
yaml_dump_all = None
|
||||
else:
|
||||
HAS_YAML = True
|
||||
try:
|
||||
SafeLoader = _yaml.CSafeLoader
|
||||
SafeDumper = _yaml.CSafeDumper
|
||||
Parser = _yaml.cyaml.CParser
|
||||
HAS_LIBYAML = True
|
||||
except AttributeError:
|
||||
SafeLoader = _yaml.SafeLoader
|
||||
SafeDumper = _yaml.SafeDumper
|
||||
Parser = _yaml.parser.Parser
|
||||
|
||||
yaml_load = _partial(_yaml.load, Loader=SafeLoader)
|
||||
yaml_load_all = _partial(_yaml.load_all, Loader=SafeLoader)
|
||||
|
||||
yaml_dump = _partial(_yaml.dump, Dumper=SafeDumper)
|
||||
yaml_dump_all = _partial(_yaml.dump_all, Dumper=SafeDumper)
|
Loading…
Reference in New Issue