DataAccess.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: UTF-8 -*-
  2. """
  3. @Project :pytest_src
  4. @File :DataAccess.py
  5. @Author :haojiang
  6. @Date :2022/9/24 15:43
  7. """
  8. from enum import Enum
  9. from openpyxl import load_workbook
  10. CHECK_LIST_FILE = r'./Data/CheckList.xlsx'
  11. class ColumHeader(Enum):
  12. Module = 0
  13. ConfigurationItem = 1
  14. Type = 2
  15. ExpectValue = 3
  16. SearchPattern = 4
  17. SearchText = 5
  18. SearchRule = 6
  19. class ItemType(Enum):
  20. FIXED_VALUE = 0
  21. CUSTOM_PATTERN = 1
  22. DEPENDENCY_PATTERN = 2
  23. ItemRule = {
  24. ItemType.FIXED_VALUE: (
  25. ColumHeader.Module.value,
  26. ColumHeader.ConfigurationItem.value,
  27. ColumHeader.ExpectValue.value),
  28. ItemType.CUSTOM_PATTERN: (
  29. ColumHeader.Module.value,
  30. {
  31. 'xpath_str': ColumHeader.SearchPattern.value,
  32. 'xpath_text': ColumHeader.SearchText.value,
  33. 'xpath_rule': ColumHeader.SearchRule.value
  34. },
  35. ColumHeader.ConfigurationItem.value,
  36. ColumHeader.ExpectValue.value),
  37. ItemType.DEPENDENCY_PATTERN: (
  38. ColumHeader.Module.value,
  39. ColumHeader.ConfigurationItem.value,
  40. ColumHeader.ExpectValue.value,
  41. ColumHeader.SearchPattern.value
  42. )
  43. }
  44. def get_item_by_item_type(item_type: ItemType, row_data):
  45. ret_data = list()
  46. for pattern in ItemRule[item_type]:
  47. if isinstance(pattern, dict):
  48. pattern_bk = dict()
  49. for k, v in pattern.items():
  50. pattern_bk[k] = row_data[v].value
  51. else:
  52. pattern_bk = row_data[pattern].value
  53. ret_data.append(pattern_bk)
  54. return tuple(ret_data)
  55. def get_xlsx_data_by_item_type(item_type: ItemType):
  56. wb = load_workbook(CHECK_LIST_FILE)
  57. sheet1 = wb.active
  58. ret_data_table = list()
  59. for row in sheet1.iter_rows():
  60. if row[ColumHeader.Type.value].value == item_type.name:
  61. ret_data_table.append(get_item_by_item_type(item_type, row))
  62. return ret_data_table
  63. if __name__ == '__main__':
  64. table = get_xlsx_data_by_item_type(ItemType.CUSTOM_PATTERN)
  65. print(table)