Commit af3ce544 authored by vertighel's avatar vertighel
Browse files

Again pause, resume etc

parent 48dde1ae
Loading
Loading
Loading
Loading
Loading
+14 −12
Original line number Diff line number Diff line
@@ -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,9 +224,13 @@ 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):
        '''