Since this thing is on the innerwebs I suppose it should have a
[python_utils.git] / file_utils.py
index 9fa8fd209264ec44378adff464904c8bc900b10d..91aeea072b03d94d670a13ca1a348b407d5734b8 100644 (file)
@@ -1,26 +1,58 @@
 #!/usr/bin/env python3
 
+# © Copyright 2021-2022, Scott Gasch
+
 """Utilities for working with files."""
 
-from dataclasses import dataclass
+import contextlib
 import datetime
 import errno
+import glob
 import hashlib
 import logging
 import os
-import io
 import pathlib
+import re
 import time
-from typing import Optional
-import glob
-from os.path import isfile, join, exists
-from typing import List
+from os.path import exists, isfile, join
+from typing import Callable, List, Literal, Optional, TextIO
 from uuid import uuid4
 
-
 logger = logging.getLogger(__name__)
 
 
+def remove_newlines(x: str) -> str:
+    return x.replace('\n', '')
+
+
+def strip_whitespace(x: str) -> str:
+    return x.strip()
+
+
+def remove_hash_comments(x: str) -> str:
+    return re.sub(r'#.*$', '', x)
+
+
+def slurp_file(
+    filename: str,
+    *,
+    skip_blank_lines=False,
+    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:
+            if line_transformers is not None:
+                for transformation in line_transformers:
+                    line = transformation(line)
+            if skip_blank_lines and line == '':
+                continue
+            ret.append(line)
+    return ret
+
+
 def remove(path: str) -> None:
     """Deletes a file.  Raises if path refers to a directory or a file
     that doesn't exist.
@@ -161,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)
@@ -185,6 +217,18 @@ def does_file_exist(filename: str) -> bool:
     return os.path.exists(filename) and os.path.isfile(filename)
 
 
+def file_is_readable(filename: str) -> bool:
+    return does_file_exist(filename) and os.access(filename, os.R_OK)
+
+
+def file_is_writable(filename: str) -> bool:
+    return does_file_exist(filename) and os.access(filename, os.W_OK)
+
+
+def file_is_executable(filename: str) -> bool:
+    return does_file_exist(filename) and os.access(filename, os.X_OK)
+
+
 def does_directory_exist(dirname: str) -> bool:
     """Returns True if a file exists and is a directory.
 
@@ -289,11 +333,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 is not None
     os.utime(filename, (atime, mtime))
 
 
 def set_file_raw_mtime(filename: str, mtime: float):
     atime = get_file_raw_atime(filename)
+    assert atime is not None
     os.utime(filename, (atime, mtime))
 
 
@@ -304,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)
@@ -346,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))
@@ -391,8 +433,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):
@@ -422,26 +464,31 @@ 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()
         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) -> 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__':