DataAccess.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. Hardware = 0
  13. Project = 1
  14. Module = 2
  15. ConfigurationItem = 3
  16. Type = 4
  17. ExpectValue = 5
  18. SearchPattern = 6
  19. SearchText = 7
  20. SearchRule = 8
  21. class ItemType(Enum):
  22. FIXED_VALUE = 0
  23. CUSTOM_PATTERN = 1
  24. DEPENDENCY_PATTERN = 2
  25. class HardwareType(Enum):
  26. ALL = 0
  27. GW04 = 1
  28. CCP = 2
  29. class ProjectType(Enum):
  30. ALL = 0
  31. AS33 = 1
  32. AS33P = 2
  33. ItemRule = {
  34. ItemType.FIXED_VALUE: (
  35. ColumHeader.Module.value,
  36. ColumHeader.ConfigurationItem.value,
  37. ColumHeader.ExpectValue.value),
  38. ItemType.CUSTOM_PATTERN: (
  39. ColumHeader.Module.value,
  40. {
  41. 'xpath_str': ColumHeader.SearchPattern.value,
  42. 'xpath_text': ColumHeader.SearchText.value,
  43. 'xpath_rule': ColumHeader.SearchRule.value
  44. },
  45. ColumHeader.ConfigurationItem.value,
  46. ColumHeader.ExpectValue.value),
  47. ItemType.DEPENDENCY_PATTERN: (
  48. ColumHeader.Module.value,
  49. ColumHeader.ConfigurationItem.value,
  50. ColumHeader.ExpectValue.value,
  51. ColumHeader.SearchPattern.value
  52. )
  53. }
  54. def get_item_by_item_type(item_type: ItemType, row_data):
  55. ret_data = list()
  56. for pattern in ItemRule[item_type]:
  57. if isinstance(pattern, dict):
  58. pattern_bk = dict()
  59. for k, v in pattern.items():
  60. pattern_bk[k] = row_data[v].value
  61. else:
  62. pattern_bk = row_data[pattern].value
  63. ret_data.append(pattern_bk)
  64. return tuple(ret_data)
  65. def get_xlsx_data_by_filter(hardware: HardwareType, project: ProjectType, item_type: ItemType):
  66. wb = load_workbook(CHECK_LIST_FILE)
  67. sheet1 = wb.active
  68. ret_data_table = list()
  69. for row in sheet1.iter_rows():
  70. if row[ColumHeader.Type.value].value == item_type.name \
  71. and (row[ColumHeader.Hardware.value].value == hardware.name
  72. or row[ColumHeader.Hardware.value].value == HardwareType.ALL.name) \
  73. and (row[ColumHeader.Project.value].value == project.name
  74. or row[ColumHeader.Project.value].value == ProjectType.ALL.name):
  75. ret_data_table.append(get_item_by_item_type(item_type, row))
  76. return ret_data_table
  77. if __name__ == '__main__':
  78. table = get_xlsx_data_by_filter(ItemType.CUSTOM_PATTERN)
  79. print(table)