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