Update of /cvsroot/freevo/kaa/base/src/notifier
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12245/notifier

Modified Files:
        __init__.py callback.py nf_generic.py 
Removed Files:
        signals.py 
Log Message:
Rewrote Signal class and moved it into callback.py; removed signals.py;
fixed typo in nf_generic.py in removeDispatcher().


--- signals.py DELETED ---

Index: nf_generic.py
===================================================================
RCS file: /cvsroot/freevo/kaa/base/src/notifier/nf_generic.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** nf_generic.py       12 Jul 2005 14:06:57 -0000      1.2
--- nf_generic.py       13 Jul 2005 20:16:25 -0000      1.3
***************
*** 114,118 ****
      global __dispatchers
      if method in __dispatchers:
!         __dispatcher.remove( method )
  
  def step( sleep = True, external = True ):
--- 114,118 ----
      global __dispatchers
      if method in __dispatchers:
!         __dispatchers.remove( method )
  
  def step( sleep = True, external = True ):

Index: __init__.py
===================================================================
RCS file: /cvsroot/freevo/kaa/base/src/notifier/__init__.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** __init__.py 13 Jul 2005 19:43:27 -0000      1.8
--- __init__.py 13 Jul 2005 20:16:24 -0000      1.9
***************
*** 37,46 ****
  from posixsignals import *
  from posixsignals import register as signal
- from signals import Signal, WeakRefMethod
  from popen import Process
  from popen import killall as kill_processes
  from thread import Thread, call_from_main
  from callback import Callback, WeakCallback, Timer, WeakTimer, OneShotTimer, \
!                      WeakOneShotTimer, SocketDispatcher, WeakSocketDispatcher
  
  # get logging object
--- 37,46 ----
  from posixsignals import *
  from posixsignals import register as signal
  from popen import Process
  from popen import killall as kill_processes
  from thread import Thread, call_from_main
  from callback import Callback, WeakCallback, Timer, WeakTimer, OneShotTimer, \
!                      WeakOneShotTimer, SocketDispatcher, 
WeakSocketDispatcher,\
!                      Signal
  
  # get logging object
***************
*** 51,55 ****
  
  
! def _idle_signal_changed(signal, flag, pos):
      if flag == Signal.SIGNAL_CONNECTED and signal.count() == 1:
          notifier.addDispatcher(signal.emit)
--- 51,55 ----
  
  
! def _idle_signal_changed(signal, flag):
      if flag == Signal.SIGNAL_CONNECTED and signal.count() == 1:
          notifier.addDispatcher(signal.emit)

Index: callback.py
===================================================================
RCS file: /cvsroot/freevo/kaa/base/src/notifier/callback.py,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** callback.py 13 Jul 2005 17:56:04 -0000      1.3
--- callback.py 13 Jul 2005 20:16:25 -0000      1.4
***************
*** 33,37 ****
              'WeakOneShotTimer', 'SocketDispatcher', 'WeakSocketDispatcher' ]
  
! import weakref
  
  try:
--- 33,38 ----
              'WeakOneShotTimer', 'SocketDispatcher', 'WeakSocketDispatcher' ]
  
! import _weakref
! import types
  
  try:
