# -*- coding:utf-8 -*- from collections import OrderedDict from ssat_sdk.utils.string_util import strToList from UIT_log import error,info,debug from UIT_treeConstant import TreeConst from TConfig import TConfig DEBUG = True INFO = True ERROR = True g_level = TreeConst.G_LEVEL def parseMoveKey(keyStr): return strToList(keyStr,";") class CPathParams(): def __init__(self): self.paths = OrderedDict() def addParent(self,level, pDict): if level not in self.paths: self.paths[level] = {} # endif pDict["value"]=[] pDict[TreeConst.Option_MoveKey] = parseMoveKey(pDict[TreeConst.Option_MoveKey]) # print "CPathParams,addParent:", pDict if pDict[TreeConst.Parent_Name] in self.paths: error(self.cls, "addParent", "Parent %s conflict."%(pDict[TreeConst.Parent_Name]),ERROR) self.paths[level][pDict[TreeConst.Parent_Name]] = pDict def addOption(self,level, pDict): # print "CPathParams,addOption:", pDict if level in self.paths: if pDict[TreeConst.Parent_Name] in self.paths[level]: if pDict[TreeConst.Option_Name].__len__() > 0: self.paths[level][pDict[TreeConst.Parent_Name]]["value"].append(pDict) else: error(self.cls, "addOption", "Parent %s not exist." % (pDict[TreeConst.Parent_Name]), ERROR) else: error(self.cls, "addOption", "Level %s not exist." % (level), ERROR) class CValueParams(): cls = "CValueParams" def __init__(self): self.values = OrderedDict() def addParent(self, pDict): if pDict[TreeConst.Value_Name] in self.values: error(self.cls, "addParent", "Parent %s conflict."%(pDict[TreeConst.Value_Name]),ERROR) pDict["value"]=[] pDict[TreeConst.Value_MoveKey] = parseMoveKey(pDict[TreeConst.Value_MoveKey]) # print "CValueParams,addParent:", pDict self.values[pDict[TreeConst.Value_Name]] = pDict def addOption(self, pDict): # print "CValueParams,addOption:", pDict if pDict[TreeConst.Value_Name] in self.values: if pDict[TreeConst.Value_Value].__len__() > 0: self.values[pDict[TreeConst.Value_Name]]["value"].append(pDict) else: error(self.cls, "addOption", "Parent %s not exist." % (pDict[TreeConst.Value_Name]), ERROR) class CDialogParams(): cls = "CDialogParams" def __init__(self): self.dialogs = OrderedDict() def addParent(self,level, pDict): # endif if pDict[TreeConst.Parent_Name] in self.dialogs: error(self.cls, "addParent","Parent %s conflict."%(pDict[TreeConst.Parent_Name]), ERROR) pDict["value"] = [] pDict[TreeConst.Option_MoveKey] = parseMoveKey(pDict[TreeConst.Option_MoveKey]) # print "CDialogParams,addParent:", pDict self.dialogs[pDict[TreeConst.Parent_Name]] = pDict def addOption(self,level,pDict): # print "CDialogParams,addOption:", pDict if pDict[TreeConst.Parent_Name] in self.dialogs: if pDict[TreeConst.Option_Name].__len__() > 0: self.dialogs[pDict[TreeConst.Parent_Name]]["value"].append(pDict) else: error(self.cls, "addOption","Parent %s not exist.", ERROR) class UITTree(): def __init__(self,tConfig): self.pathParams = CPathParams() self.valueParams = CValueParams() self.dialogParams = CDialogParams() self.tConfig = tConfig self.cls = "UITTree" # 获取键对值 def get_pair_values(self, name, option=True): if type(name) == str: name = unicode(name) pairs = [] if option is True: find = False for level in self.pathParams.paths: for item in self.pathParams.paths[level]: if item.lower() == name.lower(): find = True for item in self.pathParams.paths[level][name]["value"]: pairs.append({item['option']: item['option_for_ocr'].split(';')}) break # endfor if find is True: break # endfor else: if name in self.valueParams.values: for item in self.valueParams.values[name]["value"]: pairs.append({item['value']: item['value_for_ocr'].split(';')}) # endif return pairs # 获取指定层级的path或value的xxx_for_ocr列表; def get_ocr_list(self, level, name, option, is_option=True): if type(name) == str: name = unicode(name) if type(option) == str: option = unicode(option) list_ocr = [] if is_option is True: find = False for item in self.pathParams.paths[level]: if item.lower() == name.lower(): find = True for item in self.pathParams.paths[level][name]["value"]: if str(option).lower() != str(item["option"]).lower(): list_ocr.append(item['option_for_ocr'].split(';')) break # endfor else: if name in self.valueParams.values: for item in self.valueParams.values[name]["value"]: list_ocr.append(item['value_for_ocr'].split(';')) # endif return list_ocr # 获取指定value_name所在菜单项的全部同级菜单项ocr列表; def get_parent_ocr_list(self, option): # 编码转换; if type(option) == str: option = unicode(option) found = False list_ocr = [] # 首先,字典不排序,需要倒序; paths = reversed(self.pathParams.paths) for level in paths: for parent in self.pathParams.paths[level]: for vals in self.pathParams.paths[level][parent]["value"]: if vals['option'].lower() == option.lower(): found = True break # endfor # 找到退出; if found is True: break # endfor # 找到退出; if found is True: break # endfor # 遍历value列表; if found is True: for val in self.pathParams.paths[level][parent]["value"]: list_ocr.extend(val['option_for_ocr'].split(';')) # 按长度排序; list_ocr.sort(key=lambda i: len(i), reverse=True) # 返回结果; return list_ocr # 当value_sheet = False,表示获取path表里的option层级的所有option_for_ocr:option # 当value_sheet = True,表示获取value表里的value_name层级的所有value_for_ocr:value def get_parent_ocr_dict(self, option, value_sheet=False): print "get_parent_ocr_dict.option:", option # 编码转换; if type(option) == str: option = unicode(option) found = False dict_ocr = {} list_ocr = [] if value_sheet is False: # 首先,字典不排序,需要倒序; paths = reversed(self.pathParams.paths) for level in paths: for parent in self.pathParams.paths[level]: for vals in self.pathParams.paths[level][parent]["value"]: if vals['option'].lower() == option.lower(): found = True break # endfor # 找到退出; if found is True: break # endfor # 找到退出; if found is True: break # 遍历value列表; if found is True: for val in self.pathParams.paths[level][parent]["value"]: list_ocr = val['option_for_ocr'].split(';') for ocr in list_ocr: dict_ocr[ocr.lower()] = val['option'] else: if option in self.valueParams.values: for val in self.valueParams.values[option]["value"]: list_ocr = val['value_for_ocr'].split(';') for ocr in list_ocr: dict_ocr[ocr.lower()] = val['value'] print 'unsorted=', dict_ocr # 按长度排序; list_ocr = sorted(dict_ocr.keys(), key=lambda key: len(key), reverse=True) dict_reuslt = OrderedDict() for ocr in list_ocr: dict_reuslt[ocr] = dict_ocr[ocr] # 返回结果; return dict_reuslt def get_option_ocr(self, option): # 编码转换; if type(option) == str: option = unicode(option) found = False list_ocr = [] # 首先,字典不排序,需要倒序; paths = reversed(self.pathParams.paths) for level in paths: for parent in self.pathParams.paths[level]: for vals in self.pathParams.paths[level][parent]["value"]: if vals['option'].lower() == option.lower(): list_ocr = vals['option_for_ocr'].split(';') found = True break # endfor # 找到退出; if found is True: break # endfor # 找到退出; if found is True: break # endfor # 返回结果; return list_ocr def get_value(self, option, value=""): # 编码转换; if type(option) == str: option = unicode(option) xlsValue = self.valueParams.values[option]["value"] valueList = [] valueOcrList = [] for item in xlsValue: valueList.append(item["value"]) valueOcrList.append(item["value_for_ocr"]) vp = {} if option in self.valueParams.values: vp = {"option": option, "value": valueList, "value_for_ocr": valueOcrList, "enter_key": self.valueParams.values[option]["enter_key"], "move_key": self.valueParams.values[option]["move_key"], "others": self.valueParams.values[option]['others']} # 找到value对应的ocr; if value != "": vp["value"] = value for item in self.valueParams.values[option]["value"]: if "range(" in item["value"]: vp["value_for_ocr"] = item["value_for_ocr"].split(';') break elif item["value"].lower() == value.lower(): vp["value_for_ocr"] = item["value_for_ocr"].split(';') break # endfor # endif # endif # 返回结果; return vp # 获取option层级内容; def get_option(self, option): # 编码转换; if type(option) == str: option = unicode(option) found = False layers = 0 dict_option = {} # 首先,字典不排序,需要倒序; paths = reversed(self.pathParams.paths) for level in paths: for parent in self.pathParams.paths[level]: for vals in self.pathParams.paths[level][parent]["value"]: if vals['option'].lower() == option.lower(): if dict_option.__len__() == 0: found = True dict_option['level'] = level dict_option['parent'] = parent dict_option['others'] = self.pathParams.paths[level][parent]['others'] dict_option['enter_key'] = self.pathParams.paths[level][parent]['enter_key'] dict_option['move_key'] = self.pathParams.paths[level][parent]['move_key'] dict_option['option_ocr'] = vals['option_for_ocr'].split(';') dict_option['option_move_key'] = vals['option_move_key'] dict_option['option_enter_key'] = vals['option_enter_key'] dict_option['option_others'] = vals['option_others'] option = parent layers += 1 # 层数; break # endfor # endfor # endfor if found is True: dict_option['layers'] = layers # first层次的parent名称; dict_option['first_parent'] = option # 返回结果; return found, dict_option # 获取表格Value层级中,value_name下包含的value列表 def getSubValueList(self, value_name): valueList = [] if self.valueParams.values.has_key(value_name): # print parser.valueParams.values["source"] # print parser.valueParams.values["source"]["value"] valueDictList = self.valueParams.values[value_name]["value"] for valueDict in valueDictList: valueList.append(valueDict["value"]) # print "valueList:", valueList, type(valueList) if valueList.__len__() == 1 and "range(" in valueList[0]: value_for_ocr_low = str(valueList[0]).lower() str_num = value_for_ocr_low.replace("range", "") list_num = eval(str_num) min_num = list_num[0] max_num = list_num[1] return [min_num, max_num] return valueList # 获取表格First~Sixth层级中,parent下包含的option列表 def getSubOptionList(self, parent): optionList = [] paths = self.pathParams.paths for level in paths: if paths[level].has_key(parent): optionDictList = paths[level][parent]["value"] for optionDict in optionDictList: optionList.append(optionDict["option"]) # print "optionList:", optionList, type(optionList) return optionList # 获取表格Value层级中,value_name下的value的ocr列表 def getValueTextList(self, value_name, value): valueTextList = [] if self.valueParams.values.has_key(value_name): # print parser.valueParams.values["source"] # print parser.valueParams.values["source"]["value"] valueDictList = self.valueParams.values[value_name]["value"] for valueDict in valueDictList: if valueDict["value"] == value: value_for_ocr = valueDict["value_for_ocr"] # print "value_for_ocr:", value_for_ocr, type(value_for_ocr) valueTextList = value_for_ocr.split(';') # print "valueTextList:", valueTextList, type(valueTextList) return valueTextList # 获取表格First~Sixth层级中,parent下的option的ocr列表 def getOptionTextList(self, parent, option): optionTextList = [] paths = self.pathParams.paths for level in paths: if paths[level].has_key(parent): optionDictList = paths[level][parent]["value"] for optionDict in optionDictList: if optionDict["option"] == option: option_for_ocr = optionDict["option_for_ocr"] # print "option_for_ocr:", option_for_ocr, type(option_for_ocr) optionTextList = option_for_ocr.split(';') # print "optionList:", optionTextList, type(optionTextList) return optionTextList