"""TODO"""
import os, json

def safejsonload(path, fallback = {}):
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "a+") as json_file:
        pass

    data = fallback
    with open(path) as json_file:
        if os.path.getsize(path):
            data = json.load(json_file)

    return data

def saferead(path):
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path,"a+") as file:
        file.seek(0)
        return file.read()

def safejsondump(obj, path):
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "w+") as json_file:
        json.dump(obj, json_file, indent=4, sort_keys=True)

def safejsonloads(string):
     # HACK For some reason empty .jsons need to be loaded twice
    if not string:
        return ""
    else:
        data = json.loads(string)
        if isinstance(string, str):
            data = json.loads(string)

        return data
