UIT_tree.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. # -*- coding:utf-8 -*-
  2. from collections import OrderedDict
  3. from ssat_sdk.utils.string_util import strToList
  4. from UIT_log import error,info,debug
  5. from UIT_treeConstant import TreeConst
  6. from TConfig import TConfig
  7. DEBUG = True
  8. INFO = True
  9. ERROR = True
  10. g_level = TreeConst.G_LEVEL
  11. def parseMoveKey(keyStr):
  12. return strToList(keyStr,";")
  13. class CPathParams():
  14. def __init__(self):
  15. self.paths = OrderedDict()
  16. def addParent(self,level, pDict):
  17. if level not in self.paths:
  18. self.paths[level] = {}
  19. # endif
  20. pDict["value"]=[]
  21. pDict[TreeConst.Option_MoveKey] = parseMoveKey(pDict[TreeConst.Option_MoveKey])
  22. # print "CPathParams,addParent:", pDict
  23. if pDict[TreeConst.Parent_Name] in self.paths:
  24. error(self.cls, "addParent", "Parent %s conflict."%(pDict[TreeConst.Parent_Name]),ERROR)
  25. self.paths[level][pDict[TreeConst.Parent_Name]] = pDict
  26. def addOption(self,level, pDict):
  27. # print "CPathParams,addOption:", pDict
  28. if level in self.paths:
  29. if pDict[TreeConst.Parent_Name] in self.paths[level]:
  30. if pDict[TreeConst.Option_Name].__len__() > 0:
  31. self.paths[level][pDict[TreeConst.Parent_Name]]["value"].append(pDict)
  32. else:
  33. error(self.cls, "addOption", "Parent %s not exist." % (pDict[TreeConst.Parent_Name]), ERROR)
  34. else:
  35. error(self.cls, "addOption", "Level %s not exist." % (level), ERROR)
  36. class CValueParams():
  37. cls = "CValueParams"
  38. def __init__(self):
  39. self.values = OrderedDict()
  40. def addParent(self, pDict):
  41. if pDict[TreeConst.Value_Name] in self.values:
  42. error(self.cls, "addParent", "Parent %s conflict."%(pDict[TreeConst.Value_Name]),ERROR)
  43. pDict["value"]=[]
  44. pDict[TreeConst.Value_MoveKey] = parseMoveKey(pDict[TreeConst.Value_MoveKey])
  45. # print "CValueParams,addParent:", pDict
  46. self.values[pDict[TreeConst.Value_Name]] = pDict
  47. def addOption(self, pDict):
  48. # print "CValueParams,addOption:", pDict
  49. if pDict[TreeConst.Value_Name] in self.values:
  50. if pDict[TreeConst.Value_Value].__len__() > 0:
  51. self.values[pDict[TreeConst.Value_Name]]["value"].append(pDict)
  52. else:
  53. error(self.cls, "addOption", "Parent %s not exist." % (pDict[TreeConst.Value_Name]), ERROR)
  54. class CDialogParams():
  55. cls = "CDialogParams"
  56. def __init__(self):
  57. self.dialogs = OrderedDict()
  58. def addParent(self,level, pDict):
  59. # endif
  60. if pDict[TreeConst.Parent_Name] in self.dialogs:
  61. error(self.cls, "addParent","Parent %s conflict."%(pDict[TreeConst.Parent_Name]), ERROR)
  62. pDict["value"] = []
  63. pDict[TreeConst.Option_MoveKey] = parseMoveKey(pDict[TreeConst.Option_MoveKey])
  64. # print "CDialogParams,addParent:", pDict
  65. self.dialogs[pDict[TreeConst.Parent_Name]] = pDict
  66. def addOption(self,level,pDict):
  67. # print "CDialogParams,addOption:", pDict
  68. if pDict[TreeConst.Parent_Name] in self.dialogs:
  69. if pDict[TreeConst.Option_Name].__len__() > 0:
  70. self.dialogs[pDict[TreeConst.Parent_Name]]["value"].append(pDict)
  71. else:
  72. error(self.cls, "addOption","Parent %s not exist.", ERROR)
  73. class UITTree():
  74. def __init__(self,tConfig):
  75. self.pathParams = CPathParams()
  76. self.valueParams = CValueParams()
  77. self.dialogParams = CDialogParams()
  78. self.tConfig = tConfig
  79. self.cls = "UITTree"
  80. # 获取键对值
  81. def get_pair_values(self, name, option=True):
  82. if type(name) == str:
  83. name = unicode(name)
  84. pairs = []
  85. if option is True:
  86. find = False
  87. for level in self.pathParams.paths:
  88. for item in self.pathParams.paths[level]:
  89. if item.lower() == name.lower():
  90. find = True
  91. for item in self.pathParams.paths[level][name]["value"]:
  92. pairs.append({item['option']: item['option_for_ocr'].split(';')})
  93. break
  94. # endfor
  95. if find is True:
  96. break
  97. # endfor
  98. else:
  99. if name in self.valueParams.values:
  100. for item in self.valueParams.values[name]["value"]:
  101. pairs.append({item['value']: item['value_for_ocr'].split(';')})
  102. # endif
  103. return pairs
  104. # 获取指定层级的path或value的xxx_for_ocr列表;
  105. def get_ocr_list(self, level, name, option, is_option=True):
  106. if type(name) == str:
  107. name = unicode(name)
  108. if type(option) == str:
  109. option = unicode(option)
  110. list_ocr = []
  111. if is_option is True:
  112. find = False
  113. for item in self.pathParams.paths[level]:
  114. if item.lower() == name.lower():
  115. find = True
  116. for item in self.pathParams.paths[level][name]["value"]:
  117. if str(option).lower() != str(item["option"]).lower():
  118. list_ocr.append(item['option_for_ocr'].split(';'))
  119. break
  120. # endfor
  121. else:
  122. if name in self.valueParams.values:
  123. for item in self.valueParams.values[name]["value"]:
  124. list_ocr.append(item['value_for_ocr'].split(';'))
  125. # endif
  126. return list_ocr
  127. # 获取指定value_name所在菜单项的全部同级菜单项ocr列表;
  128. def get_parent_ocr_list(self, option):
  129. # 编码转换;
  130. if type(option) == str:
  131. option = unicode(option)
  132. found = False
  133. list_ocr = []
  134. # 首先,字典不排序,需要倒序;
  135. paths = reversed(self.pathParams.paths)
  136. for level in paths:
  137. for parent in self.pathParams.paths[level]:
  138. for vals in self.pathParams.paths[level][parent]["value"]:
  139. if vals['option'].lower() == option.lower():
  140. found = True
  141. break
  142. # endfor
  143. # 找到退出;
  144. if found is True:
  145. break
  146. # endfor
  147. # 找到退出;
  148. if found is True:
  149. break
  150. # endfor
  151. # 遍历value列表;
  152. if found is True:
  153. for val in self.pathParams.paths[level][parent]["value"]:
  154. list_ocr.extend(val['option_for_ocr'].split(';'))
  155. # 按长度排序;
  156. list_ocr.sort(key=lambda i: len(i), reverse=True)
  157. # 返回结果;
  158. return list_ocr
  159. # 当value_sheet = False,表示获取path表里的option层级的所有option_for_ocr:option
  160. # 当value_sheet = True,表示获取value表里的value_name层级的所有value_for_ocr:value
  161. def get_parent_ocr_dict(self, option, value_sheet=False):
  162. print "get_parent_ocr_dict.option:", option
  163. # 编码转换;
  164. if type(option) == str:
  165. option = unicode(option)
  166. found = False
  167. dict_ocr = {}
  168. list_ocr = []
  169. if value_sheet is False:
  170. # 首先,字典不排序,需要倒序;
  171. paths = reversed(self.pathParams.paths)
  172. for level in paths:
  173. for parent in self.pathParams.paths[level]:
  174. for vals in self.pathParams.paths[level][parent]["value"]:
  175. if vals['option'].lower() == option.lower():
  176. found = True
  177. break
  178. # endfor
  179. # 找到退出;
  180. if found is True:
  181. break
  182. # endfor
  183. # 找到退出;
  184. if found is True:
  185. break
  186. # 遍历value列表;
  187. if found is True:
  188. for val in self.pathParams.paths[level][parent]["value"]:
  189. list_ocr = val['option_for_ocr'].split(';')
  190. for ocr in list_ocr:
  191. dict_ocr[ocr.lower()] = val['option']
  192. else:
  193. if option in self.valueParams.values:
  194. for val in self.valueParams.values[option]["value"]:
  195. list_ocr = val['value_for_ocr'].split(';')
  196. for ocr in list_ocr:
  197. dict_ocr[ocr.lower()] = val['value']
  198. print 'unsorted=', dict_ocr
  199. # 按长度排序;
  200. list_ocr = sorted(dict_ocr.keys(), key=lambda key: len(key), reverse=True)
  201. dict_reuslt = OrderedDict()
  202. for ocr in list_ocr:
  203. dict_reuslt[ocr] = dict_ocr[ocr]
  204. # 返回结果;
  205. return dict_reuslt
  206. def get_option_ocr(self, option):
  207. # 编码转换;
  208. if type(option) == str:
  209. option = unicode(option)
  210. found = False
  211. list_ocr = []
  212. # 首先,字典不排序,需要倒序;
  213. paths = reversed(self.pathParams.paths)
  214. for level in paths:
  215. for parent in self.pathParams.paths[level]:
  216. for vals in self.pathParams.paths[level][parent]["value"]:
  217. if vals['option'].lower() == option.lower():
  218. list_ocr = vals['option_for_ocr'].split(';')
  219. found = True
  220. break
  221. # endfor
  222. # 找到退出;
  223. if found is True:
  224. break
  225. # endfor
  226. # 找到退出;
  227. if found is True:
  228. break
  229. # endfor
  230. # 返回结果;
  231. return list_ocr
  232. def get_value(self, option, value=""):
  233. # 编码转换;
  234. if type(option) == str:
  235. option = unicode(option)
  236. xlsValue = self.valueParams.values[option]["value"]
  237. valueList = []
  238. valueOcrList = []
  239. for item in xlsValue:
  240. valueList.append(item["value"])
  241. valueOcrList.append(item["value_for_ocr"])
  242. vp = {}
  243. if option in self.valueParams.values:
  244. vp = {"option": option, "value": valueList, "value_for_ocr": valueOcrList,
  245. "enter_key": self.valueParams.values[option]["enter_key"],
  246. "move_key": self.valueParams.values[option]["move_key"],
  247. "others": self.valueParams.values[option]['others']}
  248. # 找到value对应的ocr;
  249. if value != "":
  250. vp["value"] = value
  251. for item in self.valueParams.values[option]["value"]:
  252. if "range(" in item["value"]:
  253. vp["value_for_ocr"] = item["value_for_ocr"].split(';')
  254. break
  255. elif item["value"].lower() == value.lower():
  256. vp["value_for_ocr"] = item["value_for_ocr"].split(';')
  257. break
  258. # endfor
  259. # endif
  260. # endif
  261. # 返回结果;
  262. return vp
  263. # 获取option层级内容;
  264. def get_option(self, option):
  265. # 编码转换;
  266. if type(option) == str:
  267. option = unicode(option)
  268. found = False
  269. layers = 0
  270. dict_option = {}
  271. # 首先,字典不排序,需要倒序;
  272. paths = reversed(self.pathParams.paths)
  273. for level in paths:
  274. for parent in self.pathParams.paths[level]:
  275. for vals in self.pathParams.paths[level][parent]["value"]:
  276. if vals['option'].lower() == option.lower():
  277. if dict_option.__len__() == 0:
  278. found = True
  279. dict_option['level'] = level
  280. dict_option['parent'] = parent
  281. dict_option['others'] = self.pathParams.paths[level][parent]['others']
  282. dict_option['enter_key'] = self.pathParams.paths[level][parent]['enter_key']
  283. dict_option['move_key'] = self.pathParams.paths[level][parent]['move_key']
  284. dict_option['option_ocr'] = vals['option_for_ocr'].split(';')
  285. dict_option['option_move_key'] = vals['option_move_key']
  286. dict_option['option_enter_key'] = vals['option_enter_key']
  287. dict_option['option_others'] = vals['option_others']
  288. option = parent
  289. layers += 1 # 层数;
  290. break
  291. # endfor
  292. # endfor
  293. # endfor
  294. if found is True:
  295. dict_option['layers'] = layers
  296. # first层次的parent名称;
  297. dict_option['first_parent'] = option
  298. # 返回结果;
  299. return found, dict_option
  300. # 获取表格Value层级中,value_name下包含的value列表
  301. def getSubValueList(self, value_name):
  302. valueList = []
  303. if self.valueParams.values.has_key(value_name):
  304. # print parser.valueParams.values["source"]
  305. # print parser.valueParams.values["source"]["value"]
  306. valueDictList = self.valueParams.values[value_name]["value"]
  307. for valueDict in valueDictList:
  308. valueList.append(valueDict["value"])
  309. # print "valueList:", valueList, type(valueList)
  310. if valueList.__len__() == 1 and "range(" in valueList[0]:
  311. value_for_ocr_low = str(valueList[0]).lower()
  312. str_num = value_for_ocr_low.replace("range", "")
  313. list_num = eval(str_num)
  314. min_num = list_num[0]
  315. max_num = list_num[1]
  316. return [min_num, max_num]
  317. return valueList
  318. # 获取表格First~Sixth层级中,parent下包含的option列表
  319. def getSubOptionList(self, parent):
  320. optionList = []
  321. paths = self.pathParams.paths
  322. for level in paths:
  323. if paths[level].has_key(parent):
  324. optionDictList = paths[level][parent]["value"]
  325. for optionDict in optionDictList:
  326. optionList.append(optionDict["option"])
  327. # print "optionList:", optionList, type(optionList)
  328. return optionList
  329. # 获取表格Value层级中,value_name下的value的ocr列表
  330. def getValueTextList(self, value_name, value):
  331. valueTextList = []
  332. if self.valueParams.values.has_key(value_name):
  333. # print parser.valueParams.values["source"]
  334. # print parser.valueParams.values["source"]["value"]
  335. valueDictList = self.valueParams.values[value_name]["value"]
  336. for valueDict in valueDictList:
  337. if valueDict["value"] == value:
  338. value_for_ocr = valueDict["value_for_ocr"]
  339. # print "value_for_ocr:", value_for_ocr, type(value_for_ocr)
  340. valueTextList = value_for_ocr.split(';')
  341. # print "valueTextList:", valueTextList, type(valueTextList)
  342. return valueTextList
  343. # 获取表格First~Sixth层级中,parent下的option的ocr列表
  344. def getOptionTextList(self, parent, option):
  345. optionTextList = []
  346. paths = self.pathParams.paths
  347. for level in paths:
  348. if paths[level].has_key(parent):
  349. optionDictList = paths[level][parent]["value"]
  350. for optionDict in optionDictList:
  351. if optionDict["option"] == option:
  352. option_for_ocr = optionDict["option_for_ocr"]
  353. # print "option_for_ocr:", option_for_ocr, type(option_for_ocr)
  354. optionTextList = option_for_ocr.split(';')
  355. # print "optionList:", optionTextList, type(optionTextList)
  356. return optionTextList