Change locking boundaries for shared dict. Add a unit test.
[python_utils.git] / file_utils.py
index e40a9f5fdc2de049cb96b8f63a16541df732ffb7..cd37f3069c70efd5c0f835e3362adbdf18d52e24 100644 (file)
@@ -2,7 +2,6 @@
 
 """Utilities for working with files."""
 
-from dataclasses import dataclass
 import datetime
 import errno
 import hashlib
@@ -10,6 +9,7 @@ import logging
 import os
 import io
 import pathlib
+import re
 import time
 from typing import Optional
 import glob
@@ -21,6 +21,37 @@ from uuid import uuid4
 logger = logging.getLogger(__name__)
 
 
+def remove_newlines(x):
+    return x.replace('\n', '')
+
+
+def strip_whitespace(x):
+    return x.strip()
+
+
+def remove_hash_comments(x):
+    return re.sub(r'#.*$', '', x)
+
+
+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_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.
@@ -185,6 +216,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.
 
@@ -249,9 +292,7 @@ def is_same_file(file1: str, file2: str) -> bool:
 
 
 def get_file_raw_timestamps(filename: str) -> Optional[os.stat_result]:
-    """Stats the file and returns an os.stat_result or None on error.
-
-    """
+    """Stats the file and returns an os.stat_result or None on error."""
     try:
         return os.stat(filename)
     except Exception as e:
@@ -279,9 +320,7 @@ def get_file_raw_ctime(filename: str) -> Optional[float]:
 
 
 def get_file_md5(filename: str) -> str:
-    """Hashes filename's contents and returns an MD5.
-
-    """
+    """Hashes filename's contents and returns an MD5."""
     file_hash = hashlib.md5()
     with open(filename, "rb") as f:
         chunk = f.read(8192)
@@ -371,10 +410,9 @@ def get_file_mtime_timedelta(filename: str) -> Optional[datetime.timedelta]:
     return get_file_timestamp_timedelta(filename, lambda x: x.st_mtime)
 
 
-def describe_file_timestamp(
-    filename: str, extractor, *, brief=False
-) -> Optional[str]:
+def describe_file_timestamp(filename: str, extractor, *, brief=False) -> Optional[str]:
     from datetime_utils import describe_duration, describe_duration_briefly
+
     age = get_file_timestamp_age_seconds(filename, extractor)
     if age is None:
         return None
@@ -451,4 +489,5 @@ class FileWriter(object):
 
 if __name__ == '__main__':
     import doctest
+
     doctest.testmod()