"""Utilities for working with files."""
+import contextlib
import datetime
import errno
import glob
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)
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)
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.
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__':