X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=file_utils.py;h=f273ea4f9a5fe08da8c1f15e3108c65b7c33d0ea;hb=a4bf4d05230474ad14243d67ac7f8c938f670e58;hp=22210e4444afcb5223fbfcf70dcfe839baa15edd;hpb=0df075ce2ba86c529fe6fb73b4058c5cf20ff94c;p=python_utils.git diff --git a/file_utils.py b/file_utils.py index 22210e4..f273ea4 100644 --- a/file_utils.py +++ b/file_utils.py @@ -14,7 +14,7 @@ import time from typing import Optional import glob from os.path import isfile, join, exists -from typing import List +from typing import List, TextIO from uuid import uuid4 @@ -33,15 +33,18 @@ def remove_hash_comments(x): return re.sub(r'#.*$', '', x) -def read_file_to_list( - filename: str, *, skip_blank_lines=False, line_transformations=[] +def slurp_file( + filename: str, + *, + skip_blank_lines=False, + line_transformers=[], ): 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_transformations: + for transformation in line_transformers: line = transformation(line) if skip_blank_lines and line == '': continue @@ -329,11 +332,13 @@ def get_file_md5(filename: str) -> str: def set_file_raw_atime(filename: str, atime: float): mtime = get_file_raw_mtime(filename) + assert mtime os.utime(filename, (atime, mtime)) def set_file_raw_mtime(filename: str, mtime: float): atime = get_file_raw_atime(filename) + assert atime os.utime(filename, (atime, mtime)) @@ -431,8 +436,8 @@ def describe_file_mtime(filename: str, *, brief=False) -> Optional[str]: return describe_file_timestamp(filename, lambda x: x.st_mtime, brief=brief) -def touch_file(filename: str, *, mode: Optional[int] = 0o666) -> bool: - return pathlib.Path(filename, mode=mode).touch() +def touch_file(filename: str, *, mode: Optional[int] = 0o666): + pathlib.Path(filename, mode=mode).touch() def expand_globs(in_filename: str): @@ -467,14 +472,14 @@ class FileWriter(object): self.filename = filename uuid = uuid4() self.tempfile = f'{filename}-{uuid}.tmp' - self.handle = None + self.handle: Optional[TextIO] = None - def __enter__(self) -> io.TextIOWrapper: + def __enter__(self) -> TextIO: 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: + def __exit__(self, exc_type, exc_val, exc_tb) -> Optional[bool]: if self.handle is not None: self.handle.close() cmd = f'/bin/mv -f {self.tempfile} {self.filename}'