import re
import logging


def _add_core(key, json_results):
    """This function parses the benchmark name to add the core type to
    benchmark metadata.

    Args:
        key (str) - Key for dictionary for this benchmark results.

        json_results (dict) - Dictionary of all benchmark results
        (keyed by benchmark results filename) that the data and
        metadata from json_file are being added to.

    Returns:
        json_results (dict) - Dictionary of all benchmark results
        (keyed by benchmark results filename) that metadata from the
        benchmark name are being added to.
    """
    path = json_results[key]['path']
    match1 = re.search('-tcp-', path)
    match2 = re.search('-tcpss-', path)
    match3 = re.search('-udp-', path)
    match4 = re.search('-zmq-', path)
    match5 = re.search('-zmqss-', path)
    match6 = re.search('-mpi-', path)
    if match1:
        json_results[key]['core_type'] = 'tcp'
    elif match2:
        json_results[key]['core_type'] = 'tcpss'
    elif match3:
        json_results[key]['core_type'] = 'udp'
    elif match4:
        json_results[key]['core_type'] = 'zmq'
    elif match5:
        json_results[key]['core_type'] = 'zmqss'
    elif match6:
        json_results[key]['core_type'] = 'mpi'
    else:
        warn_str = 'Unable to find core type in {}; ' \
                        'setting to "unspecified"'.format(path)
        logging.warning(warn_str)
    return json_results