From 24fd977613c93f7bab5e2fafc47e89af05eab6c8 Mon Sep 17 00:00:00 2001 From: Zeecka Date: Thu, 1 Jan 2026 23:32:10 +0100 Subject: [PATCH] clean utils --- aperisolve/analyzers/utils.py | 51 ++--------------------------------- 1 file changed, 2 insertions(+), 49 deletions(-) diff --git a/aperisolve/analyzers/utils.py b/aperisolve/analyzers/utils.py index 3387106..a2ee0d0 100644 --- a/aperisolve/analyzers/utils.py +++ b/aperisolve/analyzers/utils.py @@ -3,56 +3,11 @@ # mypy: disable-error-code=unused-awaitable """Utility functions for analyzers modules.""" -import fcntl -import json import math -import os import struct -import threading import zlib from functools import cached_property -from pathlib import Path -from typing import Any, Iterable, Self - -_thread_lock = threading.Lock() - -MAX_PENDING_TIME = int(os.getenv("MAX_PENDING_TIME", "600")) # 10 minutes by default - - -def update_data( - output_dir: Path, new_data: dict[Any, Any], json_filename: str = "results.json" -) -> None: - """Thread-safe and process-safe JSON update using lock file and atomic replace.""" - json_file = Path(output_dir) / json_filename - lock_file = json_file.with_suffix(json_file.suffix + ".lock") - tmp_file = json_file.with_suffix(json_file.suffix + ".tmp") - - json_file.parent.mkdir(parents=True, exist_ok=True) - - with _thread_lock: # synchronizes across threads - with open(lock_file, "w", encoding="utf-8") as lock: - fcntl.flock(lock, fcntl.LOCK_EX) # synchronizes across processes - - try: - # Read existing JSON - data: dict[Any, Any] = {} - if json_file.exists(): - try: - with open(json_file, "r", encoding="utf-8") as f: - data = json.load(f) - except json.JSONDecodeError: - data = {} - - # Update with new data - data.update(new_data) - - # Write safely to a temp file - with open(tmp_file, "w", encoding="utf-8") as f: - json.dump(data, f, sort_keys=False) - - os.replace(tmp_file, json_file) # ensures file write is atomic - finally: - fcntl.flock(lock, fcntl.LOCK_UN) +from typing import Iterable, Self def get_resolutions() -> list[tuple[int, int]]: @@ -291,9 +246,7 @@ class PNG: crc = struct.unpack(">I", data[offset : offset + 4])[0] # Unpack IHDR fields - width, height, depth, color, _, _, interlace = struct.unpack( - ">IIBBBBB", ihdr_data - ) + width, height, depth, color, _, _, interlace = struct.unpack(">IIBBBBB", ihdr_data) return cls( size=(width, height),