summaryrefslogtreecommitdiff
path: root/ybd/morphdumper.py
blob: 3bbdf9878c0c25c89cb0f2217346dcb5cdbaf902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright (C) 2017  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-2 =*=
import yaml
from yaml.representer import SafeRepresenter


def morph_dump(data, defaults):
    """Dumps consistent YAML

    This serialization ensures utf8 encoding where needed, ensures
    that multiline strings are never escaped but nicely output and
    ensures a consistent output order dictated by ybd's defaults.conf.

    Args:
        data: The data to dump
        default: A Defaults object

    Returns:
        The data ready to write to a file
    """
    class MorphDumper(yaml.SafeDumper):

        # Output in the order dictated by defaults.conf
        keyorder = defaults.fields + defaults.build_steps

        def __init__(self, *args, **kwargs):

            yaml.SafeDumper.__init__(self, *args, **kwargs)

            # Ensure consistently ordered output
            self.add_representer(dict, self._represent_dict)

            # Use simple notation for one line strings, and
            # use '|' notation for multiline text, never stringify
            # and excape newlines.
            self.add_representer(str, self._represent_str)
            self.add_representer(unicode, self._represent_unicode)

        # Never emit aliases
        #
        # copied from http://stackoverflow.com/questions/21016220
        def ignore_aliases(self, data):
            return True

        def _iter_in_global_order(cls, mapping):
            for key in cls.keyorder:
                if key in mapping:
                    yield key, mapping[key]
            for key in sorted(mapping.iterkeys()):
                if key not in cls.keyorder:
                    yield key, mapping[key]

        def _represent_dict(cls, dumper, mapping):
            return dumper.represent_mapping('tag:yaml.org,2002:map',
                                            cls._iter_in_global_order(mapping))

        def _represent_str(cls, dumper, orig_data):
            try:
                data = unicode(orig_data, 'ascii')
                if data.count('\n') == 0:
                    return SafeRepresenter.represent_str(dumper, orig_data)
            except UnicodeDecodeError:
                try:
                    data = unicode(orig_data, 'utf-8')
                    if data.count('\n') == 0:
                        return SafeRepresenter.represent_str(dumper, orig_data)
                except UnicodeDecodeError:
                    return SafeRepresenter.represent_str(dumper, orig_data)
            return dumper.represent_scalar(u'tag:yaml.org,2002:str',
                                           data, style='|')

        def _represent_unicode(cls, dumper, data):
            if data.count('\n') == 0:
                return SafeRepresenter.represent_unicode(dumper, data)
            return dumper.represent_scalar(u'tag:yaml.org,2002:str',
                                           data, style='|')

    return yaml.dump(data,
                     default_flow_style=False,
                     encoding='utf-8',
                     line_break="\n",
                     Dumper=MorphDumper)