ConfigManager.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. class CConfigManager(TConfig, CBaseLog):
  11. def __init__(self, menuTreeDir, pathManager):
  12. self.pathManager = pathManager
  13. if self.pathManager is None:
  14. self.error("路径参数错误:None")
  15. # 状态:False表示路径值失败;
  16. self.status = False
  17. # menuTree目录;
  18. self.uiTreeDir = menuTreeDir
  19. # 判断是否为目录;
  20. if not os.path.isdir(self.uiTreeDir):
  21. self.error("%s 不是menuTree目录" % self.uiTreeDir)
  22. # 判断目录是否存在;
  23. if not os.path.exists(self.uiTreeDir):
  24. self.error("menuTree(%s)目录不存在" % self.uiTreeDir)
  25. # 判断配置文件是否存在;
  26. self.configPath = os.path.join(self.uiTreeDir, "menutree.ini")
  27. if not os.path.exists(self.configPath):
  28. self.error("menutree配置文件不存在:%s" % self.configPath)
  29. self.status = True
  30. # 初始化父类;
  31. TConfig.__init__(self, self.configPath)
  32. # 获取超级密码;
  33. def getSuperPassword(self):
  34. return self.get_value("Password", "super")
  35. # 获取普通密码;
  36. def getOrdinaryPassword(self):
  37. return self.get_value("Password", "ordinary")
  38. # 获取父节点等待进入子节点时间;
  39. def getParentWaitTime(self, parent):
  40. optionName = parent + "WaitTime"
  41. try:
  42. waitTime = float(self.get_value("waitTime", optionName))
  43. except Exception, e:
  44. waitTime = 1.0
  45. self.error("获取%s界面的等待界面时间失败,使用默认值1.0s" % str(parent))
  46. return waitTime
  47. # 获取阀值字典;
  48. def getThresholdDict(self, option):
  49. section = "Threshold"
  50. thresholdDict = {}
  51. value = self.get_value(section, option)
  52. self.info("value:%s,%s" % (value, type(value)))
  53. if not value:
  54. return thresholdDict
  55. value_str = str(value)
  56. self.info("value_str:%s,%s" % (value_str, type(value_str)))
  57. value_dict = eval(value_str)
  58. self.info("value_dict:%s,%s" % (value_dict, type(value_dict)))
  59. return value_dict
  60. # 获取Option的图片配置;
  61. def __getICONConfig(self, optionName, is_value_sheet=False):
  62. paths = self.pathManager.get_option_paths(optionName)
  63. # 判断路径节点是否空;
  64. if paths.__len__() == 0:
  65. self.error("当前【%s】的路径节点空" % optionName)
  66. return False, {"icon_path": "", "dcfg": {}, "dir_path": ""}
  67. # 获取first parent;
  68. first_parent = paths[g_level[0]]['parent']
  69. # 获取当前option的level;
  70. cur_level = g_level[paths.__len__() - 1]
  71. # 当前option的父节点名称;
  72. cur_parent = paths[cur_level]['parent']
  73. if is_value_sheet is True:
  74. cur_level = "value"
  75. # option聚焦的首配图片路径;
  76. icon_path = os.path.join(self.uiTreeDir, "icon\\", cur_parent + "." + cur_level + "_" + optionName + ".png")
  77. # option聚焦的dir首配图片路径;
  78. icon_dir_path = os.path.join(self.uiTreeDir, "icon\\" + cur_parent + "." + cur_level + "_" + optionName + ".dir.png")
  79. # 图片聚焦时的定位参数;
  80. opc_cfg = self.get_value_dict(cur_level, cur_parent + '.' + optionName)
  81. last_path = icon_path
  82. # 首配图片判断是否存在,不存在取用次配图片路径;
  83. if not os.path.exists(icon_path):
  84. # 使用次配图片,父级配图;
  85. icon_path = os.path.join(self.uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".png")
  86. icon_dir_path = os.path.join(self.uiTreeDir, "icon\\" + cur_parent + "." + cur_level + ".dir.png")
  87. opc_cfg = self.get_value_dict(cur_level, cur_parent)
  88. self.warn("Option(%s)首配图片不存在:%s,使用次配图:%s" % (optionName, icon_path))
  89. # 如果次配都不存在,使用顶层配图;
  90. if not os.path.exists(icon_path):
  91. self.warn("Option(%s)次配图片不存在:%s" % (optionName, icon_path))
  92. # 使用顶层配图(first parent)
  93. icon_path = os.path.join(self.uiTreeDir, "icon\\" + first_parent + "." + cur_level + "_" + optionName +
  94. ".png")
  95. icon_dir_path = os.path.join(self.uiTreeDir, "icon\\" + first_parent + "." + cur_level + "_" + optionName +
  96. ".dir.png")
  97. opc_cfg = self.get_value_dict(cur_level, first_parent)
  98. # 如果顶层配图不存在,退出;
  99. if not os.path.exists(icon_path):
  100. self.error("%s对应的顶层菜单配图不存在%s" % (optionName, icon_path))
  101. return False, {"icon_path": "", "dcfg": {}, "dir_path": ""}
  102. # endif
  103. # endif
  104. # endif
  105. if opc_cfg.__len__() == 0:
  106. opc_cfg = {"offset": 20, "minPeri": 0, "maxPeri": 0, "minArea": 0, "maxArea": 0, "morphology": []}
  107. return True, {"icon_path": icon_path, "dcfg": opc_cfg, "dir_path": icon_dir_path}
  108. # 获取Option的图片配置;
  109. def getOptionICONConfig(self, optionName):
  110. return self.__getICONConfig(optionName, False)
  111. # 获取Value的图片配置;
  112. def getValueICONConfig(self, valueName):
  113. return self.__getICONConfig(valueName, True)
  114. # 获取Option的OCR配置;
  115. def getOptionOCRConfig(self, optionName):
  116. ocr_dict = []
  117. paths = self.pathManager.get_option_paths(optionName)
  118. # 判断路径节点是否空;
  119. if paths.__len__() == 0:
  120. self.error("当前【%s】的路径节点空" % optionName)
  121. ocr_dict = [{"lan": "ChinesePRC+English", "type": 4}, {"lan": "ChinesePRC+English", "type": 253},
  122. {"lan": "ChinesePRC+English", "type": 10001}]
  123. else:
  124. # 如果状态False,退出;
  125. if self.status is False:
  126. self.warn("配置文件(%s)不存在,%s使用默认的ocr配置", self.configPath, optionName)
  127. ocr_dict = [{"lan": "ChinesePRC+English", "type": 4}, {"lan": "ChinesePRC+English", "type": 253},
  128. {"lan": "ChinesePRC+English", "type": 10001}]
  129. else:
  130. # 读取指定的ocr配置信息;
  131. for i in range(g_level.index(paths.__len__() - 1), -1, -1):
  132. cur_parent = paths['parent']
  133. first_parent = paths['first_parent']
  134. cur_level = g_level[i]
  135. # 是否有当前option的ocr配置;
  136. if self.has_option(cur_level, optionName + '.ocr'):
  137. self.warn("%s使用自身的ocr配置", optionName)
  138. ocr_dict = self.get_dict(self.get_value(cur_level, optionName + '.ocr'))
  139. break
  140. # 如果option本身没有配置,获取其父节点的配置;
  141. if self.has_option(cur_level, cur_parent + '.ocr'):
  142. self.warn("%s使用父节点%s的ocr配置", optionName, cur_parent)
  143. ocr_dict = self.get_dict(self.get_value(cur_level, cur_parent + '.ocr'))
  144. break
  145. # 如果option父级没有配置,则获取顶层配置(first parent);
  146. if self.has_option(cur_level, first_parent + '.ocr'):
  147. self.warn("%s使用顶层节点%s的ocr配置", optionName, first_parent)
  148. ocr_dict = self.get_dict(self.get_value(cur_level, first_parent + '.ocr'))
  149. break
  150. # end-for
  151. # end-if
  152. # end-if
  153. self.info("%s使用的ocr配置=%" % (optionName, ocr_dict))
  154. # 获取Value的OCR配置;
  155. def getValueOCRConfig(self, valueName, paths):
  156. pass
  157. if __name__ == "__main__":
  158. tData = CTData()
  159. upath = UITPathManage(tData)
  160. opc = CConfigManager(r'D:\SAT\resource\MenuTree\RT2851\2851', upath)
  161. # print opc.getThresholdDict("factory")
  162. print opc.getOptionICONConfig("picture")