diff options
| author | Scott Gasch <[email protected]> | 2021-07-28 22:13:43 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2021-07-28 22:13:43 -0700 |
| commit | 4faa994d32223c8d560d9dad0ca90a3f7eb10d6a (patch) | |
| tree | 1200e5615d94247a120b98a4ddceb2cd6674e0af /file_utils.py | |
| parent | c79ecbf708a63a54a9c3e8d189b65d4794930082 (diff) | |
Money, Rate, CentCount and a bunch of bugfixes.
Diffstat (limited to 'file_utils.py')
| -rw-r--r-- | file_utils.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/file_utils.py b/file_utils.py index 464b0e7..525a1af 100644 --- a/file_utils.py +++ b/file_utils.py @@ -7,11 +7,14 @@ import errno import hashlib import logging import os +import io import pathlib import time from typing import Optional import glob from os.path import isfile, join, exists +from uuid import uuid4 + logger = logging.getLogger(__name__) @@ -249,3 +252,25 @@ def get_files_recursive(directory: str): for subdir in get_directories(directory): for file_or_directory in get_files_recursive(subdir): yield file_or_directory + + +class FileWriter(object): + def __init__(self, filename: str) -> None: + self.filename = filename + uuid = uuid4() + self.tempfile = f'{filename}-{uuid}.tmp' + self.handle = None + + def __enter__(self) -> io.TextIOWrapper: + assert not does_path_exist(self.tempfile) + self.handle = open(self.tempfile, mode="w") + return self.handle + + def __exit__(self, exc_type, exc_val, exc_tb) -> bool: + if self.handle is not None: + self.handle.close() + cmd = f'/bin/mv -f {self.tempfile} {self.filename}' + ret = os.system(cmd) + if (ret >> 8) != 0: + raise Exception(f'{cmd} failed, exit value {ret>>8}') + return None |
