import yaml


def get_docker_compose_data_from_json_list(json_list):
    """Return the Yaml as a simplified structure from the json list:
    {
        image_name: {
            "container": container_name,
            "image_prefix": "mendersoftware/" or "someserver.mender.io/blahblah",
            "version": version,
        }
    }
    """
    data = {}
    for json_str in json_list:
        json_elem = yaml.safe_load(json_str)
        for container, cont_info in json_elem["services"].items():
            full_image = cont_info.get("image")
            if full_image is None or (
                "mendersoftware" not in full_image and "mender.io" not in full_image
            ):
                continue
            split = full_image.rsplit("/", 1)
            prefix = split[0]
            split = split[1].split(":", 1)
            image = split[0]
            ver = split[1]
            if data.get(image) is not None:
                raise Exception(
                    (
                        "More than one container is using the image name '%s'. "
                        + "The tool currently does not support this."
                    )
                    % image
                )
            data[image] = {
                "container": container,
                "image_prefix": prefix,
                "version": ver,
            }

    return data