Signal.py
Go to the documentation of this file.
00001 import inspect
00002 import weakref
00003 
00004 class Signal(object):
00005 
00006     def __init__(self):
00007         self._slots = []
00008         # storage to keep wrapper instances alive
00009         self._func_wrappers = set([])
00010 
00011     def emit(self, *args, **kwargs):
00012         for i in range(len(self._slots)):
00013             slot = self._slots[i]
00014             if slot != None:
00015                 slot(*args, **kwargs)
00016             else:
00017                 del self._slots[i]
00018 
00019     def connect(self, slot):
00020         if inspect.ismethod(slot):
00021             self._slots.append(_WeakMethod(slot))
00022         else:
00023             wrapper = _WeakMethod_FunctionWrapper(slot)
00024             self._slots.append(_WeakMethod(wrapper.call_function))
00025             self._func_wrappers.add(wrapper)
00026 
00027     def disconnect(self, slot):
00028         for i in range(len(self._slots)):
00029             weak_method = self._slots[i]
00030             if inspect.ismethod(slot):
00031                 if weak_method.is_equal(slot):
00032                     del self._slots[i]
00033                     return
00034             else:
00035                 if weak_method.instance().is_equal(slot):
00036                     self._func_wrappers.remove(weak_method.instance())
00037                     del self._slots[i]
00038                     return
00039 
00040     def disconnect_all(self):
00041         self._slots.clear()
00042 
00043 class _WeakMethod:
00044 
00045     def __init__(self, method):
00046         self.instance = weakref.ref(method.im_self)
00047         self.function = method.im_func
00048 
00049     def is_equal(self, method):
00050         return self.instance() == method.im_self and self.function == method.im_func 
00051 
00052     def __call__(self, *args, **kwargs):
00053         if self.instance() is not None:
00054             self.function(self.instance(), *args, **kwargs)
00055 
00056 class _WeakMethod_FunctionWrapper:
00057 
00058     def __init__(self, function):
00059         self._function = function
00060 
00061     def is_equal(self, function):
00062         return self._function == function
00063 
00064     def call_function(self, *args, **kwargs):
00065         self._function(*args, **kwargs)


slider_gui
Author(s): Dirk Thomas
autogenerated on Thu Jun 6 2019 20:32:11