X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=file_utils.py;h=91aeea072b03d94d670a13ca1a348b407d5734b8;hb=532df2c5b57c7517dfb3dddd8c1358fbadf8baf3;hp=deda45e033f6a5adb65a4b1d7725e2b346030797;hpb=7ff2af6fe7bffea90dc4a31c93140c189917c659;p=python_utils.git diff --git a/file_utils.py b/file_utils.py index deda45e..91aeea0 100644 --- a/file_utils.py +++ b/file_utils.py @@ -1,33 +1,35 @@ #!/usr/bin/env python3 +# © Copyright 2021-2022, Scott Gasch + """Utilities for working with files.""" +import contextlib import datetime import errno import glob import hashlib -import io import logging import os import pathlib import re import time from os.path import exists, isfile, join -from typing import List, Optional, TextIO +from typing import Callable, List, Literal, Optional, TextIO from uuid import uuid4 logger = logging.getLogger(__name__) -def remove_newlines(x): +def remove_newlines(x: str) -> str: return x.replace('\n', '') -def strip_whitespace(x): +def strip_whitespace(x: str) -> str: return x.strip() -def remove_hash_comments(x): +def remove_hash_comments(x: str) -> str: return re.sub(r'#.*$', '', x) @@ -35,15 +37,16 @@ def slurp_file( filename: str, *, skip_blank_lines=False, - line_transformers=[], + line_transformers: Optional[List[Callable[[str], str]]] = None, ): ret = [] if not file_is_readable(filename): raise Exception(f'{filename} can\'t be read.') with open(filename) as rf: for line in rf: - for transformation in line_transformers: - line = transformation(line) + if line_transformers is not None: + for transformation in line_transformers: + line = transformation(line) if skip_blank_lines and line == '': continue ret.append(line) @@ -190,7 +193,7 @@ def create_path_if_not_exist(path, on_error=None): >>> os.path.exists(path) True """ - logger.debug(f"Creating path {path}") + logger.debug("Creating path %s", path) previous_umask = os.umask(0) try: os.makedirs(path) @@ -347,9 +350,7 @@ def set_file_raw_atime_and_mtime(filename: str, ts: float = None): os.utime(filename, None) -def convert_file_timestamp_to_datetime( - filename: str, producer -) -> Optional[datetime.datetime]: +def convert_file_timestamp_to_datetime(filename: str, producer) -> Optional[datetime.datetime]: ts = producer(filename) if ts is not None: return datetime.datetime.fromtimestamp(ts) @@ -389,9 +390,7 @@ def get_file_mtime_age_seconds(filename: str) -> Optional[int]: return get_file_timestamp_age_seconds(filename, lambda x: x.st_mtime) -def get_file_timestamp_timedelta( - filename: str, extractor -) -> Optional[datetime.timedelta]: +def get_file_timestamp_timedelta(filename: str, extractor) -> Optional[datetime.timedelta]: age = get_file_timestamp_age_seconds(filename, extractor) if age is not None: return datetime.timedelta(seconds=float(age)) @@ -465,7 +464,12 @@ def get_files_recursive(directory: str): yield file_or_directory -class FileWriter(object): +class FileWriter(contextlib.AbstractContextManager): + """A helper that writes a file to a temporary location and then moves + it atomically to its ultimate destination on close. + + """ + def __init__(self, filename: str) -> None: self.filename = filename uuid = uuid4() @@ -477,14 +481,14 @@ class FileWriter(object): self.handle = open(self.tempfile, mode="w") return self.handle - def __exit__(self, exc_type, exc_val, exc_tb) -> Optional[bool]: + def __exit__(self, exc_type, exc_val, exc_tb) -> Literal[False]: 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 + raise Exception(f'{cmd} failed, exit value {ret>>8}!') + return False if __name__ == '__main__':