Package AccessEngine :: Package AEWalkers :: Module AccessibleWalker
[hide private]
[frames] | no frames]

Source Code for Module AccessEngine.AEWalkers.AccessibleWalker

  1  ''' 
  2  Defines a class that provides a "raw" walk of all accessibles in the hiearchy. 
  3   
  4  @author: Peter Parente 
  5  @organization: IBM Corporation 
  6  @copyright: Copyright (c) 2005, 2007 IBM Corporation 
  7  @license: The BSD License 
  8   
  9  All rights reserved. This program and the accompanying materials are made 
 10  available under the terms of the BSD license which accompanies 
 11  this distribution, and is available at 
 12  U{http://www.opensource.org/licenses/bsd-license.php} 
 13  ''' 
 14  from Base import AEWalker 
 15  from AccessEngine.AEAccInterfaces import * 
 16   
17 -class AccessibleWalker(AEWalker):
18 ''' 19 Walks the accessible hierarchy exactly as it is presented. The walk order is 20 an in-order traversal of the subtree of the accessible hierarchy rooted at a 21 top-level window accessible. The subtree is assumed to have no loops, 22 though logic could be added to detect them. 23 '''
24 - def _getFirstChild(self, por):
25 ''' 26 Gets the first child of the current accessible in the given L{AEPor}. Returns 27 the child accessible if it exists. If it does not exist, returns the given 28 L{AEPor} and L{_getNextPeer} as the method to call to continue the search. 29 30 @param por: Initial L{AEPor} 31 @type por: L{AEPor} 32 @return: L{AEPor} and the next method to call to continue the search, or 33 L{AEPor} and None to indicate the search is complete 34 @rtype: 2-tuple of L{AEPor}, callable 35 ''' 36 try: 37 # try to get first child accessible 38 child = IAccessibleNav(por).getFirstAccChild() 39 return (child, None) 40 except (LookupError, IndexError): 41 # there's no child accessible 42 return (por, self._getNextPeer)
43
44 - def _getNextPeer(self, por):
45 ''' 46 Gets the next peer of the current accessible in the given L{AEPor}. Returns 47 the peer accessible if it exists, is visible, and is not trivial. If it does 48 not exist, returns the given L{AEPor} and L{_getParentNextPeer} as the method 49 to call to continue the search. If it is invisible, returns the peer L{AEPor} 50 and L{_getNextPeer} as the method to call to continue the search. If it is 51 trivial, returns the peer and L{_getFirstChild} as the method to call to 52 continue the search. If it is trivial, returns the child accessible and 53 L{_getFirstChild} as the method to call to continue the search. 54 55 @param por: Initial L{AEPor} 56 @type por: L{AEPor} 57 @return: L{AEPor} and the next method to call to continue the search, or 58 L{AEPor} and None to indicate the search is complete 59 @rtype: 2-tuple of L{AEPor}, callable 60 ''' 61 try: 62 # try getting the next peer accessible 63 next = IAccessibleNav(por).getNextAcc() 64 return (next, None) 65 except (LookupError, IndexError): 66 # there's no next peer accessible 67 return (por, self._getParentNextPeer)
68
69 - def _getParentNextPeer(self, por):
70 ''' 71 Gets the parent accessible of the current accessible in the given L{AEPor}. 72 Returns the parent and L{_getNextPeer} as the method to call to continue the 73 search if the parent exists. Returns a sentinel (None, None) if there is no 74 parent indicating the given L{AEPor} is the root of the subtree containing 75 the starting L{AEPor}. 76 77 @param por: Initial L{AEPor} 78 @type por: L{AEPor} 79 @return: L{AEPor} and the next method to call to continue the search, or 80 L{AEPor} and None to indicate the search is complete 81 @rtype: 2-tuple of L{AEPor}, callable 82 ''' 83 try: 84 parent = IAccessibleNav(por).getParentAcc() 85 if IAccessibleInfo(parent).isAccTopLevelWindow(): 86 # stop if the parent is the root of the active window 87 return (None, None) 88 else: 89 # get the next peer of the parent accessible 90 return (parent, self._getNextPeer) 91 except (LookupError, IndexError): 92 # there's no parent, so bail because we're on the last accessible 93 return (None, None)
94
95 - def _getPrevPeer(self, por):
96 ''' 97 Gets the previous peer of the current accessible in the given L{AEPor}. If it 98 does not exist, returns the given L{AEPor} and L{_getParent} as the method to 99 call to continue the search. If it is not visible, returns the peer 100 accessible and L{_getPrevPeer} as the method to call to continue the search. 101 Otherwise, returns the peer accessible and L{_getLastChild} as the method to 102 call to continue the search. 103 104 @param por: Initial L{AEPor} 105 @type por: L{AEPor} 106 @return: L{AEPor} and the next method to call to continue the search, or 107 L{AEPor} and None to indicate the search is complete 108 @rtype: 2-tuple of L{AEPor}, callable 109 ''' 110 try: 111 # try to get the previous peer 112 prev = IAccessibleNav(por).getPrevAcc() 113 ai = IAccessibleInfo(prev) 114 if ai.isAccTopLevelWindow(): 115 # stop if the parent is the root of the active window 116 return (None, None) 117 else: 118 # get the deepest last child 119 return (prev, self._getLastChild) 120 except (LookupError, IndexError): 121 # there's no previous peer accessible 122 return (por, self._getParent)
123
124 - def _getLastChild(self, por):
125 ''' 126 Gets the last child of the accessible in the given L{AEPor}. If it does not 127 exist, checks if the given L{AEPor} is invisible or trivial. If so, returns 128 the given L{AEPor} and L{_getPrevPeer} to continue the search. If not, returns 129 a L{AEPor} to the last item in the given L{AEPor} as the result. 130 131 If the last child does exist, checks if it is visible. If so, returns the 132 child and L{_getLastChild} to continue the search. If not, returns the 133 child and L{_getPrevPeer} to continue the search. 134 135 @param por: Initial L{AEPor} 136 @type por: L{AEPor} 137 @return: L{AEPor} and the next method to call to continue the search, or 138 L{AEPor} and None to indicate the search is complete 139 @rtype: 2-tuple of L{AEPor}, callable 140 ''' 141 try: 142 # try to get the last child 143 child = IAccessibleNav(por).getLastAccChild() 144 # try for the next deeper last child 145 return (child, self._getLastChild) 146 except (LookupError, IndexError): 147 #use the last child 148 return (por, None)
149
150 - def _getParent(self, por):
151 ''' 152 Gets the parent accessible of the one in the given L{AEPor}. Returns the last 153 item in the parent if it exists. If it does not exist, Returns a sentinel 154 (None, None) indicating the given L{AEPor} is the root of the subtree 155 containing the starting L{AEPor}. If the parent is invisible or trivial, 156 returns the parent and L{_getPrevPeer} as the method to call to continue the 157 search. 158 159 @param por: Initial L{AEPor} 160 @type por: L{AEPor} 161 @return: L{AEPor} and the next method to call to continue the search, or 162 L{AEPor} and None to indicate the search is complete, or None and None to 163 indicate we're at the root 164 @rtype: 2-tuple of L{AEPor}, callable 165 ''' 166 try: 167 # try to get the parent 168 parent = IAccessibleNav(por).getParentAcc() 169 # if there is a parent, use it 170 return (parent, None) 171 except (LookupError, IndexError): 172 # there's no parent, so bail because we're at the root 173 return (None, None)
174
175 - def _getLastAcc(self, por):
176 ''' 177 Gets the last accessible of the given L{AEPor}. Returns the given L{AEPor} if 178 any errors occur. 179 180 @param por: Initial L{AEPor} 181 @type por: L{AEPor} 182 @return: L{AEPor} pointing to the last accessible in the given L{AEPor} 183 @rtype: L{AEPor} 184 ''' 185 try: 186 return IAccessibleNav(por).getLastAccChild() 187 except LookupError: 188 # just use the current por if there is an error 189 return por
190
191 - def getNextPOR(self):
192 ''' 193 Gets the next L{AEPor} in the walk order. Calls L{_getFirstChild}, 194 L{_getNextPeer}, and L{_getParentNextPeer} to attempt to get the next valid 195 L{AEPor}. Each method determines whether the L{AEPor} is valid as the next 196 L{AEPor}, and, if not, which call to make next. Each method potentially 197 returns a L{AEPor} and the next method to call to continue the search for the 198 next L{AEPor}. 199 200 @return: Next L{AEPor} or None if this is the last L{AEPor} 201 @rtype: L{AEPor} or None 202 ''' 203 state = self._getFirstChild 204 por = self.por 205 while state is not None: 206 por, state = state(por) 207 if por is not None: 208 self.por = por 209 return por
210
211 - def getPrevPOR(self):
212 ''' 213 Gets the previous L{AEPor} in the walk order. Calls L{_getPrevPeer}, 214 L{_getLastChild}, and L{_getParent} to attempt to get the previous valid 215 L{AEPor}. Each method determines whether the L{AEPor} is valid as the previous 216 L{AEPor}, and, if not, which call to make next. Each method potentially 217 returns a L{AEPor} and the next method to call to continue the search for the 218 previous L{AEPor}. 219 220 @return: Previous L{AEPor} or None if this is the first L{AEPor} 221 @rtype: L{AEPor} or None 222 ''' 223 state = self._getPrevPeer 224 por = self.por 225 while state is not None: 226 por, state = state(por) 227 if por is not None: 228 self.por = por 229 return por
230
231 - def getParentPOR(self):
232 ''' 233 @return: Point of regard to the parent of the current accessible or None if 234 it does not exist 235 @rtype: L{AEPor} 236 ''' 237 try: 238 self.por = IAccessibleNav(self.por).getParentAcc() 239 except LookupError: 240 return None 241 return self.por
242
243 - def getFirstPOR(self):
244 ''' 245 @return: Point of regard to the first accessible 246 @rtype: L{AEPor} 247 ''' 248 por = self.por 249 child = self.por 250 while 1: 251 try: 252 parent = IAccessibleNav(por).getParentAcc() 253 except LookupError: 254 self.por = child 255 return child 256 else: 257 child = por 258 por = parent
259
260 - def getLastPOR(self):
261 ''' 262 @return: Point of regard to the last accessible 263 @rtype: L{AEPor} 264 ''' 265 # get to the first POR first so we can traverse the very last branch 266 self.getFirstPOR() 267 while 1: 268 try: 269 self.por = IAccessibleNav(self.por).getLastAccChild() 270 except LookupError: 271 # stop when we can find any more children 272 return self.por
273