OptionExcel.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. # -*- coding:utf-8 -*-
  2. import os, json
  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. '''
  98. 函数:获取指定option下的所有子项名称(option作为parent,获取该父项下的所有option字段)
  99. '''
  100. def getOptionAllChildItemName(self, optionName):
  101. optionList = []
  102. paths = self.__pathParams.paths
  103. for level in paths:
  104. if paths[level].has_key(optionName):
  105. optionDictList = paths[level][optionName]["value"]
  106. for optionDict in optionDictList:
  107. optionList.append(optionDict["option"])
  108. # print "optionList:", optionList, type(optionList)
  109. return optionList
  110. '''
  111. 函数:获取指定option下的所有子项名称(option作为parent,获取该父项下的所有option字段)
  112. '''
  113. def getOptionAllChildItemOcr(self, optionName):
  114. optionList = []
  115. paths = self.__pathParams.paths
  116. for level in paths:
  117. if paths[level].has_key(optionName):
  118. optionDictList = paths[level][optionName]["value"]
  119. for optionDict in optionDictList:
  120. optionList.append(optionDict["option"])
  121. # print "optionList:", optionList, type(optionList)
  122. return optionList
  123. '''
  124. 函数:获取指定option/value_name下的所有子项(option/value)的ocr键对值;
  125. 示例:
  126. [
  127. {'suboption1': 'suboption1 ocr'},
  128. {'suboption2': 'suboption2 ocr'}
  129. ]
  130. '''
  131. def getOptionAllChildItemMap(self, optionName, isOption=True):
  132. if type(optionName) == str:
  133. optionName = unicode(optionName)
  134. pairs = []
  135. # 路径表或值表;
  136. if isOption is True:
  137. found = False
  138. for lev in self.__pathParams.paths:
  139. for item in self.__pathParams.paths[lev]:
  140. if item.lower() == optionName.lower():
  141. found = True
  142. for item in self.__pathParams.paths[lev][optionName]["value"]:
  143. pairs.append({item['option']: item['option_for_ocr'].split(';')})
  144. break
  145. # endif
  146. # endfor
  147. if found is True:
  148. break
  149. # endfor
  150. else:
  151. if optionName in self.__valueParams.values:
  152. for item in self.__valueParams.values[optionName]["value"]:
  153. pairs.append({item['value']: item['value_for_ocr'].split(';')})
  154. # endif
  155. return pairs
  156. '''
  157. 函数:获取指定option/value_name下的所有兄弟项(相同parent)的ocr键对值;
  158. 示例:
  159. [
  160. {'suboption1': 'suboption1 ocr'},
  161. {'suboption2': 'suboption2 ocr'}
  162. ]
  163. '''
  164. def getOptionAllSiblingItemMap(self, optionName, isOption=True):
  165. pass
  166. '''
  167. 函数:获取指定option/value_name下的所有兄弟项(相同parent)的 ocr:option 字典值;
  168. 示例:
  169. {
  170. 'option1_ocr': 'option1',
  171. 'option2_ocr': 'option2',
  172. 'option3_ocr': 'option3',
  173. }
  174. '''
  175. def getOptionAllSiblingItemDict(self, optionName, isOption=True):
  176. # 编码转换;
  177. if type(optionName) == str:
  178. option = unicode(optionName)
  179. found = False
  180. dict_ocr = {}
  181. list_ocr = []
  182. if isOption is True:
  183. # 首先,字典不排序,需要倒序;
  184. paths = reversed(self.__pathParams.paths)
  185. for level in paths:
  186. for parent in self.__pathParams.paths[level]:
  187. for vals in self.__pathParams.paths[level][parent]["value"]:
  188. if vals['option'].lower() == option.lower():
  189. found = True
  190. break
  191. # endfor
  192. # 找到退出;
  193. if found is True:
  194. break
  195. # endfor
  196. # 找到退出;
  197. if found is True:
  198. break
  199. # 遍历value列表;
  200. if found is True:
  201. for val in self.__pathParams.paths[level][parent]["value"]:
  202. list_ocr = val['option_for_ocr'].split(';')
  203. for ocr in list_ocr:
  204. dict_ocr[ocr.lower()] = val['option']
  205. else:
  206. if option in self.__valueParams.values:
  207. for val in self.__valueParams.values[option]["value"]:
  208. list_ocr = val['value_for_ocr'].split(';')
  209. for ocr in list_ocr:
  210. dict_ocr[ocr.lower()] = val['value']
  211. print 'unsorted=', dict_ocr
  212. # 按长度排序;
  213. list_ocr = sorted(dict_ocr.keys(), key=lambda key: len(key), reverse=True)
  214. dict_reuslt = OrderedDict()
  215. for ocr in list_ocr:
  216. dict_reuslt[ocr] = dict_ocr[ocr]
  217. # 返回结果;
  218. return dict_reuslt
  219. '''
  220. 函数:获取指定option/value_name下的所有子项(option/value)的ocr值, 1d表示One-dimensional(一维数组);
  221. 示例:
  222. [
  223. ['suboption1 ocr', 'ocr2', 'ocr3'],
  224. ['suboption2 ocr'],
  225. ['suboption3 orc']
  226. ]
  227. '''
  228. def getOptionAllChildItemOcrList1d(self, optionName, isOption=True):
  229. if type(optionName) == str:
  230. optionName = unicode(optionName)
  231. list_ocr = []
  232. # 路径表或值表;
  233. if isOption is True:
  234. found = False
  235. for lev in self.__pathParams.paths:
  236. for parent in self.__pathParams.paths[lev]:
  237. if parent.lower() == optionName.lower():
  238. found = True
  239. for item in self.__pathParams.paths[lev][optionName]["value"]:
  240. list_ocr.extend(item['option_for_ocr'].split(';'))
  241. break
  242. # endif
  243. # endfor
  244. if found is True:
  245. break
  246. # endfor
  247. else:
  248. if optionName in self.__valueParams.values:
  249. self.info(u"找到%s的value表数据" % optionName)
  250. for item in self.__valueParams.values[optionName]["value"]:
  251. list_ocr.extend(item['value_for_ocr'].split(';'))
  252. else:
  253. self.error(u"没有找到%s的value表数据" % optionName)
  254. # endif
  255. return list_ocr
  256. '''
  257. 函数:获取指定option/value_name下的所有子项(option/value)的ocr值,2d表示Two-dimensional(二维数组);
  258. 示例:
  259. [
  260. ['suboption1 ocr', 'ocr2', 'ocr3'],
  261. ['suboption2 ocr'],
  262. ['suboption3 orc']
  263. ]
  264. '''
  265. def getOptionAllChildItemOcrList2d(self, optionName, isOption=True):
  266. if type(optionName) == str:
  267. optionName = unicode(optionName)
  268. list_ocr = []
  269. # 路径表或值表;
  270. if isOption is True:
  271. found = False
  272. for lev in self.__pathParams.paths:
  273. for parent in self.__pathParams.paths[lev]:
  274. if parent.lower() == optionName.lower():
  275. found = True
  276. for item in self.__pathParams.paths[lev][optionName]["value"]:
  277. list_ocr.append(item['option_for_ocr'].split(';'))
  278. break
  279. # endif
  280. # endfor
  281. if found is True:
  282. break
  283. # endfor
  284. else:
  285. if optionName in self.__valueParams.values:
  286. self.info(u"找到%s的value表数据" % optionName)
  287. for item in self.__valueParams.values[optionName]["value"]:
  288. list_ocr.append(item['value_for_ocr'].split(';'))
  289. else:
  290. self.error(u"没有找到%s的value表数据" % optionName)
  291. # endif
  292. return list_ocr
  293. '''
  294. 函数:获取指定option所有兄弟项(相同parent)的ocr值, 1d表示One-dimensional(一维数组);
  295. 示例:
  296. [
  297. ['option1 ocr', 'ocr2', 'ocr3'],
  298. ['option2 ocr'],
  299. ['option3 orc']
  300. ]
  301. '''
  302. def getOptionAllSiblingItemOcrList1d(self, optionName):
  303. # 编码转换;
  304. if type(optionName) == str:
  305. optionName = unicode(optionName)
  306. found = False
  307. list_ocr = []
  308. # 首先,字典不排序,需要倒序;
  309. paths = reversed(self.__pathParams.paths)
  310. for level in paths:
  311. for parent in self.__pathParams.paths[level]:
  312. for vals in self.__pathParams.paths[level][parent]["value"]:
  313. if vals['option'].lower() == optionName.lower():
  314. found = True
  315. break
  316. # endfor
  317. # 找到退出;
  318. if found is True:
  319. break
  320. # endfor
  321. # 找到退出;
  322. if found is True:
  323. break
  324. # endfor
  325. # 遍历value列表;
  326. if found is True:
  327. for val in self.__pathParams.paths[level][parent]["value"]:
  328. list_ocr.extend(val['option_for_ocr'].split(';'))
  329. # 按长度排序;
  330. list_ocr.sort(key=lambda i: len(i), reverse=True)
  331. # 返回结果;
  332. return list_ocr
  333. '''
  334. 函数:获取指定option的ocr,以list返回
  335. '''
  336. def getOptionOcr(self, optionName):
  337. # 编码转换;
  338. if type(optionName) == str:
  339. option = unicode(optionName)
  340. found = False
  341. list_ocr = []
  342. # 首先,字典不排序,需要倒序;
  343. paths = reversed(self.__pathParams.paths)
  344. for level in paths:
  345. for parent in self.__pathParams.paths[level]:
  346. for vals in self.__pathParams.paths[level][parent]["value"]:
  347. if vals['option'].lower() == option.lower():
  348. list_ocr = vals['option_for_ocr'].split(';')
  349. found = True
  350. break
  351. # endfor
  352. # 找到退出;
  353. if found is True:
  354. break
  355. # endfor
  356. # 找到退出;
  357. if found is True:
  358. break
  359. # endfor
  360. # 返回结果;
  361. return list_ocr
  362. '''
  363. 函数:获取指定option的value表值,如果参数value未指定具体值则获取全部valueName的值;
  364. 示例:
  365. {
  366. "option":"option名称",
  367. "values":[option在value表中的所有value字段],
  368. "value_for_ocr":[option在value表中所有value_for_ocr字段],
  369. "enter_key":"",
  370. "move_key":"",
  371. "others":"",
  372. }
  373. '''
  374. def getOptionValues(self, optionName, value=""):
  375. # 编码转换;
  376. if type(optionName) == str:
  377. option = unicode(optionName)
  378. # option/value name是否在value表中的;
  379. if optionName not in self.__valueParams.values:
  380. return []
  381. optValues = self.valueParams.values[option]["value"]
  382. valueList = []
  383. valueOcrList = []
  384. for item in optValues:
  385. valueList.append(item["value"])
  386. valueOcrList.append(item["value_for_ocr"])
  387. vp = {"option": option, "value": valueList, "value_for_ocr": valueOcrList,
  388. "enter_key": self.valueParams.values[option]["enter_key"],
  389. "move_key": self.valueParams.values[option]["move_key"],
  390. "others": self.valueParams.values[option]['others']}
  391. # 找到value对应的ocr;
  392. if value != "":
  393. vp["value"] = value
  394. for item in self.valueParams.values[option]["value"]:
  395. if "range(" in item["value"]:
  396. vp["value_for_ocr"] = item["value_for_ocr"].split(';')
  397. break
  398. elif item["value"].lower() == value.lower():
  399. vp["value_for_ocr"] = item["value_for_ocr"].split(';')
  400. break
  401. # endfor
  402. # endif
  403. # 返回结果;
  404. return vp
  405. def getOptionInfo(self, optionName):
  406. # 编码转换;
  407. if type(optionName) == str:
  408. option = unicode(optionName)
  409. found = False
  410. layers = 0
  411. dict_option = {}
  412. # 首先,字典不排序,需要倒序;
  413. paths = reversed(self.__pathParams.paths)
  414. for level in paths:
  415. for parent in self.__pathParams.paths[level]:
  416. for vals in self.__pathParams.paths[level][parent]["value"]:
  417. if vals['option'].lower() == option.lower():
  418. if dict_option.__len__() == 0:
  419. found = True
  420. dict_option['level'] = level
  421. dict_option['parent'] = parent
  422. dict_option['others'] = self.__pathParams.paths[level][parent]['others']
  423. dict_option['enter_key'] = self.__pathParams.paths[level][parent]['enter_key']
  424. dict_option['move_key'] = self.__pathParams.paths[level][parent]['move_key']
  425. dict_option['option_ocr'] = vals['option_for_ocr'].split(';')
  426. dict_option['option_move_key'] = vals['option_move_key']
  427. dict_option['option_enter_key'] = vals['option_enter_key']
  428. dict_option['option_others'] = vals['option_others']
  429. option = parent
  430. layers += 1 # 层数;
  431. break
  432. # endfor
  433. # endfor
  434. # endfor
  435. if found is True:
  436. dict_option['layers'] = layers
  437. # first层次的parent名称;
  438. dict_option['first_parent'] = option
  439. # 返回结果;
  440. return found, dict_option
  441. '''
  442. 函数:获取指定的option在value表中的所有value字段;
  443. '''
  444. def getOptionAllValueName(self, optionName):
  445. valueList = []
  446. if self.valueParams.values.has_key(optionName):
  447. valueDictList = self.valueParams.values[optionName]["value"]
  448. for valueDict in valueDictList:
  449. valueList.append(valueDict["value"])
  450. # print "valueList:", valueList, type(valueList)
  451. if valueList.__len__() == 1 and "range(" in valueList[0]:
  452. value_for_ocr_low = str(valueList[0]).lower()
  453. str_num = value_for_ocr_low.replace("range", "")
  454. list_num = eval(str_num)
  455. min_num = list_num[0]
  456. max_num = list_num[1]
  457. return [min_num, max_num]
  458. return valueList
  459. if __name__ == "__main__":
  460. exData = CExtraData()
  461. opxls = COptionExcel(exData)
  462. # print json.dumps(opxls.pathParams.paths)
  463. # print u"getOptionMap", opxls.getOptionAllChildItemMap('picture')
  464. # print u"getOptionAllChildItemOcrList1d", opxls.getOptionAllChildItemOcrList1d('picture', False)
  465. # print u"getOptionAllChildItemOcrList2d", opxls.getOptionAllChildItemOcrList2d('picture', False)
  466. # print "getOptionAllSiblingItemOcrList", opxls.getOptionAllSiblingItemOcrList('picture')
  467. print "getOptionAllChildItem", opxls.getOptionAllChildItem('picture')