ConfigManager.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # -*- coding:utf-8 -*-
  2. import os
  3. from UIT_PathManage import UITPathManage
  4. from ssat_sdk.MenuTree3.BaseLog import CBaseLog
  5. from ssat_sdk.MenuTree3.TConfig import TConfig
  6. # 测试加头文件;
  7. from TData import CTData
  8. g_level = ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth',
  9. 'Seventh', 'Eighth', 'Ninth', 'Tenth', 'Eleventh', 'Twelfth']
  10. # 注意:所有不对外暴露的变量和函数需要私有化,以明确哪些接口和参数是对外的。
  11. # 这样便于后期维护时,根据对外的变量和函数来做处理。
  12. class CConfigManager(TConfig, CBaseLog):
  13. def __init__(self, menuTreeDir, pathManager):
  14. self.__pathManager = pathManager
  15. if self.__pathManager is None:
  16. self.error("路径参数错误:None")
  17. # 状态:False表示路径值失败;
  18. self.status = False
  19. # menuTree目录;
  20. self.__uiTreeDir = menuTreeDir
  21. # 判断是否为目录;
  22. if not os.path.isdir(self.__uiTreeDir):
  23. self.error("%s 不是menuTree目录" % self.__uiTreeDir)
  24. # 判断目录是否存在;
  25. if not os.path.exists(self.__uiTreeDir):
  26. self.error("menuTree(%s)目录不存在" % self.__uiTreeDir)
  27. # 判断配置文件是否存在;
  28. self.__configPath = os.path.join(self.__uiTreeDir, "menutree.ini")
  29. if not os.path.exists(self.__configPath):
  30. self.error("menutree配置文件不存在:%s" % self.__configPath)
  31. self.status = True
  32. # 初始化父类;
  33. TConfig.__init__(self, self.__configPath)
  34. # 获取超级密码;
  35. def getSuperPassword(self):
  36. return self.get_value("Password", "super")
  37. # 获取普通密码;
  38. def getOrdinaryPassword(self):
  39. return self.get_value("Password", "ordinary")
  40. # 获取父节点等待进入子节点时间;
  41. def getParentWaitTime(self, parent):
  42. optionName = parent + "WaitTime"
  43. try:
  44. waitTime = float(self.get_value("waitTime", optionName))
  45. except Exception, e:
  46. waitTime = 1.0
  47. self.error("获取%s界面的等待界面时间失败,使用默认值1.0s" % str(parent))
  48. return waitTime
  49. # 获取阀值字典;
  50. def getThresholdDict(self, option):
  51. section = "Threshold"
  52. thresholdDict = {}
  53. value = self.get_value(section, option)
  54. self.info("value:%s,%s" % (value, type(value)))
  55. if not value:
  56. return thresholdDict
  57. value_str = str(value)
  58. self.info("value_str:%s,%s" % (value_str, type(value_str)))
  59. value_dict = eval(value_str)
  60. self.info("value_dict:%s,%s" % (value_dict, type(value_dict)))
  61. return value_dict
  62. # 获取Option的图片配置;
  63. def __getICONConfig(self, optionName, is_value_sheet=False):
  64. # 默认值;
  65. def_cfg = {"icon_path": "", "dcfg": {}, "dir_path": ""}
  66. paths = self.__pathManager.get_option_paths(optionName)
  67. # 判断路径节点是否空;
  68. if paths.__len__() == 0:
  69. self.error("当前【%s】的路径节点空,使用默认的配置值%s" % (optionName, def_cfg))
  70. return False, def_cfg
  71. # 获取first parent;
  72. first_parent = paths[g_level[0]]['parent']
  73. # 获取当前option的level;
  74. cur_level = g_level[paths.__len__() - 1]
  75. # 当前option的父节点名称;
  76. cur_parent = paths[cur_level]['parent']
  77. if is_value_sheet is True:
  78. cur_level = "value"
  79. # option聚焦的首配图片路径;
  80. icon_path = os.path.join(self.__uiTreeDir, "icon\\", cur_parent + "." + cur_level + "_" + optionName + ".png")
  81. # option聚焦的dir首配图片路径;
  82. icon_dir_path = os.path.join(self.__uiTreeDir,
  83. "icon\\" + cur_parent + "." + cur_level + "_" + optionName + ".dir.png")
  84. # 图片聚焦时的定位参数;
  85. opc_cfg = self.get_value_dict(cur_level, cur_parent + '.' + optionName)
  86. # 首配图片判断是否存在,不存在取用次配图片路径;
  87. if not os.path.exists(icon_path):
  88. # 使用次配图片,父级配图;
  89. self.warn("Option(%s)首配图片不存在:%s" % (optionName, icon_path))
  90. icon_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".png")
  91. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".dir.png")
  92. opc_cfg = self.get_value_dict(cur_level, cur_parent)
  93. self.warn("Option(%s)首配图片不存在,尝试使用次配图(%s):%s,配置%s" % (optionName, cur_parent, icon_path, opc_cfg))
  94. # 如果次配都不存在,使用顶层配图;
  95. if not os.path.exists(icon_path):
  96. self.warn("Option(%s)次配图片不存在:%s" % (optionName, icon_path))
  97. # 使用顶层配图(first parent)
  98. icon_path = os.path.join(self.__uiTreeDir,
  99. "icon\\" + first_parent + "." + cur_level + "_" + optionName +
  100. ".png")
  101. icon_dir_path = os.path.join(self.__uiTreeDir,
  102. "icon\\" + first_parent + "." + cur_level + "_" + optionName +
  103. ".dir.png")
  104. opc_cfg = self.get_value_dict(cur_level, first_parent)
  105. self.warn("Option(%s)次配图片不存在,尝试使用顶层配图(%s):%s,配置%s" % (optionName, first_parent, icon_path, opc_cfg))
  106. # 如果顶层配图不存在,退出;
  107. if not os.path.exists(icon_path):
  108. self.error("%s对应的顶层菜单配图不存在%s,使用默认配置%s" % (optionName, icon_path, def_cfg))
  109. return False, def_cfg
  110. # endif
  111. # endif
  112. # endif
  113. if opc_cfg.__len__() == 0:
  114. opc_cfg = {"offset": 20, "minPeri": 0, "maxPeri": 0, "minArea": 0, "maxArea": 0, "morphology": []}
  115. return True, {"icon_path": icon_path, "dcfg": opc_cfg, "dir_path": icon_dir_path}
  116. # 获取Option的图片配置;
  117. def getOptionICONConfig(self, optionName):
  118. return self.__getICONConfig(optionName, False)
  119. # 获取Value的图片配置;
  120. def getValueICONConfig(self, valueName):
  121. return self.__getICONConfig(valueName, True)
  122. # 获取Option的OCR配置;
  123. def getOptionOCRConfig(self, optionName):
  124. ocr_dict = []
  125. paths = self.__pathManager.get_option_paths(optionName)
  126. # 判断路径节点是否空;
  127. if paths.__len__() == 0:
  128. self.error("当前【%s】的路径节点空" % optionName)
  129. ocr_dict = [{"lan": "ChinesePRC+English", "type": 4}, {"lan": "ChinesePRC+English", "type": 253},
  130. {"lan": "ChinesePRC+English", "type": 10001}]
  131. else:
  132. # 如果状态False,退出;
  133. if self.status is False:
  134. self.warn("配置文件(%s)不存在,%s使用默认的ocr配置" % (self.__configPath, optionName))
  135. ocr_dict = [{"lan": "ChinesePRC+English", "type": 4}, {"lan": "ChinesePRC+English", "type": 253},
  136. {"lan": "ChinesePRC+English", "type": 10001}]
  137. else:
  138. # 读取指定的ocr配置信息;
  139. for i in range(g_level.index(paths.__len__() - 1), -1, -1):
  140. cur_parent = paths['parent']
  141. first_parent = paths['first_parent']
  142. cur_level = g_level[i]
  143. # 是否有当前option的ocr配置;
  144. if self.has_option(cur_level, optionName + '.ocr'):
  145. self.warn("%s使用自身的ocr配置" % optionName)
  146. ocr_dict = self.get_dict(self.get_value(cur_level, optionName + '.ocr'))
  147. break
  148. # 如果option本身没有配置,获取其父节点的配置;
  149. if self.has_option(cur_level, cur_parent + '.ocr'):
  150. self.warn("%s使用父节点%s的ocr配置" % (optionName, cur_parent))
  151. ocr_dict = self.get_dict(self.get_value(cur_level, cur_parent + '.ocr'))
  152. break
  153. # 如果option父级没有配置,则获取顶层配置(first parent);
  154. if self.has_option(cur_level, first_parent + '.ocr'):
  155. self.warn("%s使用顶层节点%s的ocr配置" % (optionName, first_parent))
  156. ocr_dict = self.get_dict(self.get_value(cur_level, first_parent + '.ocr'))
  157. break
  158. # end-for
  159. # end-if
  160. # end-if
  161. self.info("%s使用的ocr配置=%" % (optionName, ocr_dict))
  162. # 获取ICON源屏幕的分辨率配置值;
  163. # 该值用途:由于有些icon是在某些分辨率下截图的,但是同机芯项目可能使用同一套UI,只是分辨率不一样而已。
  164. # 所以,根据当初的icon分辨率,可以计算出在别的分辨率下的icon区域大小;
  165. def getICONResolutionConfig(self):
  166. return self.get_value_dict('Screen', 'shape') if self.has_option('Screen', 'shape') else [1920, 1080]
  167. if __name__ == "__main__":
  168. tData = CTData()
  169. upath = UITPathManage(tData)
  170. opc = CConfigManager(r'D:\SAT\resource\MenuTree\RT2851\2851', upath)
  171. print opc.getOptionICONConfig("picture")