***************
*** 70,73 ****
--- 71,123 ----
  
  
+ def weakref_data(data):
+     if type(data) in (str, int, long, types.NoneType):
+         # Naive optimization for common immutable cases.
+         return data
+     elif type(data) == types.MethodType:
+         return WeakCallback(data)
+     elif type(data) in (list, tuple):
+         d = []
+         for item in data:
+             d.append(weakref_data(item))
+         if type(data) == tuple:
+             d = tuple(d)
+         return d
+     elif type(data) == dict:
+         d = {}
+         for key, val in data.items():
+             d[weakref_data(key)] = weakref_data(val)
+         return d
+     elif type(data) != types.FunctionType:
+         try:
+             return _weakref.ref(data)
+         except TypeError:
+             pass
+ 
+     return data
+ 
+ def unweakref_data(data):
+     if type(data) in (str, int, long, types.NoneType):
+         # Naive optimization for common immutable cases.
+         return data
+     elif type(data) == _weakref.ReferenceType:
+         return data()
+     elif type(data) == WeakCallback:
+         return data._get_callback()
+     elif type(data) in (list, tuple):
+         d = []
+         for item in data:
+             d.append(unweakref_data(item))
+         if type(data) == tuple:
+             d = tuple(d)
+         return d
+     elif type(data) == dict:
+         d = {}
+         for key, val in data.items():
+             d[unweakref_data(key)] = unweakref_data(val)
+         return d
+     else:
+         return data
+ 
  
  class Callback(object):
***************
*** 208,214 ****
      def __init__(self, callback, *args, **kwargs):
          super(WeakCallback, self).__init__(callback, *args, **kwargs)
!         if hasattr(callback, "im_self"):
              # For methods
!             self._instance = weakref.ref(callback.im_self, 
self._weakref_destroyed)
              self._callback = callback.im_func.func_name
          else:
--- 258,264 ----
      def __init__(self, callback, *args, **kwargs):
          super(WeakCallback, self).__init__(callback, *args, **kwargs)
!         if type(callback) == types.MethodType:
              # For methods
!             self._instance = _weakref.ref(callback.im_self, 
self._weakref_destroyed)
              self._callback = callback.im_func.func_name
          else:
***************
*** 216,220 ****
              self._instance = None
  
!         # TODO: make weak refs of args/kwargs too.
  
  
--- 266,272 ----
              self._instance = None
  
!         self._args = weakref_data(args)
!         self._kwargs = weakref_data(kwargs)
!         self._weakref_destroyed_user_cb = None
  
  
***************
*** 227,232 ****
  
  
      def _weakref_destroyed(self, object):
!         pass
  
  
--- 279,303 ----
  
  
+     def __call__(self, *args, **kwargs):
+         save_args, save_kwargs = self._args, self._kwargs
+     
+         # Remove weakrefs from user data before invoking the callback.
+         self._args = unweakref_data(self._args)
+         self._kwargs = unweakref_data(self._kwargs)
+ 
+         super(WeakCallback, self).__call__(*args, **kwargs)
+ 
+         self._args, self._kwargs = save_args, save_kwargs
+     
+         return False
+ 
+ 
+     def set_weakref_destroyed_cb(self, callback):
+         self._weakref_destroyed_user_cb = callback
+ 
+ 
      def _weakref_destroyed(self, object):
!         if self._weakref_destroyed_user_cb:
!             return self._weakref_destroyed_user_cb(self, object)
  
  
***************
*** 235,238 ****
--- 306,310 ----
  
      def _weakref_destroyed(self, object):
+         super(WeakNotifierCallback, self)._weakref_destroyed(object)
          self.unregister()
  
