import yaml
from typing import OrderedDict


def ordered_safe_load(stream):
    """Load a YAML file using OrderedDict instead of dict."""

    class OrderedSafeLoader(yaml.SafeLoader):
        """Loader class that uses OrderedDict to load a map."""

    def construct_mapping(loader, node):
        """Load a map as an OrderedDict."""
        loader.flatten_mapping(node)
        return OrderedDict(loader.construct_pairs(node))

    OrderedSafeLoader.add_constructor(
        yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_mapping)

    return yaml.load(stream, OrderedSafeLoader)