OptionExcel.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # -*- coding:utf-8 -*-
  2. import os
  3. from collections import OrderedDict
  4. from UIT_PathManage import UITPathManage
  5. from TExcelParser import CExcelParser
  6. from ssat_sdk.utils.string_util import strToList
  7. from xlsConst import xlsConst as xlsc
  8. from BaseLog import CBaseLog
  9. from ExtraData import CExtraData
  10. def parseMoveKey(keyStr):
  11. return strToList(keyStr, ";")
  12. g_level = ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth',
  13. 'Seventh', 'Eighth', 'Ninth', 'Tenth', 'Eleventh', 'Twelfth']
  14. class CPathParams(CBaseLog):
  15. def __init__(self):
  16. self.paths = OrderedDict()
  17. def addParent(self, level, pDict):
  18. if level not in self.paths:
  19. self.paths[level] = {}
  20. # endif
  21. pDict["value"] = []
  22. pDict[xlsc.move_key] = parseMoveKey(pDict[xlsc.move_key])
  23. if pDict[xlsc.parent] in self.paths:
  24. self.error("Parent %s conflict." % (pDict[xlsc.parent]))
  25. self.paths[level][pDict[xlsc.parent]] = pDict
  26. def addOption(self, level, pDict):
  27. if level in self.paths:
  28. if pDict[xlsc.parent] in self.paths[level]:
  29. if pDict[xlsc.option].__len__() > 0:
  30. self.paths[level][pDict[xlsc.parent]]["value"].append(pDict)
  31. else:
  32. self.error("Parent %s not exist." % (pDict[xlsc.parent]))
  33. else:
  34. self.error("Level %s not exist." % (level))
  35. class CValueParams(CBaseLog):
  36. def __init__(self):
  37. self.values = OrderedDict()
  38. def addParent(self, pDict):
  39. if pDict[xlsc.value_name] in self.values:
  40. self.error("Parent %s conflict." % (pDict[xlsc.value_name]))
  41. pDict["value"] = []
  42. pDict[xlsc.move_key] = parseMoveKey(pDict[xlsc.move_key])
  43. self.values[pDict[xlsc.value_name]] = pDict
  44. def addOption(self, pDict):
  45. if pDict[xlsc.value_name] in self.values:
  46. if pDict[xlsc.value].__len__() > 0:
  47. self.values[pDict[xlsc.value_name]]["value"].append(pDict)
  48. else:
  49. self.error("Parent %s not exist." % (pDict[xlsc.value_name]))
  50. class CDialogParams(CBaseLog):
  51. def __init__(self):
  52. self.dialogs = OrderedDict()
  53. def addParent(self, level, pDict):
  54. # endif
  55. if pDict[xlsc.parent] in self.dialogs:
  56. self.error("Parent %s conflict." % (pDict[xlsc.parent]))
  57. pDict["value"] = []
  58. pDict[xlsc.move_key] = parseMoveKey(pDict[xlsc.move_key])
  59. self.dialogs[pDict[xlsc.parent]] = pDict
  60. def addOption(self, level, pDict):
  61. if pDict[xlsc.parent] in self.dialogs:
  62. if pDict[xlsc.option].__len__() > 0:
  63. self.dialogs[pDict[xlsc.parent]]["value"].append(pDict)
  64. else:
  65. self.error("Parent %s not exist." % pDict[xlsc.parent])
  66. # 注意:所有不对外暴露的变量和函数需要私有化,以明确哪些接口和参数是对外的。
  67. # 这样便于后期维护时,根据对外的变量和函数来做处理。
  68. class COptionExcel(CBaseLog):
  69. def __init__(self, exData):
  70. self.__excelParse = CExcelParser(self)
  71. self.__exData = exData
  72. self.__pathParams = CPathParams()
  73. self.__valueParams = CValueParams()
  74. self.__dialogParams = CDialogParams()
  75. # 加载excel;
  76. self.loadExcel()
  77. @property
  78. def pathParams(self):
  79. return self.__pathParams
  80. @property
  81. def valueParams(self):
  82. return self.__valueParams
  83. @property
  84. def dialogParams(self):
  85. return self.__dialogParams
  86. # 加载已知表格;
  87. def loadExcel(self):
  88. self.info(u"加载excel表")
  89. if self.__exData.listExcelPath.__len__() == 0:
  90. self.error(u"没有任何excel表格可加载")
  91. for xls in self.__exData.listExcelPath:
  92. self.info(u"当前加载:%s" % xls)
  93. self.__excelParse.read_excel(xls)
  94. # 加载其他表格;
  95. def addExcel(self, xlsPath):
  96. self.__excelParse.read_excel(xlsPath)
  97. # 获取指定option/value_name下的所有子项(option/value)的ocr键对值;
  98. # 示例:[{'suboption1': 'suboption1 ocr'}, {'suboption2': 'suboption2 ocr'}]
  99. def getOptionSubitemMap(self, optionName, isOption = True):
  100. if type(optionName) == str:
  101. optionName = unicode(optionName)
  102. pairs = []
  103. # 路径表或值表;
  104. if isOption is True:
  105. found = False
  106. for lev in self.__pathParams.paths:
  107. for item in self.__pathParams.paths[lev]:
  108. if item.lower() == optionName.lower():
  109. found = True
  110. for item in self.__pathParams.paths[lev][optionName]["value"]:
  111. pairs.append({item['option']: item['option_for_ocr'].split(';')})
  112. break
  113. #endif
  114. #endfor
  115. if found is True:
  116. break
  117. #endfor
  118. else:
  119. if optionName in self.__valueParams.values:
  120. for item in self.__valueParams.values[optionName]["value"]:
  121. pairs.append({item['value']: item['value_for_ocr'].split(';')})
  122. #endif
  123. return pairs
  124. # 获取指定option/value_name下的所有子项(option/value)的ocr值;
  125. # 示例:[['suboption1 ocr', 'ocr2', 'ocr3'],['suboption2 ocr'], ['suboption3 orc']]
  126. def getOptionSubItemOcrList(self, optionName, isOption = True):
  127. if type(optionName) == str:
  128. optionName = unicode(optionName)
  129. list_ocr = []
  130. # 路径表或值表;
  131. if isOption is True:
  132. found = False
  133. for lev in self.__pathParams.paths:
  134. for item in self.__pathParams.paths[lev]:
  135. if item.lower() == optionName.lower():
  136. found = True
  137. for item in self.__pathParams.paths[lev][optionName]["value"]:
  138. if optionName.lower() != item['option'].lower():
  139. list_ocr.append(item['option_for_ocr'].split(';'))
  140. break
  141. #endif
  142. #endfor
  143. if found is True:
  144. break
  145. #endfor
  146. else:
  147. if optionName in self.__valueParams.values:
  148. for item in self.__valueParams.values[optionName]["value"]:
  149. list_ocr.append(item['value_for_ocr'].split(';'))
  150. # endif
  151. return list_ocr
  152. def getOption
  153. if __name__ == "__main__":
  154. exData = CExtraData()
  155. opxls = COptionExcel(exData)
  156. # print opxls.pathParams.paths
  157. print u"getOptionMap", opxls.getOptionSubitemMap('picture')
  158. print u"getOptionSubItemOcrList", opxls.getOptionSubItemOcrList('picture', False)