|
| 1 | +from openpyxl.utils import column_index_from_string |
| 2 | +from openpyxl.utils.cell import coordinate_from_string |
| 3 | +from ExcelDataDriver.ExcelParser.ABCParserStrategy import ABCParserStrategy |
| 4 | +from ExcelDataDriver.ExcelTestDataRow import ExcelTestDataRow |
| 5 | + |
| 6 | + |
| 7 | +class CustomExcelParser(ABCParserStrategy): |
| 8 | + |
| 9 | + def __init__(self): |
| 10 | + ABCParserStrategy.__init__(self) |
| 11 | + print('Using CustomExcelParser') |
| 12 | + self.maximum_column_index_row = 3 |
| 13 | + self.start_row = 1 |
| 14 | + |
| 15 | + def parsing_column_indexs(self, ws): |
| 16 | + ''' |
| 17 | + @param sku: Product SKU |
| 18 | + @param normal price: Product normal price |
| 19 | + @param normal cost in vat: Product normal cost include vat |
| 20 | + @param normal cost ex vat: Product normal cost exclude vat |
| 21 | + @param normal cost gp: Product normal GP % |
| 22 | + @param promotion price: Product promotion price |
| 23 | + @param promotion cost in vat: Product promotion cost include vat |
| 24 | + @param promotion cost ex vat: Product promotion cost exclude vat |
| 25 | + @param promotion cost gp: Product promotion GP % |
| 26 | + ''' |
| 27 | + ws_column_indexs = {} |
| 28 | + |
| 29 | + ########################## |
| 30 | + # Parse mandatory property |
| 31 | + ########################## |
| 32 | + for index, row in enumerate(ws.rows): |
| 33 | + if index > self.maximum_column_index_row: |
| 34 | + break |
| 35 | + for cell in row: |
| 36 | + if (cell.value is not None) and (cell.value in self.DEFAULT_COLUMN_INDEXS): |
| 37 | + ws_column_indexs[cell.value] = column_index_from_string(coordinate_from_string(cell.coordinate)[0]) |
| 38 | + print('Mandatory : '+str(cell.value) + ' : ' + str(cell.coordinate) + ' : ' + str(column_index_from_string(coordinate_from_string(cell.coordinate)[0]))) |
| 39 | + if len(ws_column_indexs) > 0: |
| 40 | + break |
| 41 | + |
| 42 | + ########################## |
| 43 | + # Parse optional property |
| 44 | + ########################## |
| 45 | + # Normal Price |
| 46 | + # Normal Price |
| 47 | + # (In Vat) |
| 48 | + # GP (%) |
| 49 | + # Promotion Price |
| 50 | + # Promotion Price |
| 51 | + # (In Vat) |
| 52 | + # GP (%) |
| 53 | + for index, row in enumerate(ws.rows): |
| 54 | + if index > self.maximum_column_index_row: |
| 55 | + break |
| 56 | + for cell in row: |
| 57 | + if (cell.value is not None) and (cell.value not in self.DEFAULT_COLUMN_INDEXS): |
| 58 | + column_name = cell.value.lower().strip() |
| 59 | + |
| 60 | + # Normal & Promotion |
| 61 | + if column_name == '(in vat)': |
| 62 | + if 'normal cost in vat' not in ws_column_indexs: |
| 63 | + ws_column_indexs['normal cost in vat'] = column_index_from_string(coordinate_from_string(cell.coordinate)[0]) |
| 64 | + else: |
| 65 | + ws_column_indexs['promotion cost in vat'] = column_index_from_string(coordinate_from_string(cell.coordinate)[0]) |
| 66 | + |
| 67 | + elif column_name == '(ex vat)': |
| 68 | + if 'normal cost ex vat' not in ws_column_indexs: |
| 69 | + ws_column_indexs['normal cost ex vat'] = column_index_from_string(coordinate_from_string(cell.coordinate)[0]) |
| 70 | + else: |
| 71 | + ws_column_indexs['promotion cost ex vat'] = column_index_from_string(coordinate_from_string(cell.coordinate)[0]) |
| 72 | + |
| 73 | + elif column_name == 'gp (%)': |
| 74 | + if 'normal cost gp' not in ws_column_indexs: |
| 75 | + ws_column_indexs['normal cost gp'] = column_index_from_string(coordinate_from_string(cell.coordinate)[0]) |
| 76 | + else: |
| 77 | + ws_column_indexs['promotion cost gp'] = column_index_from_string(coordinate_from_string(cell.coordinate)[0]) |
| 78 | + |
| 79 | + ws_column_indexs[column_name] = column_index_from_string(coordinate_from_string(cell.coordinate)[0]) |
| 80 | + print('Optional : '+str(column_name) + ' : ' + str(cell.coordinate) + ' : ' + str(column_index_from_string(coordinate_from_string(cell.coordinate)[0]))) |
| 81 | + |
| 82 | + print('Done parsing column indexes') |
| 83 | + return ws_column_indexs |
| 84 | + |
| 85 | + def parse_test_data_properties(self, ws, ws_column_indexs): |
| 86 | + test_datas = [] |
| 87 | + for index, row in enumerate(ws.rows): |
| 88 | + if index < self.start_row: |
| 89 | + continue |
| 90 | + self.is_test_data_valid(ws_column_indexs, ws.title, index, row) |
| 91 | + test_data = self.map_data_row_into_test_data_obj(ws_column_indexs, ws.title, index, row) |
| 92 | + if test_data is not None: |
| 93 | + test_datas.append(test_data) |
| 94 | + else: |
| 95 | + break |
| 96 | + print('Total test datas: ' + str(len(test_datas))) |
| 97 | + return test_datas |
| 98 | + |
| 99 | + def is_test_data_valid(self, ws_column_indexes, ws_title, row_index, row): |
| 100 | + return True |
| 101 | + |
| 102 | + def map_data_row_into_test_data_obj(self, ws_column_indexes, ws_title, row_index, row): |
| 103 | + status = row[ws_column_indexes[self.MANDATORY_TEST_DATA_COLUMN['status']] - 1] |
| 104 | + log_message = row[ws_column_indexes[self.MANDATORY_TEST_DATA_COLUMN['log_message']] - 1] |
| 105 | + screenshot = row[ws_column_indexes[self.MANDATORY_TEST_DATA_COLUMN['screenshot']] - 1] |
| 106 | + tags = row[ws_column_indexes[self.MANDATORY_TEST_DATA_COLUMN['tags']] - 1] |
| 107 | + koriico_sku = row[ws_column_indexes['sku'] - 1] |
| 108 | + |
| 109 | + # Excel library send the last row with None data. |
| 110 | + if koriico_sku.value is None: |
| 111 | + return None |
| 112 | + test_data_row = ExcelTestDataRow(ws_title, row_index, row, ws_column_indexes, status, log_message, screenshot, tags) |
| 113 | + return test_data_row |
0 commit comments