OptionConfig.py 15 KB

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