[go: up one dir, main page]

Menu

[r1437]: / mcomix / callback.py  Maximize  Restore  History

Download this file

122 lines (94 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# -*- coding: utf-8 -*-
import traceback
import weakref
import threading
import gobject
from mcomix import log
class CallbackList(object):
""" Helper class for implementing callbacks within the main thread.
Add listeners to method calls with method += callback_function. """
def __init__(self, obj, function):
self.__callbacks = []
self.__object = obj
self.__function = function
def __call__(self, *args, **kwargs):
""" Runs the wrapped function. After the funtion has finished,
callbacks are run. Code within the function and the callback is
always executed in the main thread. """
if threading.currentThread().name == 'MainThread':
if self.__object:
# Assume that the Callback object is bound to a class method.
result = self.__function(self.__object, *args, **kwargs)
else:
# Otherwise, the callback should be bound to a normal function.
result = self.__function(*args, **kwargs)
self.__run_callbacks(*args, **kwargs)
return result
else:
# Call this method again in the main thread.
gobject.idle_add(self.__mainthread_call, (args, kwargs))
def __iadd__(self, function):
""" Support for 'method += callback_function' syntax. """
obj, func = self.__get_function(function)
if (obj, func) not in self.__callbacks:
self.__callbacks.append((obj, func))
return self
def __isub__(self, function):
""" Support for 'method -= callback_function' syntax. """
obj, func = self.__get_function(function)
if (obj, func) in self.__callbacks:
self.__callbacks.remove((obj, func))
return self
def __mainthread_call(self, params):
""" Helper function to execute code in the main thread.
This will be called by gobject.idle_add, with <params> being a tuple
of (args, kwargs). """
result = self(*params[0], **params[1])
# Remove this function from the idle queue
return 0
def __run_callbacks(self, *args, **kwargs):
""" Executes callback functions. """
for obj_ref, func in self.__callbacks:
if obj_ref is None:
# Callback is a normal function
callback = func
elif obj_ref() is not None:
# Callback is a bound method.
# Recreate it by binding the function to the object.
callback = func.__get__(obj_ref())
else:
# Callback is a bound method, object
# no longer exists.
callback = None
if callback:
try:
callback(*args, **kwargs)
except Exception, e:
log.error(_('! Callback %(function)r failed: %(error)s'),
{ 'function' : callback, 'error' : e })
log.debug('Traceback:\n%s', traceback.format_exc())
def __callback_deleted(self, obj_ref):
""" Called whenever one of the callback objects is collected by gc.
This removes all callback functions registered by the object. """
self.__callbacks = filter(lambda callback: callback[0] != obj_ref,
self.__callbacks)
def __get_function(self, func):
""" If <func> is a normal function, return (None, func).
If <func> is a bound method, return (weakref(obj), func), with <obj>
being the object <func> is bound to. This is required since
weak references do not work on bound methods. """
if hasattr(func, "im_self") and getattr(func, "im_self") is not None:
return (weakref.ref(func.im_self, self.__callback_deleted), func.im_func)
else:
return (None, func)
class Callback(object):
""" Decorator class for using the CallbackList helper. """
def __init__(self, function):
# This is the function the Callback is decorating.
self.__function = function
def __get__(self, obj, cls):
""" This method makes Callback implement the descriptor interface.
Enables calling bound methods with the correct <self> reference.
Do not ask me why or how this actually works, I simply do not know. """
return CallbackList(obj, self.__function)
# vim: expandtab:sw=4:ts=4