# -*- coding:utf-8 -*- import os, json from collections import OrderedDict from UIT_PathManage import UITPathManage from TExcelParser import CExcelParser from ssat_sdk.utils.string_util import strToList from xlsConst import xlsConst as xlsc from BaseLog import CBaseLog from ExtraData import CExtraData def parseMoveKey(keyStr): return strToList(keyStr, ";") g_level = ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh', 'Eighth', 'Ninth', 'Tenth', 'Eleventh', 'Twelfth'] class CPathParams(CBaseLog): def __init__(self): self.paths = OrderedDict() def addParent(self, level, pDict): if level not in self.paths: self.paths[level] = {} # endif pDict["value"] = [] pDict[xlsc.move_key] = parseMoveKey(pDict[xlsc.move_key]) if pDict[xlsc.parent] in self.paths: self.error("Parent %s conflict." % (pDict[xlsc.parent])) self.paths[level][pDict[xlsc.parent]] = pDict def addOption(self, level, pDict): if level in self.paths: if pDict[xlsc.parent] in self.paths[level]: if pDict[xlsc.option].__len__() > 0: self.paths[level][pDict[xlsc.parent]]["value"].append(pDict) else: self.error("Parent %s not exist." % (pDict[xlsc.parent])) else: self.error("Level %s not exist." % (level)) class CValueParams(CBaseLog): def __init__(self): self.values = OrderedDict() def addParent(self, pDict): if pDict[xlsc.value_name] in self.values: self.error("Parent %s conflict." % (pDict[xlsc.value_name])) pDict["value"] = [] pDict[xlsc.move_key] = parseMoveKey(pDict[xlsc.move_key]) self.values[pDict[xlsc.value_name]] = pDict def addOption(self, pDict): if pDict[xlsc.value_name] in self.values: if pDict[xlsc.value].__len__() > 0: self.values[pDict[xlsc.value_name]]["value"].append(pDict) else: self.error("Parent %s not exist." % (pDict[xlsc.value_name])) class CDialogParams(CBaseLog): def __init__(self): self.dialogs = OrderedDict() def addParent(self, level, pDict): # endif if pDict[xlsc.parent] in self.dialogs: self.error("Parent %s conflict." % (pDict[xlsc.parent])) pDict["value"] = [] pDict[xlsc.move_key] = parseMoveKey(pDict[xlsc.move_key]) self.dialogs[pDict[xlsc.parent]] = pDict def addOption(self, level, pDict): if pDict[xlsc.parent] in self.dialogs: if pDict[xlsc.option].__len__() > 0: self.dialogs[pDict[xlsc.parent]]["value"].append(pDict) else: self.error("Parent %s not exist." % pDict[xlsc.parent]) # 注意:所有不对外暴露的变量和函数需要私有化,以明确哪些接口和参数是对外的。 # 这样便于后期维护时,根据对外的变量和函数来做处理。 class COptionExcel(CBaseLog): def __init__(self, exData): self.__excelParse = CExcelParser(self) self.__exData = exData self.__pathParams = CPathParams() self.__valueParams = CValueParams() self.__dialogParams = CDialogParams() # 加载excel; self.loadExcel() @property def pathParams(self): return self.__pathParams @property def valueParams(self): return self.__valueParams @property def dialogParams(self): return self.__dialogParams # 加载已知表格; def loadExcel(self): self.info(u"加载excel表") if self.__exData.listExcelPath.__len__() == 0: self.error(u"没有任何excel表格可加载") for xls in self.__exData.listExcelPath: self.info(u"当前加载:%s" % xls) self.__excelParse.read_excel(xls) # 加载其他表格; def addExcel(self, xlsPath): self.__excelParse.read_excel(xlsPath) ''' 函数:获取指定option下的所有子项名称(option作为parent,获取该父项下的所有option字段) ''' def getOptionAllChildItemName(self, optionName): optionList = [] paths = self.__pathParams.paths for level in paths: if paths[level].has_key(optionName): optionDictList = paths[level][optionName]["value"] for optionDict in optionDictList: optionList.append(optionDict["option"]) # print "optionList:", optionList, type(optionList) return optionList ''' 函数:获取指定option下的所有子项名称(option作为parent,获取该父项下的所有option字段) ''' def getOptionAllChildItemOcr(self, optionName): optionList = [] paths = self.__pathParams.paths for level in paths: if paths[level].has_key(optionName): optionDictList = paths[level][optionName]["value"] for optionDict in optionDictList: optionList.append(optionDict["option"]) # print "optionList:", optionList, type(optionList) return optionList ''' 函数:获取指定option/value_name下的所有子项(option/value)的ocr键对值; 示例: [ {'suboption1': 'suboption1 ocr'}, {'suboption2': 'suboption2 ocr'} ] ''' def getOptionAllChildItemMap(self, optionName, isOption=True): if type(optionName) == str: optionName = unicode(optionName) pairs = [] # 路径表或值表; if isOption is True: found = False for lev in self.__pathParams.paths: for item in self.__pathParams.paths[lev]: if item.lower() == optionName.lower(): found = True for item in self.__pathParams.paths[lev][optionName]["value"]: pairs.append({item['option']: item['option_for_ocr'].split(';')}) break # endif # endfor if found is True: break # endfor else: if optionName in self.__valueParams.values: for item in self.__valueParams.values[optionName]["value"]: pairs.append({item['value']: item['value_for_ocr'].split(';')}) # endif return pairs ''' 函数:获取指定option/value_name下的所有兄弟项(相同parent)的ocr键对值; 示例: [ {'suboption1': 'suboption1 ocr'}, {'suboption2': 'suboption2 ocr'} ] ''' def getOptionAllSiblingItemMap(self, optionName, isOption=True): pass ''' 函数:获取指定option/value_name下的所有兄弟项(相同parent)的 ocr:option 字典值; 示例: { 'option1_ocr': 'option1', 'option2_ocr': 'option2', 'option3_ocr': 'option3', } ''' def getOptionAllSiblingItemDict(self, optionName, isOption=True): # 编码转换; if type(optionName) == str: option = unicode(optionName) found = False dict_ocr = {} list_ocr = [] if isOption is True: # 首先,字典不排序,需要倒序; 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 ''' 函数:获取指定option/value_name下的所有子项(option/value)的ocr值, 1d表示One-dimensional(一维数组); 示例: [ ['suboption1 ocr', 'ocr2', 'ocr3'], ['suboption2 ocr'], ['suboption3 orc'] ] ''' def getOptionAllChildItemOcrList1d(self, optionName, isOption=True): if type(optionName) == str: optionName = unicode(optionName) list_ocr = [] # 路径表或值表; if isOption is True: found = False for lev in self.__pathParams.paths: for parent in self.__pathParams.paths[lev]: if parent.lower() == optionName.lower(): found = True for item in self.__pathParams.paths[lev][optionName]["value"]: list_ocr.extend(item['option_for_ocr'].split(';')) break # endif # endfor if found is True: break # endfor else: if optionName in self.__valueParams.values: self.info(u"找到%s的value表数据" % optionName) for item in self.__valueParams.values[optionName]["value"]: list_ocr.extend(item['value_for_ocr'].split(';')) else: self.error(u"没有找到%s的value表数据" % optionName) # endif return list_ocr ''' 函数:获取指定option/value_name下的所有子项(option/value)的ocr值,2d表示Two-dimensional(二维数组); 示例: [ ['suboption1 ocr', 'ocr2', 'ocr3'], ['suboption2 ocr'], ['suboption3 orc'] ] ''' def getOptionAllChildItemOcrList2d(self, optionName, isOption=True): if type(optionName) == str: optionName = unicode(optionName) list_ocr = [] # 路径表或值表; if isOption is True: found = False for lev in self.__pathParams.paths: for parent in self.__pathParams.paths[lev]: if parent.lower() == optionName.lower(): found = True for item in self.__pathParams.paths[lev][optionName]["value"]: list_ocr.append(item['option_for_ocr'].split(';')) break # endif # endfor if found is True: break # endfor else: if optionName in self.__valueParams.values: self.info(u"找到%s的value表数据" % optionName) for item in self.__valueParams.values[optionName]["value"]: list_ocr.append(item['value_for_ocr'].split(';')) else: self.error(u"没有找到%s的value表数据" % optionName) # endif return list_ocr ''' 函数:获取指定option所有兄弟项(相同parent)的ocr值, 1d表示One-dimensional(一维数组); 示例: [ ['option1 ocr', 'ocr2', 'ocr3'], ['option2 ocr'], ['option3 orc'] ] ''' def getOptionAllSiblingItemOcrList1d(self, optionName): # 编码转换; if type(optionName) == str: optionName = unicode(optionName) 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() == optionName.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 ''' 函数:获取指定option的ocr,以list返回 ''' def getOptionOcr(self, optionName): # 编码转换; if type(optionName) == str: option = unicode(optionName) 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 ''' 函数:获取指定option的value表值,如果参数value未指定具体值则获取全部valueName的值; 示例: { "option":"option名称", "values":[option在value表中的所有value字段], "value_for_ocr":[option在value表中所有value_for_ocr字段], "enter_key":"", "move_key":"", "others":"", } ''' def getOptionValues(self, optionName, value=""): # 编码转换; if type(optionName) == str: option = unicode(optionName) # option/value name是否在value表中的; if optionName not in self.__valueParams.values: return [] optValues = self.valueParams.values[option]["value"] valueList = [] valueOcrList = [] for item in optValues: valueList.append(item["value"]) valueOcrList.append(item["value_for_ocr"]) 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 # 返回结果; return vp def getOptionInfo(self, optionName): # 编码转换; if type(optionName) == str: option = unicode(optionName) 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 ''' 函数:获取指定的option在value表中的所有value字段; ''' def getOptionAllValueName(self, optionName): valueList = [] if self.valueParams.values.has_key(optionName): valueDictList = self.valueParams.values[optionName]["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 if __name__ == "__main__": exData = CExtraData() opxls = COptionExcel(exData) # print json.dumps(opxls.pathParams.paths) # print u"getOptionMap", opxls.getOptionAllChildItemMap('picture') # print u"getOptionAllChildItemOcrList1d", opxls.getOptionAllChildItemOcrList1d('picture', False) # print u"getOptionAllChildItemOcrList2d", opxls.getOptionAllChildItemOcrList2d('picture', False) # print "getOptionAllSiblingItemOcrList", opxls.getOptionAllSiblingItemOcrList('picture') print "getOptionAllChildItem", opxls.getOptionAllChildItem('picture')