DataAccess.py 2.3 KB

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