[scripts/util] add 'lines()' helper

This commit is contained in:
Mike Fährmann
2025-08-23 18:29:23 +02:00
parent 1787478d6b
commit 57a4b5b5b1
3 changed files with 28 additions and 19 deletions

View File

@@ -64,3 +64,21 @@ class lazy():
else:
# only update atime and mtime
os.utime(self.path)
class lines():
def __init__(self, path, lazy=True):
self.path = path
self.lazy = lazy
self.lines = ()
def __enter__(self):
with open(self.path) as fp:
self.lines = lines = fp.readlines()
return lines
def __exit__(self, exc_type, exc_value, traceback):
ctx = lazy(self.path) if self.lazy else open(self.path, "w")
with ctx as fp:
fp.writelines(self.lines)