123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- # -*- 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')
|