import json


def remove_dojo_report(path):
    """
    Remove the `DOJO_REPORT` section from the pseudopotential file.
    Write new file. Return dict with old_report, None if error.
    """
    # Read lines from file and insert jstring between the tags.
    with open(path, "rt") as fh:
        lines = fh.readlines()
        try:
            start = lines.index("<DOJO_REPORT>\n")
        except ValueError:
            start = -1

        if start == -1: return None
        stop = lines.index("</DOJO_REPORT>\n")

        report = json.loads("\n".join(lines[start+1:stop]))
        del lines[start:stop+1]

    # Write new file.
    with open(path, "w") as fh:
        fh.writelines(lines)

    return report