from config import *
import json

def loadData(metadataPath):
        data = []
        with open(metadataPath) as input_file:
            #for line in input_file:
            #        data.append(json.loads(line))
            data = json.load(input_file)
        return data

def isAllEntryOfSameDataType(input_list):
        first_entry_data_type = type(input_list[0])
        for entry in input_list:
                if first_entry_data_type != type(entry):
                        return False
        return True

def getDict(key_index, input_string):
        temp = dict()
        temp["key{}".format(key_index)] = input_string
        return temp

def convertAllEntryOfListToDict(input_list):
        for i in range(0, len(input_list)):
                if type(input_list[i]) != dict:
                        input_list[i] = getDict(i, input_list[i])
        return input_list

def checkEachEntry(data):
        if type(data) == list:
                if isAllEntryOfSameDataType(data) == False:
                        data = convertAllEntryOfListToDict(data)
        elif type(data) == dict:
                for index, key in enumerate(data):
                        data[key] = checkEachEntry(data[key])
        return data

def preProcessAllMetadata(metadataPath, newMetadataPath):
        data = loadData(metadataPath)
        for i in range(0, len(data)):
                data[i] = checkEachEntry(data[i])

        with open(newMetadataPath, 'w') as output_file:
                json.dump(data, output_file, indent=4)

if __name__ == "__main__":
        preProcessAllMetadata(METADATA_JSON_FILE_PATH,NEW_METADATA_JSON_FILE_PATH)


