def yaml_to_ordered_dict(stream, loader=yaml.SafeLoader):
    """Provides yaml.load alternative with preserved dictionary order.

    Args:
        stream (string): YAML string to load.
        loader (:class:`yaml.loader`): PyYAML loader class. Defaults to safe
            load.

    Returns:
        OrderedDict: Parsed YAML.
    """
    class OrderedUniqueLoader(loader):
        """
        Subclasses the given pyYAML `loader` class.

        Validates all sibling keys to insure no duplicates.

        Returns an OrderedDict instead of a Dict.
        """

        # keys which require no duplicate siblings.
        NO_DUPE_SIBLINGS = ["stacks", "class_path"]
        # keys which require no duplicate children keys.
        NO_DUPE_CHILDREN = ["stacks"]

        def _error_mapping_on_dupe(self, node, node_name):
            """check mapping node for dupe children keys."""
            if isinstance(node, MappingNode):
                mapping = {}
                for n in node.value:
                    a = n[0]
                    b = mapping.get(a.value, None)
                    if b:
                        msg = "{} mapping cannot have duplicate keys {} {}"
                        raise ConstructorError(
                            msg.format(node_name, b.start_mark, a.start_mark)
                        )
                    mapping[a.value] = a

        def _validate_mapping(self, node, deep=False):
            if not isinstance(node, MappingNode):
                raise ConstructorError(
                    None, None,
                    "expected a mapping node, but found %s" % node.id,
                    node.start_mark)
            mapping = OrderedDict()
            for key_node, value_node in node.value:
                key = self.construct_object(key_node, deep=deep)
                try:
                    hash(key)
                except TypeError as exc:
                    raise ConstructorError(
                        "while constructing a mapping", node.start_mark,
                        "found unhashable key (%s)" % exc, key_node.start_mark
                    )
                # prevent duplicate sibling keys for certain "keywords".
                if key in mapping and key in self.NO_DUPE_SIBLINGS:
                    msg = "{} key cannot have duplicate siblings {} {}"
                    raise ConstructorError(
                        msg.format(key, node.start_mark, key_node.start_mark)
                    )
                if key in self.NO_DUPE_CHILDREN:
                    # prevent duplicate children keys for this mapping.
                    self._error_mapping_on_dupe(value_node, key_node.value)
                value = self.construct_object(value_node, deep=deep)
                mapping[key] = value
            return mapping

        def construct_mapping(self, node, deep=False):
            """Override parent method to use OrderedDict."""
            if isinstance(node, MappingNode):
                self.flatten_mapping(node)
            return self._validate_mapping(node, deep=deep)

        def construct_yaml_map(self, node):
            data = OrderedDict()
            yield data
            value = self.construct_mapping(node)
            data.update(value)

    OrderedUniqueLoader.add_constructor(
        u'tag:yaml.org,2002:map', OrderedUniqueLoader.construct_yaml_map,
    )
    return yaml.load(stream, OrderedUniqueLoader)
Provides yaml.load alternative with preserved dictionary order.

    Args:
        stream (string): YAML string to load.
        loader (:class:`yaml.loader`): PyYAML loader class. Defaults to safe
            load.

    Returns:
        OrderedDict: Parsed YAML.