[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

@@ -185,23 +185,18 @@ def main():
args.sub = extr.subcategory
args.base = extr.basecategory
path = util.path("test", "results", f"{args.cat}.py")
with util.open(path) as fp:
lines = fp.readlines()
LOG.info("Collecting data for '%s'", args.url)
result = generate_test_result(args)
LOG.info("Writing '%s' results to '%s'", args.url, util.trim(path))
path = util.path("test", "results", f"{args.cat}.py")
path_tr = util.trim(path)
LOG.info("Writing '%s' results to '%s'", args.url, path_tr)
with util.lines(path) as lines:
insert_test_result(args, result, lines)
with util.lazy(path) as fp:
fp.writelines(lines)
if args.git:
path = util.trim(path)
LOG.info("git add %s", path)
util.git("add", "--", path)
LOG.info("git add %s", path_tr)
util.git("add", "--", path_tr)
if __name__ == "__main__":

View File

@@ -47,12 +47,8 @@ def init_extractor(args):
LOG.info(util.trim(path))
if lines:
with util.open(path) as fp:
lines = fp.readlines()
if func(args, lines):
with util.lazy(path) as fp:
fp.writelines(lines)
else:
with util.lines(path) as lines:
if not func(args, lines):
LOG.warning("'%s' already present", category)
else:
try:

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)