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