from io import BufferedWriter


class ProgressFileWriter(BufferedWriter):
    """
    opens a file and calls a given callback method on every write
    """

    def __init__(self, path, mode, callback=None):
        self.file_handler = open(path, mode)

        self.progress = 0
        self.callback = callback

        super().__init__(self.file_handler, 262144)

    def write(self, s):
        value = super().write(s)

        if self.callback is not None:
            self.progress += value
            self.callback(self.progress)

        return value
