import re


def extract_image_locations(error_string):
    """Regex based function to extract image file locations.

    Parameters
    ----------
    error_string : String
        The input string having file locations of 'Actual', 'Expected' and
        'Difference' plots. This string is generated by yt's answer-testing
        plugin, when the plot generated in the test does not match to its
        golden answer image.

    Returns
    -------
    dict
        If the `error_string` is successfully parsed to extract plot locations,
        then a dictionary with the keys 'Actual', 'Expected','Difference' and
        values having corresponding plot file locations is returned.
        eg. {'Actual': '/usr/tmp/tmp43la9b0w.png',
             'Expected': '/usr/tmp/tmpbpaqbgi3.png',
             'Difference': '/usr/tmp/tmp43la9b0w-failed-diff.png'}
    None
        When `error_string` does not conform to yt's answer-testing error
        message, which has the information for plot file locations on disk.

    """
    unknown_failure = False
    base_regex = r"\s*\n\s*(.*?.png)"
    img_regex = {
        "Actual": "Actual:" + base_regex,
        "Expected": "Expected:" + base_regex,
        "Difference": "Difference:" + base_regex,
    }
    img_path = {}
    for key in img_regex:
        result = re.search(img_regex[key], error_string, re.MULTILINE)
        if not result:
            unknown_failure = True
            break
        # store the locations of actual, expected and diff plot files
        img_path[key] = result.group(1)

    if not unknown_failure:
        return img_path
    return None