Completed
Pull Request — develop (#448)
by
unknown
37s
created

get_yaml()   A

Complexity

Conditions 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
1
# -*- coding: utf-8 -*-
2
from __future__ import absolute_import, division, print_function, unicode_literals
3
4
from .decorators import memoize
5
6
7
@memoize
8
def get_yaml():
9
    """Tries to import the YAML library."""
10
    try:
11
        import ruamel_yaml as yaml
12
    except ImportError:                                         # pragma: no cover
13
        try:                                                    # pragma: no cover
14
            import ruamel.yaml as yaml                          # pragma: no cover
15
        except ImportError:                                     # pragma: no cover
16
            raise ImportError("No yaml library available.\n"    # pragma: no cover
17
                              "To proceed, conda install "      # pragma: no cover
18
                              "ruamel_yaml")                    # pragma: no cover
19
    return yaml
20
21
22
def yaml_load(stream):
23
    """Loads a dictionary from a stream"""
24
    yaml = get_yaml()
25
    return yaml.load(stream, Loader=yaml.RoundTripLoader, version="1.2")
26
27
28
def yaml_dump(data, stream=None):
29
    """Dumps an object to a YAML string"""
30
    yaml = get_yaml()
31
    return yaml.dump(data, stream=stream, Dumper=yaml.RoundTripDumper,
32
                     block_seq_indent=2, default_flow_style=False,
33
                     indent=2)
34