import json


def parse_line(header, line):
    """Parse one line of data from the message file.

    Each line is expected to contain chunk key - comma - tile key (CSV style).

    Args:
        header (dict): Data to join with contents of line to construct a full message.
        line (string): Contents of the line.

    Returns:
        (string): JSON encoded data ready for enqueuing.

    Raises:
        (RuntimeError): if less than 2 columns found on a line.
    """
    msg = {}
    msg['job_id'] = header['job_id']
    msg['upload_queue_arn'] = header['upload_queue_url']
    msg['ingest_queue_arn'] = header['ingest_queue_url']

    tokens = line.split(',')
    if len(tokens) < 2:
        raise RuntimeError('Bad message line encountered.')

    msg['chunk_key'] = tokens[0].strip()
    msg['tile_key'] = tokens[1].strip()

    return json.dumps(msg)