From 4781c344d833ee173a2a7ed5487ea2f185c64f51 Mon Sep 17 00:00:00 2001 From: Sven Augustin Date: Wed, 15 Oct 2025 13:16:01 +0200 Subject: [PATCH] moved old/new filename check into FileHandler; properly None file/filename --- dap/accumulator.py | 9 +++------ dap/utils/filehandler.py | 9 ++++++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/dap/accumulator.py b/dap/accumulator.py index df37e3f..ac5ec8d 100644 --- a/dap/accumulator.py +++ b/dap/accumulator.py @@ -23,7 +23,6 @@ def main(): def accumulate(accumulator_host, accumulator_port): zmq_socks = ZMQSocketsAccumulator(accumulator_host, accumulator_port) - previous_run_name = None output = FileHandler() n_frames_received = 0 @@ -38,12 +37,10 @@ def accumulate(accumulator_host, accumulator_port): detector = results.get("detector_name", "") pulse_id = results.get("pulse_id", 0) - run_name = str(pulse_id // 10000 * 10000) - if run_name != previous_run_name: - previous_run_name = run_name - fname = f"{OUTPUT_DIR_NAME}/{detector}/{run_name}.dap" - output.switch(fname) + run_name = str(pulse_id // 10000 * 10000) + fname = f"{OUTPUT_DIR_NAME}/{detector}/{run_name}.dap" + output.switch(fname) res_is_good_frame = results.get("is_good_frame", -1) res_is_hit_frame = results.get("is_hit_frame", False) diff --git a/dap/utils/filehandler.py b/dap/utils/filehandler.py index 17699a9..2b15aac 100644 --- a/dap/utils/filehandler.py +++ b/dap/utils/filehandler.py @@ -4,20 +4,23 @@ import os class FileHandler: def __init__(self): - self.file = None + self.file = self.fname = None def switch(self, fname): - self.close() - self.open(fname) + if fname != self.fname: + self.close() + self.open(fname) def open(self, fname): dname = os.path.dirname(fname) os.makedirs(dname, exist_ok=True) self.file = open(fname, "a") + self.fname = fname def close(self): if self.file: self.file.close() + self.file = self.fname = None def flush(self): if self.file: