Package dogtail :: Module path
[hide private]
[frames] | no frames]

Source Code for Module dogtail.path

  1  # -*- coding: utf-8 -*- 
  2  """ 
  3  Author: David Malcolm <dmalcolm@redhat.com> 
  4  """ 
  5  __author__ = """David Malcolm <dmalcolm@redhat.com>""" 
  6   
7 -class SearchPath:
8 """ 9 Class used by the recording framework (and for more verbose script 10 logging) for identifying nodes in a persistent way, independent of the 11 style of script being written. 12 13 Implemented as a list of (predicate, isRecursive) pairs, giving the 14 'best' way to find the Accessible wrapped by a Node, starting at the 15 root and applying each search in turn. 16 17 This is somewhat analagous to an absolute path in a filesystem, except 18 that some of searches may be recursive, rather than just searching 19 direct children. 20 21 FIXME: try to ensure uniqueness 22 FIXME: need some heuristics to get 'good' searches, whatever 23 that means 24 """ 25
26 - def __init__(self):
27 self.__list = []
28
29 - def __str__(self):
30 result = "{" 31 for (predicate, isRecursive) in self.__list: 32 result += "/(%s,%s)"%(predicate.describeSearchResult(), isRecursive) 33 return result+"}"
34 35 # We need equality to work so that dicts of these work:
36 - def __eq__(self, other):
37 # print "eq: self:%s"%self 38 # print " other:%s"%other 39 if len(self.__list) != len(other.__list): 40 # print "nonequal length" 41 return False 42 else: 43 for i in range(len(self.__list)): 44 if self.__list[i]!=other.__list[i]: 45 return False 46 # print True 47 return True
48
49 - def append(self, predicate, isRecursive):
50 assert predicate 51 self.__list.append((predicate, isRecursive))
52
53 - def __iter__(self):
54 return iter(self.__list)
55
56 - def length(self):
57 return len(self.__list)
58
59 - def makeScriptMethodCall(self):
60 """ 61 Used by the recording system. 62 63 Generate the Python source code that will carry out this search. 64 """ 65 result = "" 66 for (predicate, isRecursive) in self.__list: 67 # print predicate 68 # print self.generateVariableName(predicate) 69 result += "." + predicate.makeScriptMethodCall(isRecursive) 70 return result
71
72 - def getRelativePath(self, other):
73 """ 74 Given another SearchPath instance, if the other is 'below' this 75 one, return a SearchPath that describes how to reach it relative 76 to this one (a copy of the second part of the list). Otherwise 77 return None. 78 """ 79 for i in range(len(self.__list)): 80 if self.__list[i]!=other.__list[i]: 81 break 82 if i>0: 83 # Slice from this point to the end: 84 result = SearchPath() 85 result.__list = other.__list[i+1:] 86 87 if False: 88 print "...................." 89 print "from %s"%self 90 print "to %s"%other 91 print "i=%s"%i 92 print "relative path %s"%result 93 print "...................." 94 95 return result 96 else: 97 return None
98
99 - def getPrefix(self, n):
100 """ 101 Get the first n components of this instance as a new instance 102 """ 103 result = SearchPath() 104 for i in range(n): 105 result.__list.append(self.__list[i]) 106 return result
107
108 - def getPredicate(self, i):
109 (predicate, isRecursive) = self.__list[i] 110 return predicate
111