From af3ce54440950a1d089c1c9f40fa699ee8cba1a7 Mon Sep 17 00:00:00 2001 From: vertighel Date: Thu, 29 May 2025 19:39:48 +0200 Subject: [PATCH] Again pause, resume etc --- noctua/sequencer.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/noctua/sequencer.py b/noctua/sequencer.py index 57df988..40697d6 100755 --- a/noctua/sequencer.py +++ b/noctua/sequencer.py @@ -34,9 +34,8 @@ class Sequencer(): self.params = {} # The parameters of the running template self.quitting = False self.embedded = embedded # To manage sys.exit if called alone - self.original_sigint = signal.getsignal( - signal.SIGINT) # Store original signal.signal(signal.SIGINT, self.interrupt) + self.original_sigint = signal.getsignal(signal.SIGINT) # Store original def load_script(self, template_script_path, params={}): '''Load a python file where the template is implemented''' @@ -186,23 +185,22 @@ class Sequencer(): datetime.utcnow() - now).total_seconds():.3f}s") - def interrupt(self): + def interrupt(self, signum, frame): '''Intercept a CTRL+C instead of raising a KeyboardInterrupt exception in order to be able to operate on the template class, for example to modify a pause attribute or call a method. ''' + # Temporarily restore original handler to avoid nested inputs if Ctrl+C # is hit again - signal.signal(signal.SIGINT, self.original_sigint) + # signal.signal(signal.SIGINT, self.original_sigint) try: - # Use a more robust way to get input that doesn't interfere with - # logging as much + # THIS IS WHERE THE MENU SHOULD BE PRINTED sys.stderr.write("\n--- INTERRUPT ---\n") - sys.stderr.write( - "(P)ause, (R)esume, (T)ry again paragraph, (N)ext template, (Q)uit:\n") + sys.stderr.write("(P)ause, (R)esume, (T)ry again paragraph, (N)ext template, (Q)uit:\n") sys.stderr.flush() - answer = sys.stdin.readline().strip().lower() # Read directly from stdin + answer = sys.stdin.readline().strip().lower() if answer.startswith('p'): self.pause() @@ -226,10 +224,14 @@ class Sequencer(): os._exit(1) # Force exit if not embedded else: self.quit() # Attempt graceful quit if embedded + # except Exception as e: # Add a broad catch here for debugging + # sys.stderr.write(f"ERROR IN INTERRUPT HANDLER: {type(e).__name__}: {e}\n") + # log.error("Error in interrupt handler", exc_info=True) + # # Fallback to quitting if the handler itself fails + # self.quit() finally: - # Re-hook our custom interrupt handler - signal.signal(signal.SIGINT, self.interrupt) - + signal.signal(signal.SIGINT, self.interrupt) # Re-hook + def pause(self): ''' Put the pause attribute in the running template to True. -- GitLab