***************
*** 248,250 ****
--- 320,440 ----
  
  
+ class Signal(object):
+ 
+     # Parameters for changed callback
+     SIGNAL_CONNECTED = 1
+     SIGNAL_DISCONNECTED = 2
+ 
+     def __init__(self, changed_cb = None):
+         self._callbacks = []
+         self._changed_cb = changed_cb
+         self._items = {}
+ 
+     def __getitem__(self, key):
+         return self._items[key]
+ 
+     def __setitem__(self, key, val):
+         self._items[key] = val
+ 
+     def __contains__(self, key):
+         return key in self._items
+ 
+  
+     def _connect(self, callback, args = (), kwargs = {}, once = False, 
+                  weak = False, pos = -1):
+ 
+         if len(self._callbacks) > 40:
+             # It's a common problem (for me :)) that callbacks get added
+             # inside another callback.  This is a simple sanity check.
+             print "Signal callbacks exceeds 40.  Something's wrong!"
+             print callback, args, self._callbacks[0][0].get_method()
+             raise Exception
+ 
+         if weak:
+             callback = WeakCallback(callback)
+             callback.set_weakref_destroyed_cb(self._weakref_destroyed)
+             args, kwargs = weakref_data(args), weakref_data(kwargs)
+         if pos == -1:
+             pos = len(self._callbacks)
+ 
+         self._callbacks.insert(pos, (callback, args, kwargs, once, weak))
+         if self._changed_cb:
+             self._changed_cb(self, Signal.SIGNAL_CONNECTED)
+         return True
+ 
+ 
+     def connect(self, callback, *args, **kwargs):
+         return self._connect(callback, args, kwargs)
+ 
+     def connect_weak(self, callback, *args, **kwargs):
+         return self._connect(callback, args, kwargs, weak = True)
+ 
+     def connect_once(self, callback, *args, **kwargs):
+         return self._connect(callback, args, kwargs, once = True)
+ 
+     def connect_weak_once(self, callback, *args, **kwargs):
+         return self._connect(callback, args, kwargs, once = True, weak = True)
+ 
+     def connect_first(self, callback, *args, **kwargs):
+         return self._connect(callback, args, kwargs, pos = 0)
+ 
+     def connect_weak_first(self, callback, *args, **kwargs):
+         return self._connect(callback, args, kwargs, weak = True, pos = 0)
+ 
+     def _disconnect(self, callback, args, kwargs):
+         new_callbacks = []
+         for pos, cb_all in zip(range(len(self._callbacks)), 
self._callbacks[:]):
+             cb_callback, cb_args, cb_kwargs, cb_once, cb_weak = cb_all
+             if cb_weak and args != None:
+                 cb_callback = cb_callback._get_callback()
+                 cb_args, cb_kwargs = unweakref_data((cb_args, cb_kwargs))
+ 
+             if (cb_callback == callback and args == None) or \
+                (cb_callback, cb_args, cb_kwargs) == (callback, args, kwargs):
+                 # This matches what we want to disconnect.
+                 continue
+ 
+             new_callbacks.append(cb_all)
+ 
+         if len(new_callbacks) != self._callbacks:
+             self._callbacks = new_callbacks
+             if self._changed_cb:
+                 self._changed_cb(self, Signal.SIGNAL_DISCONNECTED)
+ 
+             return True
+ 
+         return False
+ 
+ 
+     def disconnect(self, callback, *args, **kwargs):
+         return self._disconnect(callback, False, args, kwargs)
+ 
+ 
+     def disconnect_all(self):
+         count = self.count()
+         self._callbacks = []
+         if self._changed_cb and count > 0:
+             self._changed_cb(self, Signal.SIGNAL_DISCONNECTED)
+ 
+     def emit(self, *args, **kwargs):
+         retval = False
+         for cb_callback, cb_args, cb_kwargs, cb_once, cb_weak in 
self._callbacks[:]:
+             if cb_weak:
+                 cb_callback = cb_callback._get_callback()
+                 cb_args, cb_kwargs = unweakref_data((cb_args, cb_kwargs))
+             else:
+                 cb_kwargs = cb_kwargs.copy()
  
+             cb_kwargs.update(kwargs)
+             if cb_callback(*args, **kwargs):
+                 retval = True
+             if cb_once:
+                 self.disconnect(cb_callback, cb_args, cb_kwargs)
+ 
+ 
+     def _weakref_destroyed(self, callback, weakref):
+         self._disconnect(callback, None, None)
+ 
+ 
+     def count(self):
+         return len(self._callbacks)



-------------------------------------------------------
This SF.Net email is sponsored by the 'Do More With Dual!' webinar happening
July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual
core and dual graphics technology at this free one hour event hosted by HP, 
AMD, and NVIDIA.  To register visit http://www.hp.com/go/dualwebinar
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to