OptionConfig.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. def getThresholdDict(self, optionName):
  54. section = "Threshold"
  55. thresholdDict = {}
  56. value = self.get_value(section, optionName)
  57. self.info("value:%s,%s" % (value, type(value)))
  58. if not value:
  59. return thresholdDict
  60. value_str = str(value)
  61. self.info("value_str:%s,%s" % (value_str, type(value_str)))
  62. value_dict = eval(value_str)
  63. self.info("value_dict:%s,%s" % (value_dict, type(value_dict)))
  64. return value_dict
  65. '''
  66. 函数:获取Option的图片配置,方式1
  67. 参数:
  68. 返回:Boolean, 字典
  69. 示例:True, {"icon_path": "", "dcfg": {}, "dir_path": ""}
  70. 说明:
  71. 查找顺序:
  72. [当前父节点][当前层级][当前节点] ->[当前父节点][当前层级] ->[根节点][当前层级][当前节点]
  73. [curParent][curLevel][curOption] ->[curParent][curLevel] ->[firstParent][curLevel][curOption]
  74. '''
  75. def __getICONConfig1(self, optionName, is_value_sheet=False):
  76. # 默认值;
  77. def_cfg = {"icon_path": "", "dcfg": {}, "dir_path": ""}
  78. paths = self.__optionExcel.getOptionPaths(optionName)
  79. # 判断路径节点是否空;
  80. if paths.__len__() == 0:
  81. self.error(u"当前【%s】的路径节点空,使用默认的配置值%s" % (optionName, def_cfg))
  82. return False, def_cfg
  83. # 获取first parent;
  84. first_parent = paths[g_level[0]]['parent']
  85. # 获取当前option的level;
  86. cur_level = g_level[paths.__len__() - 1]
  87. # 当前option的父节点名称;
  88. cur_parent = paths[cur_level]['parent']
  89. if is_value_sheet is True:
  90. cur_level = "value"
  91. # option聚焦的首配图片路径;
  92. icon_path = os.path.join(self.__uiTreeDir, "icon\\", cur_parent + "." + cur_level + "_" + optionName + ".png")
  93. # option聚焦的dir首配图片路径;
  94. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + "_" + optionName + ".dir.png")
  95. # 图片聚焦时的定位参数;
  96. opc_cfg = self.get_value_dict(cur_level, cur_parent + '.' + optionName)
  97. # 首配图片判断是否存在,不存在取用次配图片路径;
  98. if not os.path.exists(icon_path):
  99. # 使用次配图片,父级配图;
  100. self.warn(u"Option(%s)首配图片不存在:%s" % (optionName, icon_path))
  101. icon_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".png")
  102. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".dir.png")
  103. opc_cfg = self.get_value_dict(cur_level, cur_parent)
  104. self.warn(u"Option(%s)首配图片不存在,尝试使用次配图(%s):%s,配置%s" % (optionName, cur_parent, icon_path, opc_cfg))
  105. # 如果次配都不存在,使用顶层配图;
  106. if not os.path.exists(icon_path):
  107. self.warn(u"Option(%s)次配图片不存在:%s" % (optionName, icon_path))
  108. # 使用顶层配图(first parent)
  109. icon_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + "_" + optionName + ".png")
  110. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + "_" + optionName + ".dir.png")
  111. opc_cfg = self.get_value_dict(cur_level, first_parent)
  112. self.warn(u"Option(%s)次配图片不存在,尝试使用顶层配图(%s):%s,配置%s" % (optionName, first_parent, icon_path, opc_cfg))
  113. # 如果顶层配图不存在,退出;
  114. if not os.path.exists(icon_path):
  115. self.error(u"%s对应的顶层菜单配图不存在%s,使用默认配置%s" % (optionName, icon_path, def_cfg))
  116. return False, def_cfg
  117. # endif
  118. # endif
  119. # endif
  120. if opc_cfg.__len__() == 0:
  121. opc_cfg = {"offset": 20, "minPeri": 0, "maxPeri": 0, "minArea": 0, "maxArea": 0, "morphology": []}
  122. return True, {"icon_path": icon_path, "dcfg": opc_cfg, "dir_path": icon_dir_path}
  123. '''
  124. 函数:获取Option的图片配置,方式2
  125. 参数:
  126. 返回:Boolean, 字典
  127. 示例:True, {"icon_path": "", "dcfg": {}, "dir_path": ""}
  128. 说明:
  129. 查找顺序:
  130. [根节点][当前层级][当前节点] ->[根节点][当前层级] ->[根节点][根层级]
  131. [firstParent][curLevel][curOption] ->[firstParent][curLevel] ->[firstParent][firstLevel]
  132. '''
  133. def __getICONConfig2(self, optionName, is_value_sheet=False):
  134. # 默认值;
  135. def_cfg = {"icon_path": "", "dcfg": {}, "dir_path": ""}
  136. paths = self.__optionExcel.getOptionPaths(optionName)
  137. # 判断路径节点是否空;
  138. if paths.__len__() == 0:
  139. self.error(u"当前【%s】的路径节点空,使用默认的配置值%s" % (optionName, def_cfg))
  140. return False, def_cfg
  141. # 获取first parent;
  142. first_parent = paths[g_level[0]]['parent']
  143. # 获取当前option的level;
  144. cur_level = g_level[paths.__len__() - 1]
  145. # 当前option的父节点名称;
  146. cur_parent = paths[cur_level]['parent']
  147. if is_value_sheet is True:
  148. cur_level = "value"
  149. # option聚焦的首配图片路径;
  150. icon_path = os.path.join(self.__uiTreeDir, "icon\\",
  151. first_parent + "." + cur_level + "_" + optionName + ".png")
  152. # option聚焦的dir首配图片路径;
  153. icon_dir_path = os.path.join(self.__uiTreeDir,
  154. "icon\\" + first_parent + "." + cur_level + "_" + optionName + ".dir.png")
  155. # 图片聚焦时的定位参数;
  156. opc_cfg = self.get_value_dict(cur_level, first_parent)
  157. # 首配图片判断是否存在,不存在取用次配图片路径;
  158. if not os.path.exists(icon_path):
  159. # 使用次配图片,父级配图;
  160. self.warn(u"Option(%s)首配图片不存在:%s" % (optionName, icon_path))
  161. icon_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + ".png")
  162. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + ".dir.png")
  163. self.warn(u"Option(%s)首配图片不存在,尝试使用次配图(%s):%s,配置%s" % (optionName, cur_parent, icon_path, opc_cfg))
  164. # 如果次配都不存在,使用顶层配图;
  165. if not os.path.exists(icon_path):
  166. self.warn(u"Option(%s)次配图片不存在:%s" % (optionName, icon_path))
  167. # 使用顶层配图(first parent)
  168. icon_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + "First.png")
  169. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + "First.dir.png")
  170. self.warn(
  171. u"Option(%s)次配图片不存在,尝试使用顶层配图(%s):%s,配置%s" % (optionName, first_parent, icon_path, opc_cfg))
  172. # 如果顶层配图不存在,退出;
  173. if not os.path.exists(icon_path):
  174. self.error(u"%s对应的顶层菜单配图不存在%s,使用默认配置%s" % (optionName, icon_path, def_cfg))
  175. return False, def_cfg
  176. # endif
  177. # endif
  178. # endif
  179. if opc_cfg.__len__() == 0:
  180. opc_cfg = {"offset": 20, "minPeri": 0, "maxPeri": 0, "minArea": 0, "maxArea": 0, "morphology": []}
  181. return True, {"icon_path": icon_path, "dcfg": opc_cfg, "dir_path": icon_dir_path}
  182. # 获取Option的图片配置;
  183. def getOptionICONConfig(self, optionName):
  184. outResutl, outData = self.__getICONConfig1(optionName, False)
  185. if outResutl is False:
  186. return self.__getICONConfig2(optionName, False)
  187. return outResutl, outData
  188. # 获取Value的图片配置;
  189. def getValueICONConfig(self, valueName):
  190. outResutl, outData = self.__getICONConfig1(valueName, True)
  191. if outResutl is False:
  192. return self.__getICONConfig2(valueName, True)
  193. return outResutl, outData
  194. # 获取Option的OCR配置;
  195. def getOptionOCRConfig(self, optionName):
  196. ocr_dict = []
  197. # 默认的ocr配置;
  198. def_orc = [{"lan": "ChinesePRC+English", "type": 4}, {"lan": "ChinesePRC+English", "type": 253}, {"lan": "ChinesePRC+English", "type": 10001}]
  199. paths = self.__optionExcel.getOptionPaths(optionName)
  200. # 判断路径节点是否空;
  201. if paths.__len__() == 0:
  202. self.error(u"当前【%s】的路径节点空,将使用默认的ocr配置%s" % (optionName, def_orc))
  203. else:
  204. # 如果状态False,退出;
  205. if self.status is False:
  206. self.warn(u"配置文件(%s)不存在,%s使用默认的ocr配置%s" % (self.__configPath, optionName, def_orc))
  207. else:
  208. # 获取当前option的level;
  209. cur_level = g_level[paths.__len__() - 1]
  210. # 获取first parent;
  211. first_parent = paths[g_level[0]]['parent']
  212. # 当前option的父节点名称;
  213. cur_parent = paths[cur_level]['parent']
  214. # 是否有当前option的ocr配置;
  215. if self.has_option(cur_level, optionName + '.ocr'):
  216. ocr_dict = self.get_dict(self.get_value(cur_level, optionName + '.ocr'))
  217. self.warn(u"%s使用自身的ocr配置%s" % (optionName, ocr_dict))
  218. else:
  219. # 如果option本身没有配置,获取其父节点的配置;
  220. if self.has_option(cur_level, cur_parent + '.ocr'):
  221. ocr_dict = self.get_dict(self.get_value(cur_level, cur_parent + '.ocr'))
  222. self.warn(u"%s使用父节点%s的ocr配置%s" % (optionName, cur_parent, ocr_dict))
  223. else:
  224. # 如果option父级没有配置,则获取顶层配置(first parent);
  225. if self.has_option(cur_level, first_parent + '.ocr'):
  226. ocr_dict = self.get_dict(self.get_value(cur_level, first_parent + '.ocr'))
  227. self.warn(u"%s使用顶层节点%s的ocr配置%s" % (optionName, first_parent, ocr_dict))
  228. #endif
  229. #endif
  230. # end-if
  231. # end-if
  232. if ocr_dict.__len__() == 0:
  233. ocr_dict = def_orc
  234. self.warn(u"无有效的ocr配置,将使用默认的ocr配置%s" % def_orc)
  235. self.info(u"%s使用的ocr配置=%s" % (optionName, ocr_dict))
  236. return ocr_dict
  237. # 获取ICON源屏幕的分辨率配置值;
  238. # 该值用途:由于有些icon是在某些分辨率下截图的,但是同机芯项目可能使用同一套UI,只是分辨率不一样而已。
  239. # 所以,根据当初的icon分辨率,可以计算出在别的分辨率下的icon区域大小;
  240. def getICONResolutionConfig(self):
  241. return self.get_value_dict('Screen', 'shape') if self.has_option('Screen', 'shape') else [1920, 1080]
  242. if __name__ == "__main__":
  243. exData = CExtraData()
  244. optionExcel = COptionExcel(exData)
  245. optionConfig = COptionConfig(exData, optionExcel)
  246. # print "getSuperPassword", optionConfig.getSuperPassword()
  247. # print "getOrdinaryPassword", optionConfig.getOrdinaryPassword()
  248. # print "getParentWaitTime", optionConfig.getParentWaitTime('setting')
  249. # print "getThresholdDict", optionConfig.getThresholdDict('setting')
  250. # print "getOptionICONConfig", optionConfig.getOptionICONConfig('picture')
  251. print "getOptionICONConfig", optionConfig.getOptionICONConfig('picture_preset')
  252. # print "getValueICONConfig", optionConfig.getValueICONConfig('picture')
  253. # print "getOptionOCRConfig", optionConfig.getOptionOCRConfig('picture')
  254. # print "getICONResolutionConfig", optionConfig.getICONResolutionConfig()