file_helpers.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import os
  2. import tempfile
  3. import contextlib
  4. class CallbackReader:
  5. """Wraps a file, but overrides the read method to also
  6. call a callback function with the number of bytes read so far."""
  7. def __init__(self, f, callback, *args):
  8. self.f = f
  9. self.callback = callback
  10. self.cb_args = args
  11. self.total_read = 0
  12. def __getattr__(self, attr):
  13. return getattr(self.f, attr)
  14. def read(self, *args, **kwargs):
  15. chunk = self.f.read(*args, **kwargs)
  16. self.total_read += len(chunk)
  17. self.callback(*self.cb_args, self.total_read)
  18. return chunk
  19. @contextlib.contextmanager
  20. def atomic_write_in_dir(path: str, mode: str = 'w', buffering: int = -1, encoding: str = None, newline: str = None,
  21. overwrite: bool = False):
  22. """Write to a file atomically using a temporary file in the same directory as the destination file."""
  23. dir_name = os.path.dirname(path)
  24. if not overwrite and os.path.exists(path):
  25. raise FileExistsError(f"File '{path}' already exists. To overwrite it, set 'overwrite' to True.")
  26. with tempfile.NamedTemporaryFile(mode=mode, buffering=buffering, encoding=encoding, newline=newline, dir=dir_name, delete=False) as tmp_file:
  27. yield tmp_file
  28. tmp_file_name = tmp_file.name
  29. os.replace(tmp_file_name, path)