Changeset 23 for collected/trunk

Show
Ignore:
Timestamp:
11/19/07 16:50:40 (14 months ago)
Author:
simon
Message:

Added some comments to walk.py

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • collected/trunk/python-msaa/walk.py

    r22 r23  
     1# walk.py 
     2# 
     3# A Python program that walks through the MSAA hierarchy, printing 
     4# information about each accessible object that it finds. 
     5# 
     6# Requirements: Python and comtypes 
     7# 
     8# (tested with Python 2.5 and comtypes 0.4.0 on Windows XP) 
     9# 
     10# comtypes 
     11# http://sourceforge.net/projects/comtypes/ 
     12# 
     13# Microsoft Active Accessibility 
     14# http://msdn2.microsoft.com/en-us/library/ms697707.aspx 
     15# 
     16# Active Accessibility Client Interfaces and Functions 
     17# http://msdn2.microsoft.com/en-us/library/ms696161.aspx 
     18# 
     19# IAccessible 
     20# http://msdn2.microsoft.com/en-us/library/ms696165.aspx 
     21 
    122from ctypes import windll, oledll, c_long, byref, POINTER, WINFUNCTYPE 
    223from ctypes.wintypes import HWND, LPARAM 
     
    627from comtypes.gen.Accessibility import IAccessible 
    728 
     29# some MSAA constants 
    830OBJID_WINDOW = 0 
    931CHILDID_SELF = 0 
     
    7698} 
    7799 
    78 step = 4 
    79  
    80100prototype = WINFUNCTYPE(None, HWND, LPARAM) 
    81101 
     102# passed to windll.user32.EnumWindows below to be called on each window 
    82103def callback(hwnd, lParam): 
     104    """retrieve the IAccessible interface for the window hwnd and inspect it""" 
    83105    ppvObject = POINTER(IAccessible)() 
    84106    oledll.oleacc.AccessibleObjectFromWindow(hwnd, OBJID_WINDOW, 
     
    86108    inspect(ppvObject) 
    87109 
    88 def inspect(ia, indent=0): 
    89     varID = VARIANT(CHILDID_SELF) 
    90     accName = ia.accName[varID] 
     110def inspect(ia, indent=0, indent_step=4): 
     111    """print out the name, role, and number of children for the 
     112    IAccessible ia and each of its children""" 
     113    # retrieve the name, role, and number of children for ia 
     114    selfID = VARIANT(CHILDID_SELF) 
     115    accName = ia.accName[selfID] 
    91116    if accName == None or accName == '': 
    92117        accName = 'NAMELESS' 
    93118    else: 
    94119        accName = unicode(accName).encode('utf_8') 
    95     accRole = ia.accRole[varID] 
     120    accRole = ia.accRole[selfID] 
    96121    if accRole in roles: 
    97122        accRoleText = roles[accRole] 
     
    101126    print '%s%s (%s, %d children)' % (' ' * indent, accName, accRoleText, accChildCount) 
    102127 
     128    # recurse through the accessible children 
    103129    if accChildCount > 0: 
     130        # allocate memory for information about the children 
     131        # (VARIANT structures) 
    104132        VariantArrayType = VARIANT * accChildCount 
    105133        rgvarChildren = VariantArrayType() 
     
    107135        oledll.oleacc.AccessibleChildren(ia, 0, accChildCount, rgvarChildren, 
    108136                                         byref(pcObtained)) 
     137        # child.vt may be VT_I4 or VT_DISPATCH 
     138        # see the MSDN documentation on the AccessibleChildren function 
     139        # http://msdn2.microsoft.com/en-us/library/ms697243.aspx 
    109140        for child in rgvarChildren: 
    110141            if child.vt == VT_I4: 
     
    112143                    ppdispChild = ia.accChild(child) 
    113144                    if ppdispChild: 
    114                         inspect(ppdispChild.QueryInterface(IAccessible), indent + step) 
     145                        inspect(ppdispChild.QueryInterface(IAccessible), 
     146                                indent + indent_step, indent_step) 
    115147                except: 
    116148                    pass 
    117149            elif child.vt == VT_DISPATCH: 
    118                 inspect(child.value.QueryInterface(IAccessible), indent + step) 
     150                inspect(child.value.QueryInterface(IAccessible), 
     151                        indent + indent_step, indent_step) 
    119152 
    120 windll.user32.EnumWindows(prototype(callback), None) 
     153if __name__ == '__main__': 
     154    # call our callback function on each window 
     155    windll.user32.EnumWindows(prototype(callback), None)