import json


def analyse_line_dataturk_format(line):
    """
    scans a json file line and returns the file name and label

    the format is specifically for the one returned by the dataturks labelling
    platform

    Args:
        line: string read from json file

    Returns:
        file_name: sting with name of the file in the json
        label: string with the identified value of label identified
    """
    line_dict = json.loads(line)
    file_name = line_dict["content"]
    file_name = "___".join(file_name.split("___")[1:])
    label = None
    if "annotation" in line_dict.keys():
        if line_dict["annotation"] is not None:
            if "labels" in line_dict["annotation"].keys():
                labels = line_dict["annotation"]["labels"]
                if len(labels) > 1:
                    print("more than one label contained in file {}".format(file_name))
                    #raise ValueError("more than one label contained in file {}".format(file_name))
                elif len(labels) == 0:
                    print("No label contained in file {}".format(file_name))
                    #raise ValueError("No label contained in file {}".format(file_name))
                else:
                    label =  labels[0]
            elif "label" in line_dict["annotation"].keys():
                labels = line_dict["annotation"]["label"]
                if len(labels) > 1:
                    print("more than one label contained in file {}".format(file_name))
                    #raise ValueError("more than one label contained in file {}".format(file_name))
                elif len(labels) == 0:
                    print("No label contained in file {}".format(file_name))
                    #raise ValueError("No label contained in file {}".format(file_name))
                label = labels[0]

    return(file_name, label)