OptionConfig.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # -*- coding:utf-8 -*-
  2. import os
  3. from UIT_PathManage import UITPathManage
  4. from BaseLog import CBaseLog
  5. from TConfig import TConfig
  6. from ExtraData import CExtraData
  7. from OptionExcel import COptionExcel
  8. g_level = ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth',
  9. 'Seventh', 'Eighth', 'Ninth', 'Tenth', 'Eleventh', 'Twelfth']
  10. # 注意:所有不对外暴露的变量和函数需要私有化,以明确哪些接口和参数是对外的。
  11. # 这样便于后期维护时,根据对外的变量和函数来做处理。
  12. class COptionConfig(TConfig, CBaseLog):
  13. def __init__(self, exData, optionExcel):
  14. CBaseLog.__init__(self)
  15. self.__exData = exData
  16. self.__optionExcel = optionExcel
  17. if self.__optionExcel is None:
  18. self.error(u"路径参数错误:None")
  19. # 状态:False表示路径值失败;
  20. self.status = False
  21. # menuTree目录;
  22. self.__uiTreeDir = self.__exData.menuTreeDir
  23. # 判断是否为目录;
  24. if not os.path.isdir(self.__uiTreeDir):
  25. self.error(u"%s 不是menuTree目录" % self.__uiTreeDir)
  26. # 判断目录是否存在;
  27. if not os.path.exists(self.__uiTreeDir):
  28. self.error(u"menuTree(%s)目录不存在" % self.__uiTreeDir)
  29. # 判断配置文件是否存在;
  30. self.__configPath = os.path.join(self.__uiTreeDir, "menutree.ini")
  31. if os.path.exists(self.__configPath):
  32. self.status = True
  33. else:
  34. self.error(u"menutree配置文件不存在:%s" % self.__configPath)
  35. # 初始化父类;
  36. TConfig.__init__(self, self.__configPath)
  37. # 获取超级密码;
  38. def getSuperPassword(self):
  39. return self.get_value("Password", "super")
  40. # 获取普通密码;
  41. def getOrdinaryPassword(self):
  42. return self.get_value("Password", "ordinary")
  43. # 获取父节点等待进入子节点时间;
  44. def getParentWaitTime(self, parent):
  45. optionName = parent + "WaitTime"
  46. try:
  47. waitTime = float(self.get_value("waitTime", optionName))
  48. except Exception:
  49. waitTime = 1.0
  50. self.error(u"获取%s界面的等待界面时间失败,使用默认值1.0s" % str(parent))
  51. return waitTime
  52. # 获取阀值字典;
  53. '''
  54. 函数:获取指定option的阀值字典
  55. 参数:
  56. optionName 节点名称;
  57. 返回:字典。
  58. '''
  59. def getThresholdDict(self, optionName):
  60. section = "Threshold"
  61. thresholdDict = {}
  62. value = self.get_value(section, optionName)
  63. self.info("value:%s,%s" % (value, type(value)))
  64. if not value:
  65. return thresholdDict
  66. value_str = str(value)
  67. self.info("value_str:%s,%s" % (value_str, type(value_str)))
  68. value_dict = eval(value_str)
  69. self.info("value_dict:%s,%s" % (value_dict, type(value_dict)))
  70. return value_dict
  71. '''
  72. 函数:获取Option的图片配置,方式1
  73. 参数:
  74. 返回:Boolean, 字典
  75. 示例:True, {"icon_path": "", "dcfg": {}, "dir_path": ""}
  76. 说明:
  77. 查找顺序:
  78. [当前父节点][当前层级][当前节点] ->[当前父节点][当前层级] ->[根节点][当前层级][当前节点]
  79. [curParent][curLevel][curOption] ->[curParent][curLevel] ->[firstParent][curLevel][curOption]
  80. '''
  81. def __getICONConfig1(self, optionName, is_value_sheet=False):
  82. # 默认值;
  83. def_cfg = {"icon_path": "", "dcfg": {}, "dir_path": ""}
  84. paths = self.__optionExcel.getOptionPaths(optionName)
  85. # 判断路径节点是否空;
  86. if paths.__len__() == 0:
  87. self.error(u"当前【%s】的路径节点空,使用默认的配置值%s" % (optionName, def_cfg))
  88. return False, def_cfg
  89. # 获取first parent;
  90. first_parent = paths[g_level[0]]['parent']
  91. # 获取当前option的level;
  92. cur_level = g_level[paths.__len__() - 1]
  93. # 当前option的父节点名称;
  94. cur_parent = paths[cur_level]['parent']
  95. if is_value_sheet is True:
  96. cur_level = "value"
  97. # option聚焦的首配图片路径;
  98. icon_path = os.path.join(self.__uiTreeDir, "icon\\", cur_parent + "." + cur_level + "_" + optionName + ".png")
  99. # option聚焦的dir首配图片路径;
  100. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + "_" + optionName + ".dir.png")
  101. # 图片聚焦时的定位参数;
  102. opc_cfg = self.get_value_dict(cur_level, cur_parent + '.' + optionName)
  103. # 首配图片判断是否存在,不存在取用次配图片路径;
  104. if not os.path.exists(icon_path):
  105. # 使用次配图片,父级配图;
  106. self.warn(u"Option(%s)首配图片不存在:%s" % (optionName, icon_path))
  107. icon_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".png")
  108. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".dir.png")
  109. if opc_cfg.__len__() == 0:
  110. opc_cfg = self.get_value_dict(cur_level, cur_parent)
  111. self.warn(u"Option(%s)首配图片不存在,尝试使用次配图(%s):%s,配置%s" % (optionName, cur_parent, icon_path, opc_cfg))
  112. # 如果次配都不存在,使用顶层配图;
  113. if not os.path.exists(icon_path):
  114. self.warn(u"Option(%s)次配图片不存在:%s" % (optionName, icon_path))
  115. # 使用顶层配图(first parent)
  116. icon_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + "_" + optionName + ".png")
  117. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + "_" + optionName + ".dir.png")
  118. if opc_cfg.__len__() == 0:
  119. opc_cfg = self.get_value_dict(cur_level, first_parent)
  120. self.warn(u"Option(%s)次配图片不存在,尝试使用顶层配图(%s):%s,配置%s" % (optionName, first_parent, icon_path, opc_cfg))
  121. # 如果顶层配图不存在,退出;
  122. if not os.path.exists(icon_path):
  123. self.error(u"%s对应的顶层菜单配图不存在%s,使用默认配置%s" % (optionName, icon_path, def_cfg))
  124. return False, def_cfg
  125. # endif
  126. # endif
  127. # endif
  128. if opc_cfg.__len__() == 0:
  129. opc_cfg = {"offset": 20, "minPeri": 0, "maxPeri": 0, "minArea": 0, "maxArea": 0, "morphology": []}
  130. return True, {"icon_path": icon_path, "dcfg": opc_cfg, "dir_path": icon_dir_path}
  131. '''
  132. 函数:获取Option的图片配置,方式2
  133. 参数:
  134. 返回:Boolean, 字典
  135. 示例:True, {"icon_path": "", "dcfg": {}, "dir_path": ""}
  136. 说明:
  137. 查找顺序:
  138. [根节点][当前层级][当前节点] ->[根节点][当前层级] ->[根节点][根层级]
  139. [firstParent][curLevel][curOption] ->[firstParent][curLevel] ->[firstParent][firstLevel]
  140. '''
  141. def __getICONConfig2(self, optionName, is_value_sheet=False):
  142. # 默认值;
  143. def_cfg = {"icon_path": "", "dcfg": {}, "dir_path": ""}
  144. paths = self.__optionExcel.getOptionPaths(optionName)
  145. # 判断路径节点是否空;
  146. if paths.__len__() == 0:
  147. self.error(u"当前【%s】的路径节点空,使用默认的配置值%s" % (optionName, def_cfg))
  148. return False, def_cfg
  149. # 获取first parent;
  150. first_parent = paths[g_level[0]]['parent']
  151. # 获取当前option的level;
  152. cur_level = g_level[paths.__len__() - 1]
  153. # 当前option的父节点名称;
  154. cur_parent = paths[cur_level]['parent']
  155. if is_value_sheet is True:
  156. cur_level = "value"
  157. # option聚焦的首配图片路径;
  158. icon_path = os.path.join(self.__uiTreeDir, "icon\\",
  159. first_parent + "." + cur_level + "_" + optionName + ".png")
  160. # option聚焦的dir首配图片路径;
  161. icon_dir_path = os.path.join(self.__uiTreeDir,
  162. "icon\\" + first_parent + "." + cur_level + "_" + optionName + ".dir.png")
  163. # 图片聚焦时的定位参数;
  164. opc_cfg = self.get_value_dict(cur_level, first_parent)
  165. # 首配图片判断是否存在,不存在取用次配图片路径;
  166. if not os.path.exists(icon_path):
  167. # 使用次配图片,父级配图;
  168. self.warn(u"Option(%s)首配图片不存在:%s" % (optionName, icon_path))
  169. icon_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + ".png")
  170. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + ".dir.png")
  171. self.warn(u"Option(%s)首配图片不存在,尝试使用次配图(%s):%s,配置%s" % (optionName, cur_parent, icon_path, opc_cfg))
  172. # 如果次配都不存在,使用顶层配图;
  173. if not os.path.exists(icon_path):
  174. self.warn(u"Option(%s)次配图片不存在:%s" % (optionName, icon_path))
  175. # 使用顶层配图(first parent)
  176. icon_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + "First.png")
  177. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + "First.dir.png")
  178. self.warn(
  179. u"Option(%s)次配图片不存在,尝试使用顶层配图(%s):%s,配置%s" % (optionName, first_parent, icon_path, opc_cfg))
  180. # 如果顶层配图不存在,退出;
  181. if not os.path.exists(icon_path):
  182. self.error(u"%s对应的顶层菜单配图不存在%s,使用默认配置%s" % (optionName, icon_path, def_cfg))
  183. return False, def_cfg
  184. # endif
  185. # endif
  186. # endif
  187. if opc_cfg.__len__() == 0:
  188. opc_cfg = {"offset": 20, "minPeri": 0, "maxPeri": 0, "minArea": 0, "maxArea": 0, "morphology": []}
  189. return True, {"icon_path": icon_path, "dcfg": opc_cfg, "dir_path": icon_dir_path}
  190. '''
  191. 函数:获取Option的图片配置
  192. 参数:
  193. optionName 要获取的option。
  194. 返回:Boolean, {}
  195. '''
  196. def getOptionICONConfig(self, optionName):
  197. outResutl, outData = self.__getICONConfig1(optionName, False)
  198. if outResutl is False:
  199. return self.__getICONConfig2(optionName, False)
  200. return outResutl, outData
  201. '''
  202. 函数:获取Value的图片配置
  203. 参数:
  204. valueName 要获取的value名称。
  205. 返回:Boolean, {}
  206. '''
  207. def getValueICONConfig(self, valueName):
  208. outResutl, outData = self.__getICONConfig1(valueName, True)
  209. if outResutl is False:
  210. return self.__getICONConfig2(valueName, True)
  211. return outResutl, outData
  212. '''
  213. 函数:获取Option的OCR配置
  214. 参数:
  215. optionName 要获取的option名称
  216. 返回:字典
  217. '''
  218. def getOptionOCRConfig(self, optionName):
  219. ocr_dict = []
  220. # 默认的ocr配置;
  221. def_orc = [{"lan": "ChinesePRC+English", "type": 4}, {"lan": "ChinesePRC+English", "type": 253}, {"lan": "ChinesePRC+English", "type": 10001}]
  222. paths = self.__optionExcel.getOptionPaths(optionName)
  223. # 判断路径节点是否空;
  224. if paths.__len__() == 0:
  225. self.error(u"当前【%s】的路径节点空,将使用默认的ocr配置%s" % (optionName, def_orc))
  226. else:
  227. # 如果状态False,退出;
  228. if self.status is False:
  229. self.warn(u"配置文件(%s)不存在,%s使用默认的ocr配置%s" % (self.__configPath, optionName, def_orc))
  230. else:
  231. # 获取当前option的level;
  232. cur_level = g_level[paths.__len__() - 1]
  233. # 获取first parent;
  234. first_parent = paths[g_level[0]]['parent']
  235. # 当前option的父节点名称;
  236. cur_parent = paths[cur_level]['parent']
  237. # 是否有当前option的ocr配置;
  238. if self.has_option(cur_level, optionName + '.ocr'):
  239. ocr_dict = self.get_dict(self.get_value(cur_level, optionName + '.ocr'))
  240. self.warn(u"%s使用自身的ocr配置%s" % (optionName, ocr_dict))
  241. else:
  242. # 如果option本身没有配置,获取其父节点的配置;
  243. if self.has_option(cur_level, cur_parent + '.ocr'):
  244. ocr_dict = self.get_dict(self.get_value(cur_level, cur_parent + '.ocr'))
  245. self.warn(u"%s使用父节点%s的ocr配置%s" % (optionName, cur_parent, ocr_dict))
  246. else:
  247. # 如果option父级没有配置,则获取顶层配置(first parent);
  248. if self.has_option(cur_level, first_parent + '.ocr'):
  249. ocr_dict = self.get_dict(self.get_value(cur_level, first_parent + '.ocr'))
  250. self.warn(u"%s使用顶层节点%s的ocr配置%s" % (optionName, first_parent, ocr_dict))
  251. #endif
  252. #endif
  253. # end-if
  254. # end-if
  255. if ocr_dict.__len__() == 0:
  256. ocr_dict = def_orc
  257. self.warn(u"无有效的ocr配置,将使用默认的ocr配置%s" % def_orc)
  258. self.info(u"%s使用的ocr配置=%s" % (optionName, ocr_dict))
  259. return ocr_dict
  260. '''
  261. 函数:获取ICON源屏幕的分辨率配置值
  262. 参数:
  263. 返回:
  264. 说明:
  265. 该值用途:由于有些icon是在某些分辨率下截图的,但是同机芯项目可能使用同一套UI,只是分辨率不一样而已。
  266. 所以,根据当初的icon分辨率,可以计算出在别的分辨率下的icon区域大小;
  267. '''
  268. def getICONResolutionConfig(self):
  269. return self.get_value_dict('Screen', 'shape') if self.has_option('Screen', 'shape') else [1920, 1080]
  270. if __name__ == "__main__":
  271. exData = CExtraData()
  272. optionExcel = COptionExcel(exData)
  273. optionConfig = COptionConfig(exData, optionExcel)
  274. # print "getSuperPassword", optionConfig.getSuperPassword()
  275. # print "getOrdinaryPassword", optionConfig.getOrdinaryPassword()
  276. # print "getParentWaitTime", optionConfig.getParentWaitTime('setting')
  277. # print "getThresholdDict", optionConfig.getThresholdDict('setting')
  278. # print "getOptionICONConfig", optionConfig.getOptionICONConfig('picture')
  279. print "getOptionICONConfig", optionConfig.getOptionICONConfig('picture_preset')
  280. # print "getValueICONConfig", optionConfig.getValueICONConfig('picture')
  281. # print "getOptionOCRConfig", optionConfig.getOptionOCRConfig('picture')
  282. # print "getICONResolutionConfig", optionConfig.getICONResolutionConfig()