OptionConfig.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. 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. def getThresholdDict(self, optionName):
  53. section = "Threshold"
  54. thresholdDict = {}
  55. value = self.get_value(section, optionName)
  56. self.info("value:%s,%s" % (value, type(value)))
  57. if not value:
  58. return thresholdDict
  59. value_str = str(value)
  60. self.info("value_str:%s,%s" % (value_str, type(value_str)))
  61. value_dict = eval(value_str)
  62. self.info("value_dict:%s,%s" % (value_dict, type(value_dict)))
  63. return value_dict
  64. # 获取Option的图片配置;
  65. def __getICONConfig(self, optionName, is_value_sheet=False):
  66. # 默认值;
  67. def_cfg = {"icon_path": "", "dcfg": {}, "dir_path": ""}
  68. paths = self.__optionExcel.getOptionPaths(optionName)
  69. # 判断路径节点是否空;
  70. if paths.__len__() == 0:
  71. self.error(u"当前【%s】的路径节点空,使用默认的配置值%s" % (optionName, def_cfg))
  72. return False, def_cfg
  73. # 获取first parent;
  74. first_parent = paths[g_level[0]]['parent']
  75. # 获取当前option的level;
  76. cur_level = g_level[paths.__len__() - 1]
  77. # 当前option的父节点名称;
  78. cur_parent = paths[cur_level]['parent']
  79. if is_value_sheet is True:
  80. cur_level = "value"
  81. # option聚焦的首配图片路径;
  82. icon_path = os.path.join(self.__uiTreeDir, "icon\\", cur_parent + "." + cur_level + "_" + optionName + ".png")
  83. # option聚焦的dir首配图片路径;
  84. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + "_" + optionName + ".dir.png")
  85. # 图片聚焦时的定位参数;
  86. opc_cfg = self.get_value_dict(cur_level, cur_parent + '.' + optionName)
  87. # 首配图片判断是否存在,不存在取用次配图片路径;
  88. if not os.path.exists(icon_path):
  89. # 使用次配图片,父级配图;
  90. self.warn(u"Option(%s)首配图片不存在:%s" % (optionName, icon_path))
  91. icon_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".png")
  92. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".dir.png")
  93. opc_cfg = self.get_value_dict(cur_level, cur_parent)
  94. self.warn(u"Option(%s)首配图片不存在,尝试使用次配图(%s):%s,配置%s" % (optionName, cur_parent, icon_path, opc_cfg))
  95. # 如果次配都不存在,使用顶层配图;
  96. if not os.path.exists(icon_path):
  97. self.warn(u"Option(%s)次配图片不存在:%s" % (optionName, icon_path))
  98. # 使用顶层配图(first parent)
  99. icon_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + "_" + optionName + ".png")
  100. icon_dir_path = os.path.join(self.__uiTreeDir, "icon\\" + first_parent + "." + cur_level + "_" + optionName + ".dir.png")
  101. opc_cfg = self.get_value_dict(cur_level, first_parent)
  102. self.warn(u"Option(%s)次配图片不存在,尝试使用顶层配图(%s):%s,配置%s" % (optionName, first_parent, icon_path, opc_cfg))
  103. # 如果顶层配图不存在,退出;
  104. if not os.path.exists(icon_path):
  105. self.error(u"%s对应的顶层菜单配图不存在%s,使用默认配置%s" % (optionName, icon_path, def_cfg))
  106. return False, def_cfg
  107. # endif
  108. # endif
  109. # endif
  110. if opc_cfg.__len__() == 0:
  111. opc_cfg = {"offset": 20, "minPeri": 0, "maxPeri": 0, "minArea": 0, "maxArea": 0, "morphology": []}
  112. return True, {"icon_path": icon_path, "dcfg": opc_cfg, "dir_path": icon_dir_path}
  113. # 获取Option的图片配置;
  114. def getOptionICONConfig(self, optionName):
  115. return self.__getICONConfig(optionName, False)
  116. # 获取Value的图片配置;
  117. def getValueICONConfig(self, valueName):
  118. return self.__getICONConfig(valueName, True)
  119. # 获取Option的OCR配置;
  120. def getOptionOCRConfig(self, optionName):
  121. ocr_dict = []
  122. # 默认的ocr配置;
  123. def_orc = [{"lan": "ChinesePRC+English", "type": 4}, {"lan": "ChinesePRC+English", "type": 253}, {"lan": "ChinesePRC+English", "type": 10001}]
  124. paths = self.__optionExcel.getOptionPaths(optionName)
  125. # 判断路径节点是否空;
  126. if paths.__len__() == 0:
  127. self.error(u"当前【%s】的路径节点空,将使用默认的ocr配置%s" % (optionName, def_orc))
  128. else:
  129. # 如果状态False,退出;
  130. if self.status is False:
  131. self.warn(u"配置文件(%s)不存在,%s使用默认的ocr配置%s" % (self.__configPath, optionName, def_orc))
  132. else:
  133. # 获取当前option的level;
  134. cur_level = g_level[paths.__len__() - 1]
  135. # 获取first parent;
  136. first_parent = paths[g_level[0]]['parent']
  137. # 当前option的父节点名称;
  138. cur_parent = paths[cur_level]['parent']
  139. # 是否有当前option的ocr配置;
  140. if self.has_option(cur_level, optionName + '.ocr'):
  141. ocr_dict = self.get_dict(self.get_value(cur_level, optionName + '.ocr'))
  142. self.warn(u"%s使用自身的ocr配置%s" % (optionName, ocr_dict))
  143. else:
  144. # 如果option本身没有配置,获取其父节点的配置;
  145. if self.has_option(cur_level, cur_parent + '.ocr'):
  146. ocr_dict = self.get_dict(self.get_value(cur_level, cur_parent + '.ocr'))
  147. self.warn(u"%s使用父节点%s的ocr配置%s" % (optionName, cur_parent, ocr_dict))
  148. else:
  149. # 如果option父级没有配置,则获取顶层配置(first parent);
  150. if self.has_option(cur_level, first_parent + '.ocr'):
  151. ocr_dict = self.get_dict(self.get_value(cur_level, first_parent + '.ocr'))
  152. self.warn(u"%s使用顶层节点%s的ocr配置%s" % (optionName, first_parent, ocr_dict))
  153. #endif
  154. #endif
  155. # end-if
  156. # end-if
  157. if ocr_dict.__len__() == 0:
  158. ocr_dict = def_orc
  159. self.warn(u"无有效的ocr配置,将使用默认的ocr配置%s" % def_orc)
  160. self.info(u"%s使用的ocr配置=%s" % (optionName, ocr_dict))
  161. return 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. exData = CExtraData()
  169. optionExcel = COptionExcel(exData)
  170. optionConfig = COptionConfig(exData, optionExcel)
  171. # print "getSuperPassword", optionConfig.getSuperPassword()
  172. # print "getOrdinaryPassword", optionConfig.getOrdinaryPassword()
  173. # print "getParentWaitTime", optionConfig.getParentWaitTime('setting')
  174. # print "getThresholdDict", optionConfig.getThresholdDict('setting')
  175. # print "getOptionICONConfig", optionConfig.getOptionICONConfig('picture')
  176. # print "getValueICONConfig", optionConfig.getValueICONConfig('picture')
  177. # print "getOptionOCRConfig", optionConfig.getOptionOCRConfig('picture')
  178. print "getICONResolutionConfig", optionConfig.getICONResolutionConfig()