From 3bb6404859d6527f5800fdda40b0718fdab2e3a7 Mon Sep 17 00:00:00 2001 From: Danilo Horta Date: Fri, 17 May 2024 12:52:38 +0100 Subject: [PATCH] Add context manager to Progress class --- control/deciphonctl/progress.py | 7 +++++++ control/deciphonctl/scanner.py | 16 ++++++++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/control/deciphonctl/progress.py b/control/deciphonctl/progress.py index b8f0b77..914d9b9 100644 --- a/control/deciphonctl/progress.py +++ b/control/deciphonctl/progress.py @@ -36,3 +36,10 @@ def stop(self): self._continue.set() self._thread.join() self._logger.stop() + + def __enter__(self): + self.start() + return self + + def __exit__(self, *_): + self.stop() diff --git a/control/deciphonctl/scanner.py b/control/deciphonctl/scanner.py index 14f202b..7940a6a 100644 --- a/control/deciphonctl/scanner.py +++ b/control/deciphonctl/scanner.py @@ -62,18 +62,18 @@ def callback(self, message: str): ) logger.info(f"scan parameters: {params}") scan = Scan(params, db) - with scan: - bar = Progress("scan", scan, self._sched, x.job_id) - bar.start() + with scan, Progress("scan", scan, self._sched, x.job_id): scan.dial(daemon.port) for seq in x.seqs: scan.add(Sequence(seq.id, seq.name, seq.data)) scan.run(snap) - bar.stop() - logger.info( - "Scan has finished successfully and " - f"results stored in '{snap.path}'." - ) + if scan.interrupted(): + raise InterruptedError("Scanner has been interrupted.") + snap.make_archive() + logger.info( + "Scan has finished successfully and " + f"results stored in '{snap.path}'." + ) self._sched.snap_post(x.id, snap.path)