import openpyxl

from errorHandler import outputError
from pinClass import PinPLC, PinIOType

wb = openpyxl.load_workbook(r"C:\TomFiles\SLAC\PPS bench test\input list.xlsx", read_only=True, data_only=True)
sheetNamesList = ['safety_in', 'safety_out', 'AC_in', 'AC_out', 'AC_to_safety', 'safety_to_AC',
                  'AC_to_EPICS', 'EPICS_to_AC']
columnNames = ['signal name', 'slot', 'pin #', 'English description'] # correct headings for spreadsheet columns
for sheetName in sheetNamesList:
    s = wb[sheetName]
    pins = []
    firstRow = True
    headingError = False

    # loop over rows of the worksheet
    for r in s.rows:
        if firstRow:
            # this is the first row of the spread sheet. Make sure it has the correct column headings
            firstRow = False
            # loop over columns
            for iColumn, columnName in enumerate(columnNames):
                if columnName != r[iColumn].value:
                    outputError('Heading in column ' + str(iColumn+1) + ' should be ' + columnName +
                                ' and is ' + r[iColumn].value)
                    headingError = True
            continue
        if headingError:
            break  # column headings are wrong, so skip reading the rest of the worksheet.

        # We are now in the second or later row of the sheet. Create a pin with the data in the row.
        pins.append(PinPLC(r[0].value, PinIOType.INPUT, r[1].value, r[2].value, r[3].value))

        print(pins[-1])
        # end loop over rows

    # end loop over sheets

print(pins)
wb.close()  # close the Excel workbook.
# print(wb.sheetnames)

