""" Script that sorts warnings generated by doxygen to maintain consistent order. """
import sys

def read_by_line(file_path):
    with open(file_path, "r") as f:
        while line := f.readline():
            yield line[:-1]

def main():
    if len(sys.argv) != 3:
        print("Wrong number of arguments provided, expected two.")
        print("Usage: python3 sort_doxygen_warnings.py [input_file] [output_file]")
        sys.exit(1)
    warnings = []
    for line in read_by_line(sys.argv[1]):
        if "warning:" in line or "error:" in line:
            warnings.append(line)
            continue
        # Doxygen warnings can span multiple lines, keep these lines together.
        warnings[-1] = f"{warnings[-1]}\n{line}"
    warnings = sorted(set(warnings))
    with open(sys.argv[2], "w") as out:
        out.write("\n".join(warnings))
        out.write("\n")
    print("Doxygen warnings sorted successfully.")

if __name__ == "__main__":
    main()
