UAT_PathManage.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding:utf-8 -*-
  2. from ssat_sdk.UATree.UAT_data import UATData
  3. from UAT_tree import UATTree
  4. from UAT_treeConstant import TreeConst
  5. from ssat_sdk.utils.string_util import strcmp
  6. from UAT_log import error,info,debug
  7. import os, sys, time
  8. DEBUG = True
  9. INFO = True
  10. ERROR = True
  11. class UATPathManage():
  12. def __init__(self):
  13. self.uatData = UATData()
  14. self.levelList = self.uatData.UATree.treeDict.keys()
  15. '''
  16. 构建一条从first parent到目标option路径。
  17. path最后一个元素,是目标option
  18. :return result,path. result:-1 代表option异常;0 代表路径中断异常;1 代表路径完整,找到了first parent
  19. '''
  20. def getOptionPath(self, option, parent = None):
  21. path = []
  22. if option is None and parent is None:
  23. error(str(self.__class__), "getOptionPath", "getPath Error:Param option is None!!", ERROR)
  24. return -1, path
  25. #获取option所在的parent
  26. elif parent is None:
  27. parent = self.uatData.getParentByOption(option)
  28. if parent is None:
  29. error(str(self.__class__), "getOptionPath",
  30. "getPath Error:option%s's parent not found!!" % (option[UATTree.TAB_NAME]), ERROR)
  31. return -1, path
  32. path.append(option)
  33. self.addPrevParentPath(parent, path)
  34. if path.__len__() == 0:
  35. result = -1
  36. elif strcmp(path[-1]["level"], "first") is False:
  37. error(str(self.__class__), "getOptionPath", "getPath Error:option%s path interrupt!!"%(option[UATTree.TAB_NAME]), ERROR)
  38. result = 0
  39. else:
  40. result = 1
  41. return result, path
  42. def addPrevParentPath(self, parent, path):
  43. if parent is None:
  44. return
  45. path.append(parent)
  46. prevParent = parent[UATTree.TAB_PREV_PARENT]
  47. if prevParent is None:
  48. return
  49. else:
  50. self.addPrevParentPath(prevParent, path)
  51. def info(self,path):
  52. for item in path:
  53. if item is not None:
  54. info(str(self.__class__), "info", "Path: " + item[UATTree.TAB_NAME], INFO)
  55. else:
  56. info(str(self.__class__), "info", "Path: " + str(item), INFO)
  57. '''
  58. 构建一条当前parent到option的路径。
  59. 如果当前电视页面分析,不在UATree中,则构建一条first parent到目标option中的目录。
  60. :return backPath, forwardPath. backPath是返回路径,forwardPath是去往目标option的路径。
  61. 注意:backPath路径是顺序执行, forwardPath的路径是要反过来逐步执行。
  62. backPath格式:[currentOption, currentParent, parent1, parent2, ..., FirstParent]
  63. forwardPath格式: [targetOption, targetParent, parent1, parent2, ..., FirstParent]
  64. forwardPath路径为空:表示路径异常。
  65. forwardPath路径包含option和parent:表示在路径最后一个parent所在页面,或者表示需要从first parent进入
  66. 1 如果backPath为空,则表示forwardPath从First parent重新进入。
  67. 2 如果backPath包含option和parent,则表示在backPath的option所在页面,需要返回到forwardPath最后一个parent所在页面。
  68. backPath返回到forwardPath最后一个parent所在页面:
  69. 注意:backPath和forwardPath的最后一个parent,一定会一样。
  70. 1 backPath,返回到最后一个parent页面。
  71. 2 在backPath最后一个parent页面,即forwardPath最后一个parent页面,再依次递进,进入targetParent页面,选中targetOption。
  72. '''
  73. def genOptionSmartPath(self, curParent, option):
  74. backPath = []
  75. forwardPath = []
  76. if curParent is None:
  77. result,forwardPath = self.getOptionPath(option)
  78. return backPath,forwardPath
  79. print "genOptionSmartPath:curParent:", curParent
  80. curOptionDict = curParent[UATTree.TAB_OPTION]
  81. if curOptionDict.__len__() > 0:
  82. curOption = curOptionDict[curOptionDict.keys()[0]]
  83. else:
  84. curOption = None
  85. resutl1, toFirstPath = self.getOptionPath(curOption, parent=curParent)
  86. print "toFirstPath:",self.info(toFirstPath)
  87. resutl2, targetOptionPath = self.getOptionPath(option)
  88. print "targetOptionPath:",self.info(targetOptionPath)
  89. if resutl1 < 1 and resutl2 == 1:
  90. forwardPath = targetOptionPath
  91. return backPath, forwardPath
  92. elif resutl1 < 1:
  93. return backPath, forwardPath
  94. #levelMin代表parent的个数,所以要去掉option计数
  95. fLen = toFirstPath.__len__()
  96. tLen = targetOptionPath.__len__()
  97. levelMin = min(fLen, tLen) - 1
  98. for i in range(levelMin): #仅核对parent部分
  99. print toFirstPath[fLen-1-i][UATTree.TAB_NAME],targetOptionPath[tLen-1-i][UATTree.TAB_NAME]
  100. #从first parent开始核对,找到第一个不同点
  101. if toFirstPath[fLen-1-i][UATTree.TAB_NAME] <> targetOptionPath[tLen-1-i][UATTree.TAB_NAME]:
  102. if i == 0: #first parent不吻合。toFirstPath和targetOptionPath路径没有交点
  103. print "<> i == 0:", i
  104. forwardPath = targetOptionPath
  105. else: #toFirstPath和targetOptionPath路径是相交关系
  106. print "<> else:", i
  107. backPath = toFirstPath[0:fLen-i+1] #取到上一个相同的parent。
  108. forwardPath = targetOptionPath[0:tLen-i+1] # 取到上一个相同的parent。
  109. break
  110. if i == levelMin-1:#toFirstPath和targetOptionPath路径是包含关系
  111. print "i == levelMin:", i
  112. backPath = toFirstPath[0:fLen-i] # 包含关系,取到当前相同parent
  113. forwardPath = targetOptionPath[0:tLen-i] #包含关系,取到当前相同parent
  114. return backPath, forwardPath
  115. if __name__ == '__main__':
  116. uatPathManage = UATPathManage()
  117. parentName = "av_devices_settings"
  118. curParent = uatPathManage.uatData.getParentDict(parentName)
  119. print "curParent:", curParent
  120. option = uatPathManage.uatData.getOption("av_vivid")
  121. backPath, forwardPath = uatPathManage.genOptionSmartPath(curParent,option)
  122. print "option:", option
  123. print "backPath:"
  124. uatPathManage.info(backPath)
  125. print "forwardPath:"
  126. uatPathManage.info(forwardPath)