"""
This code was taken from https://www.geeksforgeeks.org/make-notepad-using-tkinter/ which was a simple
notepad like editor. I then modified it to act like a debugger for test PLCs. I have minimized the
changes to the original code.
"""
import time
# profile import cProfile
import os.path
from datetime import datetime
import traceback
import io
import openpyxl
from openpyxl.utils import get_column_letter
from tkinter import messagebox
from tkinter.messagebox import showinfo
from tkinter.filedialog import *
import epics
from epics import caget
from playsound import playsound, PlaysoundException  # used to play sound files to indicate when the test is done.
import sys

import globalVariables
from errorHandler import outputError, testPrint, windows, indentLevelInc
from globalVariables import partDict, pinDict, listDict, routineDict, parts, maxSamples, sampleTime
from partClass import PartSafetyToAC, PartEpicsToAC, PartACtoSafety, PartACtoEpics, \
    PartIO, setAllExternalInputs
from pinClass import PinIOType
from listClass import List
from routineClass import Routine, readWorksheet
from variableClass import Variable
from userFormClass import UserForm


globalVariables.debugging = True  # True if we are executing lines in the debugger window and
#                                   False if we doing a full fledged
# test from scratch. Used to determine whether test output should be written to the debug window.


class EditDebug:

    def __init__(self, **kwargs):
        self.root = Tk()
        self._bigDot = u"\u26AB"  # unicode character used for a breakpoint. It makes a big solid dot.
        self.root.report_callback_exception = self.report_callback_exception
        self._fileName = ''  # The name of the Excel file (including directory path) we will read

        # default window width and height
        self._thisWidth = 300
        self._thisHeight = 300
        self._chosenTestName = None  # The name of the part (logic block) we have chosen to test
        self.testNameBeingRun = None  # The name of the main routine (not subroutine) we are currently running.
        self._steppingWithDebugger = False  # Set true when we are stepping through commands with the debugger.
        #                                Used to avoid calls to update the Tk screen.
        self._breakOnError = BooleanVar()  # Indicates whether we should break on error
        self._breakOnError.set(True)
        self._haveRealHardware = BooleanVar()  # Indicates whether we have real hardware
        self._haveRealHardware.set(False)
        self._haveACPLC = BooleanVar()  # Indicates whether we have Access Control PLC
        self._haveACPLC.set(False)
        self._pauseForEPICSCheck = BooleanVar()  # Indicates whether we should pause so user can check EPICS displays
        self._pauseForEPICSCheck.set(False)
        self._loopUntilError = BooleanVar()  # Indicates whether we should loop until error
        self._loopUntilError.set(False)
        self._examineInputsWindow = None  # pointer to window that shows all the inputs and variables
        self._examineOutputsWindow = None  # pointer to window that shows all the outputs
        self._pauseForEPICSCheckWindow = None  # pointer to window displayed when user should check EPICS display
        self._examineRefreshCount = 0  # Count of the number of times we have updated the examine logic block
        #                                since we last executed a line
        # self._frmContent = Frame(_root)
        self._thisTextArea = Text(self.root, width=40, height=20, borderwidth=5, relief='sunken', wrap=WORD)
        self._thisTextArea.tag_configure('highlightLine', background='yellow')
        self._thisTextArea.tag_configure('breakpoint', foreground='red')
        self._thisTextArea.tag_configure('bold', font=self._thisTextArea.cget("font") + " 10 bold")
        self._thisMenuBar = Menu(self.root)
        self._thisFileMenu = Menu(self._thisMenuBar, tearoff=0)
        self._thisEditMenu = Menu(self._thisMenuBar, tearoff=0)
        self._thisViewMenu = Menu(self._thisMenuBar, tearoff=0)
        self._thisRunMenu = Menu(self._thisMenuBar, tearoff=0)
        self._thisHelpMenu = Menu(self._thisMenuBar, tearoff=0)
        self._testListMenu = Menu(self._thisRunMenu, tearoff=0)

        self._frmButton = Frame(self.root, borderwidth=0, relief="sunken")

        # To add scrollbar
        self._thisScrollBar = Scrollbar(self._thisTextArea)
        self._file = None

        self._lineNumber = 0  # The line number we just executed. Note line numbers start from 1, not 0
        self._previousLineNumber = 0  # The previous value of line number (saved so we can remove the highlighting)

        # Set icon
        try:
            self.root.wm_iconbitmap("Notepad.ico")
        except TclError:
            pass

        # Set window size (the default is 300x300)

        try:
            self._thisWidth = kwargs['width']
        except KeyError:
            pass

        try:
            self._thisHeight = kwargs['height']
        except KeyError:
            pass

        # Set the window text
        self.root.title("Untitled - testPPS")
        self.root.lift()
        self.root.attributes('-topmost', 1)
        self.root.attributes('-topmost', 0)
        # Center the window
        screenWidth = self.root.winfo_screenwidth()
        screenHeight = self.root.winfo_screenheight()

        # For left-align
        left = (screenWidth / 2) - (self._thisWidth / 2)

        # For right-align
        top = (screenHeight / 2) - (self._thisHeight / 2)

        # For top and bottom
        self.root.geometry('%dx%d+%d+%d' % (self._thisWidth,
                                            self._thisHeight,
                                            left, top))

        # To make the text area auto resizable
        self.root.grid_rowconfigure(0, weight=1)
        self.root.grid_columnconfigure(0, weight=0)
        self.root.grid_columnconfigure(1, weight=1)

        # Add controls (widget)
        # self._frmContent.grid(column=0, row=0)
        self._frmButton.grid(column=0, row=0, sticky=N + W)
        self._thisTextArea.grid(column=1, row=0, sticky=N + E + S + W)

        self._btnRun = Button(self._frmButton, text='Run\nfrom\nBeginning', command=self._run,
                              borderwidth=5, relief="raised")
        self._btnAbort = Button(self._frmButton, text='Abort\nTest', command=self._abortTest,
                                borderwidth=5, relief="raised")
        self._btnContinue = Button(self._frmButton, text='Continue', command=self._continue,
                                   borderwidth=5, relief="raised")
        self._btnBreakpoint = Button(self._frmButton, text='Breakpoint', command=self._breakpoint,
                                     borderwidth=5, relief="raised")
        self._btnStep = Button(self._frmButton, text='Step', command=self._step,
                               borderwidth=5, relief="raised")
        self._btnStepOver = Button(self._frmButton, text='Step\nOver', command=self._stepOver,
                                   borderwidth=5, relief="raised")
        self._btnExecuteCursor = Button(self._frmButton, text='Execute\nLine\nwith\nCursor',
                                        command=self._executeCursor,
                                        borderwidth=5, relief="raised")
        self._btnBreakOnError = Checkbutton(self._frmButton, text='Break\non Error',
                                            command=self._breakOnErrorFunction,
                                            variable=self._breakOnError, onvalue=True, offvalue=False)
        self._btnHaveRealHardware = Checkbutton(self._frmButton, text='Have real\nHardware',
                                                command=self._haveRealHardwareFunction,
                                                variable=self._haveRealHardware, onvalue=True, offvalue=False)
        self._btnHaveACPLC = Checkbutton(self._frmButton, text='Have Access\nControl PLC',
                                         command=self._haveACPLCFunction,
                                         variable=self._haveACPLC, onvalue=True, offvalue=False)
        self._btnPauseForEPICSCheck = Checkbutton(self._frmButton, text='Pause for\nEPICS display\nchecks',
                                                  command=self._pauseForEPICSCheckFunction,
                                                  variable=self._pauseForEPICSCheck, onvalue=True, offvalue=False)
        self._btnLoopUntilError = Checkbutton(self._frmButton, text='Loop\nUntil Error',
                                              command=self._loopUntilErrorFunction,
                                              variable=self._loopUntilError, onvalue=True, offvalue=False)
        self._btnRun.grid(column=0, row=0, sticky=E + W)
        self._btnContinue.grid(column=0, row=1, sticky=E + W)
        self._btnBreakpoint.grid(column=0, row=2, sticky=E + W)
        self._btnStep.grid(column=0, row=3, sticky=E + W)
        self._btnStepOver.grid(column=0, row=4, sticky=E + W)
        self._btnExecuteCursor.grid(column=0, row=5, sticky=E + W)
        self._btnAbort.grid(column=0, row=6, sticky=E + W)
        self._btnBreakOnError.grid(column=0, row=7, sticky=E + W)
        self._btnLoopUntilError.grid(column=0, row=8, sticky=E + W)
        self._btnHaveRealHardware.grid(column=0, row=9, sticky=E + W)
        self._btnHaveACPLC.grid(column=0, row=10, sticky=E + W)
        self._btnPauseForEPICSCheck.grid(column=0, row=11, sticky=E + W)

        #
        # define the "file" menu-+

        self._thisFileMenu.add_command(label="New",
                                       command=self._newFile)
        self._thisFileMenu.add_command(label="Open",
                                       command=self._openFile)
        self._thisFileMenu.add_command(label="Save",
                                       command=self._saveFile)
        self._thisFileMenu.add_command(label="Save As",
                                       command=self._saveAsFile)
        # To create a line in the dialog
        self._thisFileMenu.add_separator()
        self._thisFileMenu.add_command(label="Exit",
                                       command=self._quitApplication)
        self._thisMenuBar.add_cascade(label="File",
                                      menu=self._thisFileMenu)

        #
        # define the "edit" menu

        self._thisEditMenu.add_command(label="Cut",
                                       command=self._cut)
        self._thisEditMenu.add_command(label="Copy",
                                       command=self._copy)
        self._thisEditMenu.add_command(label="Paste",
                                       command=self._paste)
        self._thisMenuBar.add_cascade(label="Edit",
                                      menu=self._thisEditMenu)

        #
        # define the "view" menu

        self._thisViewMenu.add_command(label="Test inputs and Variables",
                                       command=self._createExamineInputsWindow)
        self._thisViewMenu.add_command(label="Test outputs",
                                       command=self._createExamineOutputsWindow)
        self._thisMenuBar.add_cascade(label="View",
                                      menu=self._thisViewMenu)

        #
        # define the "run" menu that allows various tests to be run

        self._thisRunMenu.add_command(label="Init from spreadsheet ...",
                                      command=self._initFromSpreadsheet)
        self._thisRunMenu.add_command(label="Run all tests",
                                      command=lambda: self._runAllTests())
        self._thisRunMenu.add_cascade(label="Choose single test to run",
                                      menu=self._testListMenu)
        self._thisRunMenu.add_command(label="Run a single test",
                                      command=lambda: self._RunSingleTest())
        self._thisMenuBar.add_cascade(label="Run",
                                      menu=self._thisRunMenu)

        #
        # define the testList submenu. It is dynamic as it depends on the spreadsheet that was read in.

        for thisRoutineName in routineDict:  # loop over all routines
            thisRoutine = routineDict[thisRoutineName]
            if not thisRoutine.isSubroutine:  # This is a main test routine, not a subroutine
                self._testListMenu.add_command(label=thisRoutineName,
                                               command=lambda name=thisRoutineName: self._chooseTest(name))

        #
        # define the "help" menu

        self._thisHelpMenu.add_command(label="About testPPS",
                                       command=self._showAbout)
        self._thisMenuBar.add_cascade(label="Help",
                                      menu=self._thisHelpMenu)

        self.root.config(menu=self._thisMenuBar)

        self._thisScrollBar.pack(side=RIGHT, fill=Y)

        # Scrollbar will adjust automatically according to the content
        self._thisScrollBar.config(command=self._thisTextArea.yview)
        self._thisTextArea.config(yscrollcommand=self._thisScrollBar.set)

    def report_callback_exception(self, exc, val, tb2):

        # Handles unexpected exceptions to give a traceback and print variables
        err = traceback.format_exception(exc, val, tb2)
        testPrint(0, '')
        testPrint(0, 'There was an unhandled exception. Traceback information is below. Save it and inform the '
                     'software engineer responsible for this software.')
        for line in err:
            testPrint(0, line)

        self._thisTextArea.see('end')  # scroll to make this line visible
        messagebox.showerror('Unhandled Exception. Give this output to author of program', err)
        self._disableUI(False)  # In case the exception happened during a long test re-enable GUI elements

    def _disableUI(self, disable: bool):
        """
        This routine will disable or re-enable GUI elements that should be disabled when the program
        is doing something that takes along time and shouldn't be interrupted like running a test.
        :param disable: If true, disable UI elements, if false, re-enable them
        :return:
        """
        if disable:
            state = 'disabled'
        else:
            state = 'normal'

        self._btnRun['state'] = state
        self._btnContinue['state'] = state
        self._btnBreakpoint['state'] = state
        self._btnStep['state'] = state
        self._btnStepOver['state'] = state
        self._btnExecuteCursor['state'] = state
        self._thisMenuBar.entryconfig('Run', state=state)
        self._thisMenuBar.entryconfig('File', state=state)

    def _quitApplication(self):
        self.root.destroy()

    # exit()

    def adjustTestMenu(self):
        """
        This routine should be called after initialization from the spreadsheet. It will populate the menu
        with the list of all logic blocks
        :return:
        """
        # first delete any old menu items
        self._testListMenu.delete(0, 'end')

        for thisRoutineName in routineDict:  # loop over all routines
            thisRoutine = routineDict[thisRoutineName]
            if not thisRoutine.isSubroutine:  # This is a main test routine, not a subroutine
                self._testListMenu.add_command(label=thisRoutineName,
                                               command=lambda name=thisRoutineName: self._chooseTest(name))

    def _breakOnErrorFunction(self):
        """
        This routine is called when someone checks or un-checks the break on error checkbox.
        All it does is save the checkbox variable to a global variable
        :return:
        """
        globalVariables.breakOnError = self._breakOnError.get()

    def _haveRealHardwareFunction(self):
        """
        This routine is called when someone checks or un-checks the have real hardware checkbox.
        All it does is save the checkbox variable to a global variable
        :return:
        """
        globalVariables.haveRealHardware = self._haveRealHardware.get()

    def _haveACPLCFunction(self):
        """
        This routine is called when someone checks or un-checks the have AC PLC checkbox.
        All it does is save the checkbox variable to a global variable
        :return:
        """
        globalVariables.haveAccessControlPLC = self._haveACPLC.get()

    def _pauseForEPICSCheckFunction(self):
        """
        This routine is called when someone checks or un-checks the pause for EPICS display checkbox.
        All it does is save the checkbox variable to a global variable
        :return:
        """
        globalVariables.pauseForEPICSCheck = self._pauseForEPICSCheck.get()
        if globalVariables.pauseForEPICSCheck:
            showinfo(title='Warning', message='Make sure you have set the preserved preset timer lengths in the '
                                              'PLC code to their full lengths.')
        else:
            showinfo(title='Warning', message='Make sure you have set the preserved preset timer lengths in the '
                                              'PLC code to the shorter times given on the procedure sheet.')

    def _loopUntilErrorFunction(self):
        """
        This routine is called when someone checks or un-checks the loop until error checkbox.
        All it does is save the checkbox variable to a global variable
        :return:
        """
        globalVariables.loopUntilError = self._loopUntilError.get()
        # Since we are loop until error force break on error to true
        globalVariables.breakOnError = True
        self._breakOnError.set(True)

    @staticmethod
    def _showAbout():
        showinfo("testPPS", "This is part of the automated PPS test bench. It allows one to run tests,"
                            "set breakpoints and single step through the steps so "
                            "that PLC code can be debugged.")

    def _openFile(self):

        self._file = askopenfilename(defaultextension=".rtf",
                                     filetypes=[("Text Documents", "*.rtf; *.txt"),
                                                ("All Files", "*.*")])

        if self._file == "":

            # no file to open
            self._file = None
        else:

            # Try to open the file
            # set the window title
            self.root.title(os.path.basename(self._file) + " - testPPS")
            self._thisTextArea.delete(1.0, END)

            file = open(self._file, "r")

            if self._file[-4:] == '.txt':
                self._thisTextArea.insert(1.0, file.read())
            else:  # it is an RTF file
                globalVariables.debugging = False  # Set so that windows[0].addLine will actually add the line.
                # Remove most of the RTF junk. \cf1 flags normal text. \cf2 flags red text.
                myString = file.read()
                # Get rid of everything before the first \line as it is a bunch of RTF garbage
                try:
                    index = myString.index(r'\line')
                    myString = myString[index:]
                    index = myString.index(r'\langfenp1033')  # this is for Microsoft Word file
                    myString = myString[index + 13:]
                except ValueError:
                    pass
                # The following lines are done to get rid of all the crap Microsoft Word adds into the file.
                myString = myString.replace('\n', '')
                myString = myString.replace(r'{', '')
                myString = myString.replace(r'}', '')
                myString = myString.replace(r'\hich', '')
                myString = myString.replace(r'\af0', '')
                myString = myString.replace(r'\dbch', '')
                myString = myString.replace(r'\af31505', '')
                myString = myString.replace(r'\loch', '')
                myString = myString.replace(r'\f0  ', '')
                myString = myString.replace(r'\f0 ', '')
                myString = myString.replace(r'\f0', '')
                myString = myString.replace(r'\rtlch', '')
                myString = myString.replace(r'\fcs1 ', '')
                myString = myString.replace(r'\afs24 ', '')
                myString = myString.replace(r'\ltrch', '')
                myString = myString.replace(r'\fcs0 ', '')
                myString = myString.replace(r'\fs24', '')
                myString = myString.replace(r'\cf6', r'\cf2')
                myString = myString.replace(r'\insrsid12674743 ', '')
                # The following lines are done because the windows.addLine method adds a line feed, so we kill
                # the one in the file.  Also, if the file is edited with Wordpad, it changes spaces, so there are
                # two versions of most of the replaces to handle both files that have just been saved by testPPS
                # and those that have been edited with WordPad. Don't even think about using Word to edit the file.
                myString = myString.replace(r'\line \cf1 ', r'\cf1')  # replace string that indicates next line
                myString = myString.replace(r'\line \cf2 ', r'\cf2')  # replace string that indicates next line
                myString = myString.replace(r'\line \cf', r'\cf')  # replace string that indicates next line
                myString = myString.replace(r'\line\cf1 ', r'\cf1')  # replace string that indicates next line
                myString = myString.replace(r'\line\cf2 ', r'\cf2')  # replace string that indicates next line
                myString = myString.replace(r'\line\cf', r'\cf')  # replace string that indicates next line
                myString = myString.replace(r'\cf1 \line ', r'\cf1')  # replace string that indicates next line
                myString = myString.replace(r'\cf2 \line ', r'\cf2')  # replace string that indicates next line
                myString = myString.replace(r'\cf1\line ', r'\cf1')  # replace string that indicates next line
                myString = myString.replace(r'\cf2\line ', r'\cf2')  # replace string that indicates next line
                myString = myString.replace(r'\cf1 \cf2', r'\cf2')  # Get rid of extra \cf as it causes new line
                myString = myString.replace(r'\cf1\cf2', r'\cf2')  # Get rid of extra \cf as it causes new line
                myString = myString.replace(r'\cf2 \cf1', r'\cf1')  # Get rid of extra \cf as it causes new line
                myString = myString.replace(r'\cf2\cf1', r'\cf1')  # Get rid of extra \cf as it causes new line
                myString = myString.replace(r'\line ', '\n')  # replace string that indicates next line
                myString = myString.replace(r'\line', '\n')  # replace string that indicates next line
                # myString should now be of the form:  text\cf2text\cf1\text\cf2\text...
                redText = False  # Set to true when following text should be red
                pointer = 0  # points to the position in myString we are processing.
                oldPointer = -4
                try:
                    while True:
                        pointer = myString.index(r'\cf', pointer + 1)
                        if redText:
                            windows[0].addLine(myString[oldPointer + 4:pointer], 'red')
                        else:
                            windows[0].addLine(myString[oldPointer + 4:pointer])
                        oldPointer = pointer
                        redText = myString[pointer + 3] == '2'
                except ValueError:  # the .index call did not find the string
                    # output the last bit of text
                    # First get rid of crud that Word and Wordpad add to the end of the file.
                    # Such crud starts with \par or \af
                    lastChar = len(myString) + 1
                    try:
                        lastChar = myString.index(r'\par', oldPointer + 4)
                    except ValueError:
                        pass
                    try:
                        lastChar = myString.index(r'\af', oldPointer + 4)
                    except ValueError:
                        pass
                    if redText:
                        windows[0].addLine(myString[oldPointer + 4:lastChar - 1], 'red')
                    else:
                        windows[0].addLine(myString[oldPointer + 4:lastChar - 1])
                    globalVariables.debugging = True  # Restore to its original value

            file.close()

    def _newFile(self):
        self.root.title("Untitled - testPPS")
        self._file = None
        self._thisTextArea.delete(1.0, END)

    def _writeFile(self):
        """
        This routine actually writes the test results data out to a file.
        It writes an RTF (rich text format) file simply to preserve the error message showing in red.
        Below is an example RTF file. We will write our file to look like this.
        :return:
        """
        # {\rtf1\ansi\deff0
        # {\colortbl;\red0\green0\blue0;\red255\green0\blue0;}
        # This line is the default color\line
        # \cf2
        # This line is red\line
        # \cf1
        # This line is the default color
        # }
        rangesTK = self._thisTextArea.tag_ranges('breakpoint')  # the 'breakpoint' tag name is colored red.
        # range is a list where the 1st element tells where the first red text starts, the second tells where
        # it stops, the third where the second red text starts etc.
        ranges = []
        # convert the TK object to strings.
        for rangeTK in rangesTK:
            ranges.append(rangeTK.string)
        ranges.append(END)
        file = open(self._file, "w")
        file.write(r'{\rtf1\ansi\deff0 {\colortbl;\red0\green0\blue0;\red255\green0\blue0;}')
        start = '1.0'
        for i in range(len(ranges)):
            end = ranges[i]
            if i // 2 * 2 == i:  # i is even
                myFormat = r'\cf1 '  # normal black text
            else:
                myFormat = r'\cf2 '
            myString = myFormat + self._thisTextArea.get(start, end)
            myString = myString.replace('\n', r'\line ')
            file.write(myString)
            start = end
        # end loop over ranges

        file.write('}')
        file.close()

        # Change the window title
        self.root.title(os.path.basename(self._file) + " - testPPS")

    def _saveFile(self):

        if self._file is None:
            # Save as new file
            self._file = asksaveasfilename(initialfile='Untitled.rtf',
                                           defaultextension=".rtf",
                                           filetypes=[("Text Files", "*.rtf;*.txt"),
                                                      ("All Files", "*.*")])

            if self._file == "":
                self._file = None
            else:

                # Try to save the file
                self._writeFile()

        else:
            # Try to save the file
            self._writeFile()

    def _saveAsFile(self):

        # Save as new file
        self._file = asksaveasfilename(initialfile=self._file,
                                       defaultextension=".rtf",
                                       filetypes=[("Text Files", "*.rtf;*.txt"),
                                                  ("All Files", "*.*")])

        if self._file == "":
            self._file = None
        else:

            # Try to save the file
            self._writeFile()

    def _cut(self):
        self._thisTextArea.event_generate("<<Cut>>")

    def _copy(self):
        self._thisTextArea.event_generate("<<Copy>>")

    def _paste(self):
        self._thisTextArea.event_generate("<<Paste>>")

    @staticmethod
    def _abortTest():
        globalVariables.abortTest = True
        if len(windows) > 1:
            windows[1].destroy()
            windows[1].topWindow = None

    def _executeLine(self, lineNum: int, ignoreBreak: bool = False):
        """
        This routine executes a single line of the file.
        :param lineNum: The line number that we should execute
        :param ignoreBreak: bool: If true, it will execute the line even though there is a breakpoint on it.
        :return: -3 if hit end of file; -2 if hit breakpoint; -4 if breaking due to an error;
        -1 if executed command; 0 if line had no command
        """
        if not self._steppingWithDebugger:
            self.root.update()  # handle TK screen updates
        globalVariables.brokeOnError = False  # clear flag saying we had stopped because of an error
        totalNumberOfLInes = int(self._thisTextArea.index('end-1c').split('.')[0])
        if lineNum > totalNumberOfLInes:
            return -3
        # Get this line of text.
        command = self._thisTextArea.get(f'{lineNum}.0', f'{lineNum}.end')
        if len(command) <= 0:  # blank line
            return 0
        if command[0] == '#' or command[0] == '/':  # It is a comment
            return 0
        if command[0] == self._bigDot and not ignoreBreak:  # It is a breakpoint
            return -2
        command = command.strip()  # remove leading and trailing spaces
        """
        valid commands should be like the following:
        ____Setting all inputs to 0
        ____Set input: RI HW Reset ALL input, to 1 on part: safety_in
        ____Set input: RI HW Reset ALL input, to 1 on part: safety_in, but skip it as it already was 1
        ____Check that output: LAT BSOIC-G1 RAD OK (input), is 1 on part: safety_to_AC
        ____Wait for 1 seconds
        Running test: testName
        """
        if '____Setting all inputs to' in command:  # it is a set all inputs command
            value = int(command[-1])
            setAllExternalInputs(value)
            time.sleep(globalVariables.calcTimePLC)  # give time for PLC to execute code

        elif command[0:13] == '____Wait for ':
            split1 = command[13:].split(' ')
            if len(split1) != 2:
                outputError(f'Wait command must have an integer number of seconds. Command was:\n{command}')
                # split1[0] should contain the integer number of seconds to wait.
            elif split1[0].isdigit():
                time.sleep(int(split1[0]))
            else:
                outputError(f'Wait command must have an integer number of seconds. Command was:\n{command}')

        elif command[0:14] == 'Running test: ':
            # Save the name of the test we are testing. It is after the ': '
            split1 = command.split(':')
            if len(split1) != 2:
                outputError(f'The following command line should have exactly one ":"  '
                            f'It contains {len(split1) - 1} \n{command}')
                return -1
            self.testNameBeingRun = split1[1][1:]
            if self.testNameBeingRun not in routineDict:
                outputError(f'The following command specifies a non-existent '
                            f'test: {self.testNameBeingRun}. \n{command}')
                self.testNameBeingRun = None
                globalVariables.maxSamples = None
                return -1
            else:
                globalVariables.mainRoutineRunning = routineDict[self.testNameBeingRun]
        elif '____' in command:  # it is either a set or check command
            split1 = command.split(',')
            if len(split1) != 2 and len(split1) != 3:
                outputError(f'The following command line should contain1 or 2 commas. It contains {len(split1) - 1}'
                            f'\n{command}')
                return -1
            split2 = split1[0].split(':')
            if len(split2) != 2:
                outputError(f'The following command line should have exactly one ":" before the comma. '
                            f'It contains {len(split2) - 1} \n{command}')
                return -1
            pinName = split2[1][1:]
            try:
                bitValue = int(split1[1][4])
            except ValueError:
                outputError(f'The following command specifies a invalid bit value: {split1[1][4]}. \n{command}')
                return -1
            partName = split1[1][15:]
            if partName not in partDict:
                outputError(f'The following command specifies a non-existent part: {partName}. \n{command}')
                return -1
            part = partDict[partName]
            if pinName not in part.pins:
                outputError(f'The following command specifies a non-existent pin: {pinName}. \n{command}')
                return -1
            pin = part.pins[pinName]
            try:
                if '____Set input: ' in command:  # It is a set input command
                    pin.setBit(bitValue, 1)
                    time.sleep(globalVariables.calcTimePLC)  # give time for PLC to execute code
                elif '____Check that output: ' in command:  # It is a check output command
                    # We will do nothing, because in the debugger, there are pauses which make the checking of
                    # pulsed outputs not work. The user can use the examine logic block display instead.
                    # part.checkOutputs(1, bitValue, pin.function)
                    pass
                return 0  # return code flags it like a comment.
            except SystemExit:  # This is caused by user hitting Abort Test button or breaking on error
                self._disableUI(False)  # re-enable GUI elements that should not be done while we were doing this.
                if globalVariables.abortTest:
                    outputError('Run Aborted due to user request')
                    globalVariables.abortTest = False  # Clear the flag the user set to make us abort.
                return -4

        return 0

    def _continue(self):
        """
        This routine continues execution from the present line and executes commands until it hits a breakpoint or
        the end of the file.
        :return:
        """
        self._disableUI(True)  # disable GUI elements that should not be done while we are doing this.
        self._thisTextArea.tag_remove('highlightLine', f'{self._previousLineNumber + 1}.0',
                                      f'{self._previousLineNumber + 2}.0')
        finished = 0
        ignoreBreak = True  # Since we are continuing from a breakpoint, we must ignore the breakpoint of the first line
        while finished > -2 and not globalVariables.abortTest:  # loop until breakpoint or end of file or abort
            self._lineNumber += 1
            finished = self._executeLine(self._lineNumber, ignoreBreak)
            ignoreBreak = False
        if finished == -2 or finished == -4 or globalVariables.abortTest:  # We hit a breakpoint or aborted.
            if finished != -4:  # We didn't stop due to break on error
                self._lineNumber -= 1  # we didn't execute the line with the breakpoint, so set line number back
            self._thisTextArea.tag_add('highlightLine', f'{self._lineNumber + 1}.0', f'{self._lineNumber + 2}.0')
            self.writeExamineInputs()  # display new data in the examine inputs window
            self.writeExamineOutputs()  # display new data in the examine outputs window
            self._thisTextArea.see(f'{self._lineNumber + 5}.0')  # scroll to make this line visible
            self._thisTextArea.see(f'{self._lineNumber - 5}.0')  # scroll to make this line visible
            globalVariables.abortTest = False
        self._previousLineNumber = self._lineNumber
        self._disableUI(False)  # re-enable GUI elements that should not be done while we were doing this.

    def _run(self):
        """
        This routine starts on the first line of the file and executes commands until it hits a breakpoint or
        the end of the file.
        :return:
        """
        self._lineNumber = 0
        self._continue()

    def _goToNextExecutableLine(self):
        """
        This routine skips to the next executable line (comment lines are considered executable, but not
        "____Check" lines or blank lines
        This routine is called just after we have executed a line.
        self._lineNumber will be pointing at the line we just executed.
        We want to skip any lines starting with "___Check" as the time since the last "____Set" may be high
        since the user is stepping through the code and the check may fail due to pulsed outputs.
        Also, the user can look at the examine logic  block output display to see if thins are working.
        After skipping lines if necessary, we highlight the next line to be executed and un-highlight the last line
        executed.
        :return:
        """
        totalNumberOfLInes = int(self._thisTextArea.index('end-1c').split('.')[0])
        lineNum = self._lineNumber
        while True:  # loop over lines
            lineNum += 1
            if lineNum == totalNumberOfLInes:  # We just executed the last line of the file
                lineNum = 0  # go back to the beginning of the file
                break
            command = self._thisTextArea.get(f'{lineNum}.0', f'{lineNum}.end')
            if not ('____Check' in command or len(command) < 2):
                # This is not a ____Check command or blank line, so don't skip any more lines
                lineNum -= 1
                break
        self._lineNumber = lineNum
        self._thisTextArea.tag_add('highlightLine', f'{self._lineNumber + 1}.0', f'{self._lineNumber + 2}.0')
        self.writeExamineInputs()  # display new data in the examine inputs window
        self.writeExamineOutputs()  # display new data in the examine outputs window
        self._thisTextArea.see(f'{self._lineNumber + 5}.0')  # scroll to make this line visible
        self._thisTextArea.see(f'{self._lineNumber - 5}.0')  # scroll to make this line visible
        self._thisTextArea.tag_remove('highlightLine', f'{self._previousLineNumber + 1}.0',
                                      f'{self._previousLineNumber + 2}.0')
        self._previousLineNumber = self._lineNumber

    def _step(self):
        """
        This routine executes one line.  Note that for ease of coding and to make stepOver work easily, this
        doesn't skip comment lines. So, they will be highlighted and "executed" even though that execution does
        nothing.
        :return:
        """
        self._steppingWithDebugger = True
        self._lineNumber += 1
        self._executeLine(self._lineNumber, True)
        self._goToNextExecutableLine()
        self._steppingWithDebugger = False

    def _numIndentSpaces(self, lineNum: int):
        """
        This routine counts how many spaces there are before the first non-space character. A first character of
        bitDot (signifying a breakpoint) is ignored.
        :param lineNum: int: the line number of the text widget we should examine.
        :return: The number of leading spaces (indent level)
        """
        command = self._thisTextArea.get(f'{lineNum}.0', f'{lineNum}.end')
        count = 0
        for i, character in enumerate(command):  # loop over characters in the command line
            if character == ' ':
                count += 1
            else:  # character is not a space
                if not (i == 0 and character == self._bigDot):
                    break

        return count

    def _stepOver(self):
        """
        This routine checks how spaces the command that is about to be executed is indented. It then executes commands
        until it comes to another line indented that amount or less.
        :return:
        """
        self._disableUI(True)  # disable GUI elements that should not be done while we are doing this.
        numIndentSpacesStart = self._numIndentSpaces(self._lineNumber + 1)
        numIndentSpaces = 999
        ignoreBreak = True  # Since we may be continuing from a breakpoint,
        # we must ignore the breakpoint of the first line
        while numIndentSpaces > numIndentSpacesStart:
            self._lineNumber += 1
            self._executeLine(self._lineNumber, ignoreBreak)
            ignoreBreak = False
            numIndentSpaces = self._numIndentSpaces(self._lineNumber + 1)
        self._goToNextExecutableLine()
        self._disableUI(False)  # re-enable GUI elements that should not be done while we were doing this.

    def _executeCursor(self):
        """
        This routine executes the line where the cursor presently is.
        :return:
        """
        cursorPosition = self._thisTextArea.index('insert')  # This returns the cursor position in the form row.column
        split1 = cursorPosition.split('.')
        self._lineNumber = int(split1[0])
        self._thisTextArea.tag_remove('highlightLine', f'{self._previousLineNumber + 1}.0',
                                      f'{self._previousLineNumber + 2}.0')
        # highlight the line we are  about to execute for a short time.
        self._thisTextArea.tag_add('highlightLine', f'{self._lineNumber}.0', f'{self._lineNumber + 1}.0')
        self._thisTextArea.see(f'{self._lineNumber + 5}.0')  # scroll to make this line visible
        self._thisTextArea.see(f'{self._lineNumber - 5}.0')  # scroll to make this line visible
        self.root.update_idletasks()  # force that highlight to display.
        self._executeLine(self._lineNumber, True)
        time.sleep(0.5)  # let the highlight on the original line display for a little while
        self._previousLineNumber = self._lineNumber - 1
        self._goToNextExecutableLine()

    def _breakpoint(self):
        """
        This routine sets or clears a breakpoint on the line where the cursor presently is.
        Note that a breakpoint is set simply by making the first character in a line "*"
        :return:
        """
        cursorPosition = self._thisTextArea.index('insert')  # This returns the cursor position in the form row.column
        split1 = cursorPosition.split('.')
        lineNumber = int(split1[0])
        firstCharacter = self._thisTextArea.get(f'{lineNumber}.0', f'{lineNumber}.1')
        if firstCharacter == self._bigDot:  # There was already a breakpoint set on this line.
            # remove it
            self._thisTextArea.delete(f'{lineNumber}.0', f'{lineNumber}.1')
        else:  # There wasn't a breakpoint on this line
            # add a breakpoint by putting a "*" as the first character on the line
            self._thisTextArea.insert(f'{lineNumber}.0', self._bigDot)
            self._thisTextArea.tag_add('breakpoint', f'{lineNumber}.0', f'{lineNumber}.1')
            self._thisTextArea.see(f'{self._lineNumber - 5}.0')  # scroll to make this line visible
            self._thisTextArea.see(f'{self._lineNumber + 5}.0')  # scroll to make this line visible

    def addLine(self, text: str, color: str = 'none'):
        """
        This routine adds a line at the end of the main text window
        It only does this if we are executing tests from scratch, not when we are debugging
        :param text: The text to add
        :param color: Optional: the color of the text
        :return:
        """
        if globalVariables.debugging:
            return
        self._thisTextArea.insert('end', '\n')
        self._thisTextArea.insert('end', text)
        self._thisTextArea.see('end')  # scroll to make this line visible
        if color == 'red':
            self._thisTextArea.tag_add('breakpoint', 'end - 1 line', 'end - 1c')
        elif color == 'bold':
            self._thisTextArea.tag_add('bold', 'end - 1 line', 'end - 1c')
        if not self._steppingWithDebugger:
            self.root.update()

    def update(self):
        """
        This routine runs the TK update routine which allows the GUI to check for inputs and update displays
        :return:
        """
        self.root.update()

    def _RunSingleTest(self):
        """
        This routine runs a single test named in self._chosenTestName
        :return:
        """
        if self._chosenTestName is None or self._chosenTestName not in routineDict:
            showinfo(title='Warning', message='You must first select a specific test to run.')
            return
        globalVariables.brokeOnError = False
        self._disableUI(True)  # disable GUI elements that should not be done while we are doing this.
        startTime = time.time()
        outputError('zeroErrorCount')
        self.testNameBeingRun = self._chosenTestName  # save name of routine being run for use in
        #                                                       examine outputs display and loop class
        thisRoutine = routineDict[self._chosenTestName]
        globalVariables.mainRoutineRunning = thisRoutine
        globalVariables.debugging = False
        if not globalVariables.haveRealHardware:
            outputError('Running without any real hardware to allow debugging of input spreadsheet. '
                        'No real tests are being done.')
        globalVariables.totalSets = 0
        globalVariables.totalChecks = 0

        firstTime = True
        runAborted = False
        while firstTime or globalVariables.loopUntilError:  # loop once or until an error is found
            firstTime = False
            try:
                thisRoutine.run(0)
            except SystemExit:  # This is caused by user hitting Abort Test button or aborting on error
                self._disableUI(False)  # re-enable GUI elements that should not be done while we were doing this.
                if globalVariables.abortTest:
                    outputError('Run Aborted due to user request')
                    globalVariables.abortTest = False  # Clear the flag the user set to make us abort.
                # set the window title to explain where we stopped.
                windows[0].root.title(f'Aborted test: {windows[0].testNameBeingRun}, '
                                      f'at Row: {thisRoutine.commands[thisRoutine.iRow][-1] + 1}.')
                runAborted = True  # Flag that the run was aborted
                if globalVariables.haveRealHardware:
                    try:
                        playsound(globalVariables.pathToPythonApp + '\\soundFiles\\failed.mp3', False)
                    except PlaysoundException:
                        pass  # If sound file not found, just continue on without playing it.
                    except NotImplementedError:
                        pass  # Not implemented for this version or configuration of Linux
                break
        errorCount = outputError('printErrorCount')
        endTime = time.time()
        elapsedTime = endTime - startTime
        testPrint(0, f'Time to run all tests was {elapsedTime / 60:.1f} minutes. '
                     f'Total signals set and checked: {globalVariables.totalSets} and {globalVariables.totalChecks}.')
        if not runAborted:
            windows[0].root.title(f'Test completed.')
            if globalVariables.haveRealHardware:
                try:
                    if errorCount == 0:  # The test passed
                        playsound(globalVariables.pathToPythonApp + '\\soundFiles\\passed_short.mp3', False)
                        testPrint(0, f'Test was run with pauseForEpicsCheck '
                                     f'set to: {globalVariables.pauseForEPICSCheck}.')
                        if self.userForm is not None and globalVariables.pauseForEPICSCheck:
                            self.userForm.outputForm()  # Output a form for user to fill in.
                    else:  # the test failed
                        playsound(globalVariables.pathToPythonApp + '\\soundFiles\\failed.mp3', False)
                except PlaysoundException:
                    pass  # If sound file not found, just continue on without playing it.
                except NotImplementedError:
                    pass  # Not implemented for this version or configuration of Linux
        self._disableUI(False)  # re-enable GUI elements that should not be done while we were doing this.
        globalVariables.debugging = True
        globalVariables.brokeOnError = True  # set this so display shows data from last step of test.

    def _runAllTests(self):
        """
        This routine loops through all the main test routines and runs them.
        :return:
        """
        if not globalVariables.successfulInit:
            messagebox.showinfo(title='Warning',
                                message='Please read in spreadsheet without major errors before doing tests.')
            return
        globalVariables.brokeOnError = False
        outputError('zeroErrorCount')  # zero the count of the total number of errors found
        globalVariables.debugging = False
        if not globalVariables.haveRealHardware:
            outputError('Running without any real hardware to allow debugging of input spreadsheet. '
                        'No real tests are being done.')
        globalVariables.totalSets = 0
        globalVariables.totalChecks = 0
        self._disableUI(True)  # disable GUI elements that should not be done while we are doing this.
        testPrint(0, f'Starting to run all tests')
        testPrint(0, epics.ca.find_libca())
        startTime = time.time()
        done = False

        firstTime = True
        runAborted = False
        while firstTime or globalVariables.loopUntilError:  # loop once or until an error is found
            firstTime = False

            for thisTestName in routineDict:  # loop over all routines
                thisRoutine = routineDict[thisTestName]
                if not thisRoutine.isSubroutine:  # This is a main test routine, not a subroutine
                    self.testNameBeingRun = thisRoutine.name
                    globalVariables.mainRoutineRunning = thisRoutine
                    try:
                        thisRoutine.run(0)
                    except SystemExit:  # This is caused by user hitting Abort Test button or abort on error
                        done = True
                        # Save name of the routine that caused abort so it is the single routine we will run.
                        self._chosenTestName = self.testNameBeingRun
                        self._disableUI(
                            False)  # re-enable GUI elements that should not be done while we were doing this.
                        if globalVariables.abortTest:
                            outputError('Run Aborted due to user request')
                            globalVariables.abortTest = False  # Clear the flag the user set to make us abort.
                        # set the window title to explain where we stopped.
                        windows[0].root.title(f'Aborted test: {windows[0].testNameBeingRun}, '
                                              f'at Row: {thisRoutine.commands[thisRoutine.iRow][-1] + 1}.')
                        runAborted = True  # Flag that the run was aborted
                        if globalVariables.haveRealHardware:
                            try:
                                playsound(globalVariables.pathToPythonApp + '\\soundFiles\\failed.mp3', False)
                            except PlaysoundException:
                                testPrint(0, 'Sound file not found. Current directory: ' +
                                          globalVariables.pathToPythonApp)
                                pass  # If sound file not found, just continue on without playing it.
                            except NotImplementedError:
                                pass  # Not implemented for this version or configuration of Linux
                        break  # break out of loop over tests in routineDict
            if done:
                break  # break out of loop over nIterations

        errorCount = outputError('printErrorCount')  # print the total error count
        endTime = time.time()
        elapsedTime = endTime - startTime
        testPrint(0, f'Time to run all tests was {elapsedTime / 60:.1f} minutes. '
                     f'Total signals set and checked: {globalVariables.totalSets} and {globalVariables.totalChecks}.')
        if not runAborted:
            if globalVariables.haveRealHardware:
                try:
                    if errorCount == 0:  # The test passed
                        playsound(globalVariables.pathToPythonApp + '\\soundFiles\\passed_long.mp3', False)
                        if self.userForm is not None:
                            self.userForm.outputForm()  # Output a form for user to fill in.
                    else:  # the test failed
                        playsound(globalVariables.pathToPythonApp + '\\soundFiles\\failed.mp3', False)
                except PlaysoundException:
                    pass  # If sound file not found, just continue on without playing it.
                except NotImplementedError:
                    pass  # Not implemented for this version or configuration of Linux
            windows[0].root.title(f'Test completed.')
        self._disableUI(False)  # re-enable GUI elements that should not be done while we were doing this.
        globalVariables.debugging = True

        self.writeExamineInputs()  # display new data in the examine inputs window
        self.writeExamineOutputs()  # display new data in the examine outputs window

    def _chooseTest(self, routineName):
        """
        This routine is called when a logic block to be tested is selected from a menu.
        All it does is record the name of the logic block so it can later be tested.
        :return:
        """
        self._chosenTestName = routineName

    def _initFromSpreadsheet(self):
        # Import all the lists and routines
        outputError('zeroTotalErrorCount')
        globalVariables.debugging = False
        self._fileName = askopenfilename(defaultextension=".xlsx", initialfile=self._fileName,
                                         filetypes=[("Excel files", ".xls .xlsx"),
                                                    ("All Files", "*.*")])

        if self._fileName == "":
            # no file to open
            return

        try:
            # Open the spreadsheet as an in-memory file. This is done to allow Excel to write a new version
            # of the file while this program is running. This is necessary because wb.close() doesn't actually close
            # a read-only file.
            with open(self._fileName, "rb") as f:
                in_mem_file = io.BytesIO(f.read())

            wb = openpyxl.load_workbook(in_mem_file, read_only=True, data_only=True)
        except Exception as ex:
            outputError(f'Excel file not read because: {ex}')
            return

        self._disableUI(True)  # disable GUI elements that should not be allowed while we are doing this.
        # profile pr = cProfile.Profile()  # set up profiler

        # parts and partDict are now declared in globalVariables so they can be used in other modules.
        parts.clear()  # list off all the parts. this maintains the order in which they were created.
        partDict.clear()  # dictionary of all the parts. This makes it easy to find a part
        listDict.clear()  # dictionary of all the lists
        pinDict.clear()  # dictionary of all the pins.
        routineDict.clear()  # dictionary of all the routines
        self.userForm = None

        # clear the output window
        self._thisTextArea.delete('1.0', 'end')

        if 'PYEPICS_LIBCA' not in os.environ:
            # following lines are for debugging the finding of the EPICS dlls.
            pathOfPythonScript = os.path.realpath(__file__)  # path of the source python script we are running
            # It includes the actual filename of python file. We will strip that off to just get the folder
            mySplit = pathOfPythonScript.rsplit('\\', 1)
            globalVariables.pathToPythonApp = mySplit[0]
            # Set environment variable telling pyepics the path where the ca.dll lives. Com.dll will be in same folder.
            pathOfEpicsDLLs = mySplit[0] + '/epics_dlls/ca.dll'
            # testPrint(0, 'path of EPICS DLLs used to set environment variable: ' + pathOfEpicsDLLs)
            os.environ['PYEPICS_LIBCA'] = pathOfEpicsDLLs

        testPrint(0, f'Reading Excel file: {self._fileName}, at {datetime.now()}. '
                     f'It was last modified on: {time.ctime(os.path.getmtime(self._fileName))}')

        globalVariables.successfulInit = True  # Set false if we there are errors in the input file that we can't
        # easily clean up
        # enough to continue testing without blowing up.

        newPart = None

        try:
            # loop over all sheets
            for sheet in wb:
                windows[0].addLine(f'processing sheet {sheet.title}')
                self.root.update()
                firstCell = sheet['A1'].value
                # if this sheet describes a part(s) cell A1 will contain a string of the form 'partType=<partTypeName>
                # where <partTypeName> is one the strings in the partType dictionary (e.g. 'AND' or 'safety_in'
                if firstCell == 'partType=user_form':
                    newPart = UserForm()
                    self.userForm = newPart
                elif firstCell == 'partType=safety_in':
                    newPart = PartIO(True)
                elif firstCell == 'partType=safety_out':
                    newPart = PartIO(False)
                elif firstCell == 'partType=AC_in':
                    newPart = PartIO(True)
                elif firstCell == 'partType=AC_out':
                    newPart = PartIO(False)
                elif firstCell == 'partType=EpicsToAC':
                    newPart = PartEpicsToAC()
                elif firstCell == 'partType=ACtoEpics':
                    newPart = PartACtoEpics()
                elif firstCell == 'partType=ACtoSafety':
                    newPart = PartACtoSafety()
                elif firstCell == 'partType=safetyToAC':
                    newPart = PartSafetyToAC()
                elif firstCell == 'partType=list':
                    # loop over columns in the sheet looking for row 3 being non-blank.
                    # That column contains a list with the list name being in row 3.
                    for iColumn in range(2, sheet.max_column + 1):
                        listName = sheet.cell(3, iColumn).value
                        if listName is not None:  # This cell has a list name
                            if listName in listDict:
                                outputError(
                                    f'List defined in column: {get_column_letter(iColumn)}, named: {listName}, '
                                    f'has previously been defined.')
                                continue  # go on to next column
                            else:
                                try:
                                    rows = readWorksheet(sheet)
                                    List(rows, 2, iColumn - 1)
                                except SyntaxError:
                                    pass  # With error, the list simply not saved by the List class init.
                    newPart = None

                elif firstCell == 'partType=test' or firstCell == 'partType=subroutine':
                    # This sheet contains a main routine (test) or it could have several subroutines.
                    # The only difference is that the name of a test is taken from the sheetName while
                    # the name of a subroutine comes from the subroutine statement.
                    # There can be multiple subroutines on a sheet. In fact, a test sheet can have
                    # subroutines on it after the initial test, but that is discouraged due to poor readability.

                    # Each test or subroutine will be a separate object, we call the object creation routine
                    # with the line number of where a routine starts. It returns self.nextLineNumber
                    # the line number where the
                    # routine starts (or -1 as a flag that there are no more routines on the sheet).

                    iRow = 3  # routines always start on row 4 (row 3 has column labels).
                    # iRow is set to 1 less than this because rows are numbered from 0 in Python and 1 in Excel
                    # after your program ends
                    while iRow != -1:
                        # profile pr.enable()
                        newRoutine = Routine(sheet, iRow)
                        # profile pr.disable()
                        # the routine we just processed may be followed by another routine. If this is the case
                        # newRoutine.nextRoutineRow will be set to the row number where it starts.
                        # It will be -1 otherwise.
                        iRow = newRoutine.nextRoutineRow
                        routineDict[newRoutine.name] = newRoutine

                else:
                    # Cell A1 doesn't have a valid partType in it. complain.
                    if firstCell is None:  # handle empty cells
                        lowerCell = 'none'
                    else:
                        lowerCell = firstCell.lower()

                    if 'none' in lowerCell or 'junk' in lowerCell or 'dummy' in lowerCell:
                        testPrint(indentLevelInc, 'Sheet skipped because cell A1 was empty or '
                                                  'contained "none" or "dummy"')
                        continue
                    else:
                        outputError(
                            f'Cell A1 on this sheet has an invalid value: {firstCell}. Sheet will not be processed.')
                        continue

                if newPart:  # This is a part that took a whole sheet, e.g. PLC I/O, not one with multiple logic blocks
                    parts.append(newPart)
                    newPart.initFromExcel(sheet)
                    partDict[newPart.partName] = newPart

                # end loop over sheets

            # now that we have read in and compiled all the tests, check to make sure that all pins are used.
            first = True
            for pin in pinDict.values():
                if not pin.used:
                    if first:
                        outputError('Following signals are never used and hence tests are not thorough enough', False)
                        first = False
                    outputError(f'Signal: {pin.signalName}, of part: {pin.parent.partName}, defined on Excel '
                                f'row: {pin.excelRow}.')

            if not globalVariables.successfulInit:
                outputError(f'Because of above severe errors in the input Excel file, tests cannot be done.')
            outputError('printErrorCount')
            self.adjustTestMenu()

        except SystemExit:
            # user requested us to abort partway through. Clear the stuff we did since it is only partially done.
            parts.clear()
            globalVariables.successfulInit = False
            if globalVariables.abortTest:
                outputError('Run Aborted due to user request')
                globalVariables.abortTest = False  # Clear the flag the user set to make us abort.
            partDict.clear()
        wb.close()  # Close the Excel workbook
        globalVariables.debugging = True
        self._disableUI(False)  # re-enable GUI elements that should not be done while we were doing this.

        # profile pr.print_stats(sort="calls")  # output profile information

    def _createExamineInputsWindow(self):
        """
        This routine creates a window that will be used to display all the inputs and variables of the current test
        :return:
        """
        if self._examineInputsWindow is None:  # create the window
            self._examineInputsWindow = Toplevel(self.root)
            self._examineInputsWindow.title('Examine inputs and Variables')
            self._examineInputsWindow.geometry('450x600-50+100')
            # set a routine to call if the user  closes the window (by clicking on the 'x')
            self._examineInputsWindow.protocol("WM_DELETE_WINDOW", self._closedExamineInputsWindow)
            # make a text area with scroll bar in the window
            self._inputTextArea = Text(self._examineInputsWindow, width=40, height=20,
                                       borderwidth=5, relief='sunken')
            self._inputTextArea.tag_configure('disagree', foreground='red')
            self._inputTextArea.grid(column=1, row=0, sticky=N + E + S + W)
            # self._logicBlockTextArea['state'] = 'disabled'
            # To make the text area auto resizable
            self._examineInputsWindow.grid_rowconfigure(0, weight=1)
            self._examineInputsWindow.grid_columnconfigure(0, weight=0)
            self._examineInputsWindow.grid_columnconfigure(1, weight=1)
            # To add scrollbar
            self._inputScrollBar = Scrollbar(self._inputTextArea)
            self._inputScrollBar.pack(side=RIGHT, fill=Y)

            # Scrollbar will adjust automatically according to the content
            self._inputScrollBar.config(command=self._inputTextArea.yview)
            self._inputTextArea.config(yscrollcommand=self._inputScrollBar.set)

            # Fill in the data
            self.writeExamineInputs()

        else:  # Just pop the window to the top
            self._examineInputsWindow.deiconify()
            self._examineInputsWindow.lift()

    def _createExamineOutputsWindow(self):
        """
        This routine creates a window that will be used to display all the inputs and variables of the current test
        :return:
        """
        if self._examineOutputsWindow is None:  # create the window
            self._examineOutputsWindow = Toplevel(self.root)
            self._examineOutputsWindow.title('Examine outputs')
            self._examineOutputsWindow.geometry('900x600-5+40')
            # set a routine to call if the user  closes the window (by clicking on the 'x')
            self._examineOutputsWindow.protocol("WM_DELETE_WINDOW", self._closedExamineOutputsWindow)
            # make a text area with scroll bar in the window
            self._outputTextArea = Text(self._examineOutputsWindow, width=40, height=20,
                                        borderwidth=5, relief='sunken')
            self._outputTextArea.tag_configure('disagree', foreground='red')
            self._outputTextArea.grid(column=1, row=0, sticky=N + E + S + W)
            # self._logicBlockTextArea['state'] = 'disabled'
            # To make the text area auto resizable
            self._examineOutputsWindow.grid_rowconfigure(0, weight=1)
            self._examineOutputsWindow.grid_columnconfigure(0, weight=0)
            self._examineOutputsWindow.grid_columnconfigure(1, weight=1)
            # To add scrollbar
            self._outputScrollBar = Scrollbar(self._outputTextArea)
            self._outputScrollBar.pack(side=RIGHT, fill=Y)

            # Scrollbar will adjust automatically according to the content
            self._outputScrollBar.config(command=self._outputTextArea.yview)
            self._outputTextArea.config(yscrollcommand=self._outputScrollBar.set)

            # Fill in the data
            self.writeExamineOutputs()

        else:  # Just pop the window to the top
            self._examineOutputsWindow.deiconify()
            self._examineOutputsWindow.lift()

    @staticmethod
    def _makePulseTime(myTime: int, value: str):
        """
        This routine returns a string which describes how long an output was at a value
        :param myTime: int: time in ms that the output was at a given value
        :param value: str: a 1 character string giving the value of the output typically '0' or '1' or '?'
        :return:
        """
        if myTime >= 10000:
            myTime = round(myTime / 1000)
            units = 's'
        else:
            units = 'ms'
        myText = f' {myTime}{units}{value}'
        return myText

    def writeExamineInputs(self):
        """
        This routine fills the self._inputTextArea with all the inputs and variables
        of the test whose name is in self._currentTest
        This routine normally gets called by other code each time a command line is executed and we are waiting
        for the user to request another operation (e.g. after doing a step or stopping at a breakpoint.
        It then schedules itself to be called again every tenth of a second for 2 seconds.
        :return:
        """
        col3offEdge = 4
        column2Title = 'signalName                         '
        column3Title = 'Value  '
        column2Len = len(column2Title) + col3offEdge

        if self._examineInputsWindow is None:  # There is no window, so do nothing
            return

        textBox = self._inputTextArea  # save to a shorter name.
        lineNum = 1

        if self.testNameBeingRun in routineDict:
            routine = routineDict[self.testNameBeingRun]
        elif self._chosenTestName is not None:
            routine = routineDict[self._chosenTestName]
        else:
            textBox.replace(f'{lineNum}.0', f'{lineNum + 1}.0',
                            f'Requested test: {self.testNameBeingRun} does not exist\n')
            textBox.delete(f'{lineNum + 1}.0', END)
            return

        # Finally ready to put up the display
        self._examineInputsWindow.title(f'Examine inputs and variables of test: {routine.name}')
        textBox.replace(f'{lineNum}.0', f'{lineNum + 1}.0',
                        f'Inputs and variables of test: {routine.name}\n\n\n')
        lineNum += 1

        # First list all the variables used in the test.
        textBox.replace(f'{lineNum}.0', f'{lineNum + 2}.0',
                        '\n       Variables:\n\n')
        lineNum += 2
        textBox.replace(f'{lineNum}.0', f'{lineNum + 1}.0',
                        f'{column2Title}  {column3Title}\n\n\n')  # output column headings
        lineNum += 1
        for pin in routine.pins:  # loop over pins and variables used in the test
            if isinstance(pin, Variable):
                text = (pin.name + (column2Len - len(pin.name)) * ' ' +
                        str(pin.presentValue))
                textBox.replace(f'{lineNum}.0', f'{lineNum + 1}.0',
                                f'{text}\n\n')
                lineNum += 1
        # end loop over pins

        # Now list all the pins used in the test.
        textBox.replace(f'{lineNum}.0', f'{lineNum + 2}.0',
                        '\n       Inputs:\n\n')
        lineNum += 2
        textBox.replace(f'{lineNum}.0', f'{lineNum + 1}.0',
                        f'{column2Title}  {column3Title}\n\n')  # output column headings
        lineNum += 1
        for pin in routine.pins:  # loop over pins and variables used in the test
            if not isinstance(pin, Variable):  # it is a pin
                if pin.pinIOType == PinIOType.INPUT:  # This is an input pin
                    text = (pin.signalName + (column2Len - len(pin.signalName)) * ' ' +
                            str(pin.presentValue))
                    textBox.replace(f'{lineNum}.0', f'{lineNum + 1}.0',
                                    f'{text}\n\n')
                    lineNum += 1
        # end loop over pins
        # delete any extra text that may be there because the current display has fewer lines than the previous one
        textBox.delete(f'{lineNum}.0', END)

    def writeExamineOutputs(self, refreshCount=0):
        """
        This routine fills the self._outputTextArea with all the I/O information for the
        routine whose name is in self._currentTest
        This routine normally gets called by other code each time a command line is executed and we are waiting
        for the user to request another operation (e.g. after doing a step or stopping at a breakpoint.
        It then schedules itself to be called again every tenth of a second for 2 seconds.
        :param refreshCount: Number of times we will have refreshed this display since the last time we
                             executed a command.
        :return:
        """
        col3offEdge = 4
        column2Title = 'signalName                               '
        column3Title = 'Expected  '
        column4Title = 'Actual'
        column2Len = len(column2Title) + col3offEdge
        column3Len = len(column3Title)

        if refreshCount != 0 and refreshCount != self._examineRefreshCount + 1:
            # A new command has been executed, calling us afresh, so don't continue the refreshes instigated
            # by the previous command.
            return

        if self._examineOutputsWindow is None:  # There is no window, so do nothing
            return

        textBox = self._outputTextArea  # save to a shorter name.
        lineNum = 1

        if self.testNameBeingRun in routineDict:
            routine = routineDict[self.testNameBeingRun]
        elif self._chosenTestName is not None:
            routine = routineDict[self._chosenTestName]
        else:
            textBox.replace(f'{lineNum}.0', f'{lineNum + 1}.0',
                            f'Requested test: {self.testNameBeingRun} does not exist\n')
            textBox.delete(f'{lineNum + 1}.0', END)
            return

        if not globalVariables.waiting and (globalVariables.brokeOnError or not globalVariables.debugging):
            # Since we stopped due to an error or the end of a completed test, we will have repeatedly done cagets
            # the number of times saved by checkOutputs in nSamples
            refreshCount = globalVariables.nSamples
            # refreshCount = -1
            # if len(globalVariables.readTime) > 0:
            #     refreshCount = len(globalVariables.readTime[0]) - 1
        else:
            # read the pins we are supposed to check. This gets skipped if brokenOnError is true because
            # the data was read and saved in lists when we did the cagets that read data that was incorrect.
            # We don't want to read the data again because pulsed outputs would have already finished their pulse.
            if refreshCount == 0:  # First call after executing a command
                # Set buffer to 2 to flag as uninitialized.
                globalVariables.readTime = [[2 for _ in range(maxSamples)] for _ in range(len(routine.pins))]
                globalVariables.readValue = [[2 for _ in range(maxSamples)] for _ in range(len(routine.pins))]
            readError = False  # Will be set true if caget returns a bad SEVR

            # Read all the I/O pins used by this routine and store in a time history buffer.
            for iPin, pin in enumerate(routine.pins):  # loop over pins in the part
                if isinstance(pin, Variable) or pin.pinIOType != PinIOType.OUTPUT:
                    # Don't deal with variables or input pins.
                    continue
                # get and store the present output of the pin and the present time in ms.
                hardwareOut = caget(pin.PVname, timeout=2)
                sevr = caget(pin.PVsevr, timeout=2, as_string=True)
                myTime = int(time.time() * 1000)
                if hardwareOut is None:
                    # The operation timed out.
                    hardwareOut = 'readError'
                    readError = True
                elif sevr == 'INVALID':
                    # There was some other problem e.g. the PLC has no power or network connection
                    hardwareOut = f'SEVR={sevr}'
                    readError = True
                globalVariables.readTime[iPin][refreshCount] = myTime
                globalVariables.readValue[iPin][refreshCount] = hardwareOut
            # end loop over pins

            self._examineRefreshCount = refreshCount
            # schedule this routine to be called again in sampleTime ms
            if refreshCount < maxSamples - 1 and not readError and not globalVariables.waiting:
                self.root.after(sampleTime, lambda: self.writeExamineOutputs(refreshCount + 1))
            # end if globalVariables.brokeOnError:

        # Finally ready to put up the display
        self._examineOutputsWindow.title(f'Examine outputs of test: {routine.name}')
        textBox.replace(f'{lineNum}.0', f'{lineNum + 1}.0',
                        f'Outputs of test: {routine.name}    at {datetime.now()}\n\n')
        lineNum += 1
        textBox.replace(f'{lineNum}.0', f'{lineNum + 1}.0',
                        f'\n{column2Title}{column3Title}{column4Title} {refreshCount}\n\n')
        lineNum += 2

        for iPin, pin in enumerate(routine.pins):  # loop over pins in the part
            if isinstance(pin, Variable) or pin.pinIOType != PinIOType.OUTPUT:
                # Don't deal with variables or input pins.
                continue
            # put together a string of the form '100ms0 200ms1 500ms0'
            # Any long gaps between caput and first caget or between cagets will be flagged with e.g. '400ms?'
            myText = ''
            myPreviousTime = globalVariables.timeOfLastCaput
            lastRecordedTime = myPreviousTime  # Time of last change we recorded in myTest
            lastRecordedValue = 2  # What we read at the lastRecordedTime (2 means uninitialized)
            if len(globalVariables.readTime) > 0:
                for iSample in range(refreshCount + 1):  # Loop over data we have read.
                    myTime = globalVariables.readTime[iPin][iSample]
                    myValue = globalVariables.readValue[iPin][iSample]
                    myTimeDiff = myTime - myPreviousTime

                    if myTimeDiff > 1500 + sampleTime:  # too much time has passed. Could have missed a
                        # short output pulse
                        if iSample > 0:  # There is a previous reading we should report.
                            myTimeDiffX = globalVariables.readTime[iPin][iSample - 1] - lastRecordedTime + sampleTime
                            myText += self._makePulseTime(myTimeDiffX, str(lastRecordedValue))
                        myText += self._makePulseTime(myTimeDiff, '?')
                        lastRecordedTime = myTime
                        lastRecordedValue = myValue

                    else:  # There hasn't been a long time gap.
                        if iSample == 0:
                            # Save first read value as the last recorded value.
                            lastRecordedValue = myValue
                        if lastRecordedValue != myValue:
                            # The read out value has changed from its last value. Note that lastRecordedValue == 2
                            # flags having not yet read anything out.
                            myTimeDiff = myTime - lastRecordedTime
                            myText += self._makePulseTime(myTimeDiff, str(lastRecordedValue))
                            lastRecordedTime = myTime
                            lastRecordedValue = myValue
                    # end if myTimeDiff

                    myPreviousTime = myTime

                # end loop over samples
            # end if refreshCount > 0:

            if maxSamples > 0 and refreshCount >= 0:
                correctValue = pin.presentValue
                try:
                    hardwareOut = globalVariables.readValue[iPin][refreshCount]
                    myTime = globalVariables.readTime[iPin][refreshCount]
                except IndexError:
                    hardwareOut = 3
                    myTime = 3
                if myText == '':  # this pin has had the same value the whole time.
                    myText = str(hardwareOut)
                else:  # show the last values
                    myTimeDiff = myTime - lastRecordedTime
                    myText += self._makePulseTime(myTimeDiff, str(lastRecordedValue))
                text = (
                        pin.signalName + (column2Len - len(pin.signalName)) * ' ' +
                        str(correctValue) + (column3Len - 2 - col3offEdge) * ' ' +
                        myText)
                textBox.replace(f'{lineNum}.0', f'{lineNum + 1}.0',
                                f'{text}\n\n')
                if correctValue != 2 and hardwareOut != correctValue:  # error found so color the line red
                    textBox.tag_add('disagree', f'{lineNum}.0', f'{lineNum + 1}.0')
                lineNum += 1
            # end if nSamples > 0
        # end loop over pins
        # delete any extra text that may be there because the current display has fewer lines than the previous one
        textBox.delete(f'{lineNum}.0', END)

    def _closedExamineInputsWindow(self):
        """
        This routine is called when the user closes the examine inputs window.
        It just sets the pointer to the window to None to flag it no longer exists and closes the window
        :return:
        """
        self._examineInputsWindow.destroy()
        self._examineInputsWindow = None

    def _closedExamineOutputsWindow(self):
        """
        This routine is called when the user closes the examine outputs window.
        It just sets the pointer to the window to None to flag it no longer exists and closes the window
        :return:
        """
        self._examineOutputsWindow.destroy()
        self._examineOutputsWindow = None

    def run(self):

        # Run main application via the main TK event loop
        self.root.mainloop()
