diff --git a/pyia/__init__.py b/pyia/__init__.py index 9118ab0..24a126b 100644 --- a/pyia/__init__.py +++ b/pyia/__init__.py @@ -29,10 +29,10 @@ GetModule('oleacc.dll') from comtypes.gen.Accessibility import IAccessible del GetModule -import accessible -from utils import * -from constants import * -import registry +from . import accessible +from .utils import * +from .constants import * +from . import registry # Create singleton registry. Registry = registry.Registry() diff --git a/pyia/accessible.py b/pyia/accessible.py index 8f1df77..8a2deb3 100644 --- a/pyia/accessible.py +++ b/pyia/accessible.py @@ -25,13 +25,12 @@ Boston, MA 02111-1307, USA. ''' -import new import types from comtypes.automation import VARIANT, VT_I4, VT_DISPATCH from ctypes import c_long, oledll, byref, create_unicode_buffer from comtypes.gen.Accessibility import IAccessible from comtypes import named_property, COMError, hresult -from constants import CHILDID_SELF, \ +from .constants import CHILDID_SELF, \ UNLOCALIZED_ROLE_NAMES, \ UNLOCALIZED_STATE_NAMES @@ -46,7 +45,7 @@ def _makeExceptionHandler(func): def _inner(self, *args, **kwargs): try: return func(self, *args, **kwargs) - except COMError, e: + except COMError as e: # TODO: Translate COMErrors to more pythonic equivalents. raise return _inner @@ -61,13 +60,13 @@ def _mixExceptions(cls): ''' # get a method type as a reference from a known method # loop over all names in the new class - for name in cls.__dict__.keys(): + for name in list(cls.__dict__.keys()): obj = cls.__dict__[name] # check if we're on a protected or private method if name.startswith('_'): continue # check if we're on a method - elif isinstance(obj, new.instancemethod): + elif isinstance(obj, types.MethodType): # wrap the function in an exception handler method = _makeExceptionHandler(obj) # add the wrapped function to the class @@ -110,13 +109,13 @@ def _mixClass(cls, new_cls, ignore=[]): @type ignore: iterable ''' # loop over all names in the new class - for name, func in new_cls.__dict__.items(): + for name, func in list(new_cls.__dict__.items()): if name in ignore: continue if isinstance(func, types.FunctionType): # build a new function that is a clone of the one from new_cls - method = new.function(func.func_code, func.func_globals, name, - func.func_defaults, func.func_closure) + method = types.FunctionType(func.__code__, func.__globals__, name, + func.__defaults__, func.__closure__) try: # check if a method of the same name already exists in the # target @@ -180,7 +179,7 @@ def __iter__(self): rgvarChildren, byref(pcObtained)) except: pcObtained = c_long(0) - for i in xrange(pcObtained.value): + for i in range(pcObtained.value): child = rgvarChildren[i] if child.vt == VT_I4: yield ManagedChildAccessible(self, child.value) @@ -189,11 +188,11 @@ def __iter__(self): def __str__(self): try: - return u'[%s | %s]' % (self.accRoleName(), + return '[%s | %s]' % (self.accRoleName(), self.accName(CHILDID_SELF) or '') except: raise - return u'[DEAD]' + return '[DEAD]' def __len__(self): return self.accChildCount @@ -201,7 +200,7 @@ def __len__(self): def accStateSet(self, child_id=CHILDID_SELF): states = [] state = self.accState(child_id) - for shift in xrange(64): + for shift in range(64): state_bit = 1 << shift if state_bit & state: states.append( @@ -212,7 +211,7 @@ def accStateSet(self, child_id=CHILDID_SELF): def accLocalizedStateSet(self, child_id=CHILDID_SELF): states = [] state = self.accState(child_id) - for shift in xrange(64): + for shift in range(64): state_bit = 1 << shift if state_bit & state: states.append(self._getStateText(state_bit & state)) @@ -273,15 +272,15 @@ def __getattr__(self, name): getattr(self.parent, name), self.child_id) raise AttributeError - def __nonzero__(self): + def __bool__(self): return True def __str__(self): try: - return u'[%s | %s]' % (self.accRoleName(), + return '[%s | %s]' % (self.accRoleName(), self.accName() or '') except: - return u'[DEAD]' + return '[DEAD]' def __len__(self): return 0 diff --git a/pyia/constants.py b/pyia/constants.py index c5964c0..b5946b8 100644 --- a/pyia/constants.py +++ b/pyia/constants.py @@ -92,67 +92,67 @@ # Unlocalized role strings UNLOCALIZED_ROLE_NAMES = { - 1: u'title bar', - 2: u'menu bar', - 3: u'scroll bar', - 4: u'grip', - 5: u'sound', - 6: u'cursor', - 7: u'caret', - 8: u'alert', - 9: u'window', - 10: u'client', - 11: u'popup menu', - 12: u'menu item', - 13: u'tool tip', - 14: u'application', - 15: u'document', - 16: u'pane', - 17: u'chart', - 18: u'dialog', - 19: u'border', - 20: u'grouping', - 21: u'separator', - 22: u'tool bar', - 23: u'status bar', - 24: u'table', - 25: u'column header', - 26: u'row header', - 27: u'column', - 28: u'row', - 29: u'cell', - 30: u'link', - 31: u'help balloon', - 32: u'character', - 33: u'list', - 34: u'list item', - 35: u'outline', - 36: u'outline item', - 37: u'page tab', - 38: u'property page', - 39: u'indicator', - 40: u'graphic', - 41: u'text', - 42: u'editable text', - 43: u'push button', - 44: u'check box', - 45: u'radio button', - 46: u'combo box', - 47: u'drop down', - 48: u'progress bar', - 49: u'dial', - 50: u'hot key field', - 51: u'slider', - 52: u'spin box', - 53: u'diagram', - 54: u'animation', - 55: u'equation', - 56: u'drop down button', - 57: u'menu button', - 58: u'grid drop down button', - 59: u'white space', - 60: u'page tab list', - 61: u'clock'} + 1: 'title bar', + 2: 'menu bar', + 3: 'scroll bar', + 4: 'grip', + 5: 'sound', + 6: 'cursor', + 7: 'caret', + 8: 'alert', + 9: 'window', + 10: 'client', + 11: 'popup menu', + 12: 'menu item', + 13: 'tool tip', + 14: 'application', + 15: 'document', + 16: 'pane', + 17: 'chart', + 18: 'dialog', + 19: 'border', + 20: 'grouping', + 21: 'separator', + 22: 'tool bar', + 23: 'status bar', + 24: 'table', + 25: 'column header', + 26: 'row header', + 27: 'column', + 28: 'row', + 29: 'cell', + 30: 'link', + 31: 'help balloon', + 32: 'character', + 33: 'list', + 34: 'list item', + 35: 'outline', + 36: 'outline item', + 37: 'page tab', + 38: 'property page', + 39: 'indicator', + 40: 'graphic', + 41: 'text', + 42: 'editable text', + 43: 'push button', + 44: 'check box', + 45: 'radio button', + 46: 'combo box', + 47: 'drop down', + 48: 'progress bar', + 49: 'dial', + 50: 'hot key field', + 51: 'slider', + 52: 'spin box', + 53: 'diagram', + 54: 'animation', + 55: 'equation', + 56: 'drop down button', + 57: 'menu button', + 58: 'grid drop down button', + 59: 'white space', + 60: 'page tab list', + 61: 'clock'} # Navigation constants @@ -201,37 +201,37 @@ # Unlocalized state strings UNLOCALIZED_STATE_NAMES = { - 1: u'unavailable', - 2: u'selected', - 4: u'focused', - 8: u'pressed', - 16: u'checked', - 32: u'mixed', - 64: u'read only', - 128: u'hot tracked', - 256: u'default', - 512: u'expanded', - 1024: u'collapsed', - 2048: u'busy', - 4096: u'floating', - 8192: u'marqueed', - 16384: u'animated', - 32768: u'invisible', - 65536: u'offscreen', - 131072: u'sizeable', - 262144: u'moveable', - 524288: u'self voicing', - 1048576: u'focusable', - 2097152: u'selectable', - 4194304: u'linked', - 8388608: u'traversed', - 16777216: u'multiple selectable', - 33554432: u'extended selectable', - 67108864: u'alert low', - 134217728: u'alert medium', - 268435456: u'alert high', - 536870912: u'protected', - 1073741824: u'has popup'} + 1: 'unavailable', + 2: 'selected', + 4: 'focused', + 8: 'pressed', + 16: 'checked', + 32: 'mixed', + 64: 'read only', + 128: 'hot tracked', + 256: 'default', + 512: 'expanded', + 1024: 'collapsed', + 2048: 'busy', + 4096: 'floating', + 8192: 'marqueed', + 16384: 'animated', + 32768: 'invisible', + 65536: 'offscreen', + 131072: 'sizeable', + 262144: 'moveable', + 524288: 'self voicing', + 1048576: 'focusable', + 2097152: 'selectable', + 4194304: 'linked', + 8388608: 'traversed', + 16777216: 'multiple selectable', + 33554432: 'extended selectable', + 67108864: 'alert low', + 134217728: 'alert medium', + 268435456: 'alert high', + 536870912: 'protected', + 1073741824: 'has popup'} # SetWinEventHook() flags WINEVENT_OUTOFCONTEXT = 0x0 @@ -292,6 +292,6 @@ winEventIDsToEventNames={} -for _sym, _val in locals().items(): +for _sym, _val in list(locals().items()): if _sym.startswith('EVENT_'): winEventIDsToEventNames[_val] = _sym diff --git a/pyia/event.py b/pyia/event.py index b6d9e7f..47ee7f9 100644 --- a/pyia/event.py +++ b/pyia/event.py @@ -24,8 +24,8 @@ Boston, MA 02111-1307, USA. ''' -from constants import winEventIDsToEventNames -from utils import accessibleObjectFromEvent +from .constants import winEventIDsToEventNames +from .utils import accessibleObjectFromEvent class Event(object): def __init__(self, diff --git a/pyia/registry.py b/pyia/registry.py index a522472..9a8dfe7 100644 --- a/pyia/registry.py +++ b/pyia/registry.py @@ -25,12 +25,12 @@ Boston, MA 02111-1307, USA. ''' -import constants +from . import constants import traceback from ctypes import CFUNCTYPE, c_int, c_voidp, windll from comtypes.client import PumpEvents -from event import Event -from utils import accessibleObjectFromEvent +from .event import Event +from .utils import accessibleObjectFromEvent class Registry(object): def __init__(self): @@ -46,7 +46,7 @@ def __call__(self): def _handleEvent(self, handle, eventID, window, objectID, childID, threadID, timestamp): e = Event(eventID, window, objectID, childID, threadID, timestamp) - for client, event_type in self.clients.keys(): + for client, event_type in list(self.clients.keys()): if event_type == eventID: try: client(e) @@ -55,7 +55,7 @@ def _handleEvent(self, handle, eventID, window, objectID, childID, def registerEventListener(self, client, *event_types): for event_type in event_types: - if self.clients.has_key((client, event_type)): + if (client, event_type) in self.clients: continue hook_id = \ windll.user32.SetWinEventHook( @@ -64,8 +64,8 @@ def registerEventListener(self, client, *event_types): if hook_id: self.clients[(client, event_type)] = hook_id else: - print "Could not register callback for %s" % \ - constants.winEventIDsToEventNames.get(event_type, event_type) + print("Could not register callback for %s" % \ + constants.winEventIDsToEventNames.get(event_type, event_type)) def deregisterEventListener(self, client, *event_types): for event_type in event_types: diff --git a/pyia/utils.py b/pyia/utils.py index d8359ff..22a3578 100644 --- a/pyia/utils.py +++ b/pyia/utils.py @@ -24,7 +24,7 @@ Boston, MA 02111-1307, USA. ''' -import constants +from . import constants from ctypes import windll, oledll, POINTER, byref, c_int from comtypes.automation import VARIANT from comtypes.gen.Accessibility import IAccessible @@ -198,7 +198,7 @@ def findAncestor(acc, pred): acc = parent def printSubtree(acc, indent=0): - print '%s%s' % (indent*' ', unicode(acc).encode('cp1252', 'ignore')) + print('%s%s' % (indent*' ', str(acc).encode('cp1252', 'ignore'))) for child in acc: try: printSubtree(child, indent+1)