~ubuntu-branches/ubuntu/utopic/dogtail/utopic

« back to all changes in this revision

Viewing changes to dogtail/path.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2006-12-21 13:33:47 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20061221133347-xo9jg11afp5plcka
Tags: upstream-0.6.1
ImportĀ upstreamĀ versionĀ 0.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
__author__ = """David Malcolm <dmalcolm@redhat.com>"""
6
6
 
7
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
 
        
 
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