Author: dmeyer
Date: Wed Feb 13 14:08:08 2008
New Revision: 3073

Log:
move Signal(s) from callback.py to a new file signals.py

Added:
   trunk/base/src/notifier/signals.py
Modified:
   trunk/base/src/notifier/__init__.py
   trunk/base/src/notifier/async.py
   trunk/base/src/notifier/callback.py
   trunk/base/src/notifier/jobserver.py
   trunk/base/src/notifier/main.py
   trunk/base/src/notifier/nf_wrapper.py
   trunk/base/src/notifier/popen.py
   trunk/base/src/notifier/sockets.py
   trunk/base/src/notifier/thread.py
   trunk/base/src/notifier/yieldfunc.py

Modified: trunk/base/src/notifier/__init__.py
==============================================================================
--- trunk/base/src/notifier/__init__.py (original)
+++ trunk/base/src/notifier/__init__.py Wed Feb 13 14:08:08 2008
@@ -31,7 +31,8 @@
 # -----------------------------------------------------------------------------
 
 from popen import Process
-from callback import Callback, WeakCallback, Signal, Signals
+from callback import Callback, WeakCallback
+from signals import Signal, Signals
 from thread import MainThreadCallback, ThreadCallback, is_mainthread
 from timer import Timer, WeakTimer, OneShotTimer, WeakOneShotTimer, AtTimer, 
OneShotAtTimer
 from sockets import IOMonitor, WeakIOMonitor, Socket, IO_READ, IO_WRITE

Modified: trunk/base/src/notifier/async.py
==============================================================================
--- trunk/base/src/notifier/async.py    (original)
+++ trunk/base/src/notifier/async.py    Wed Feb 13 14:08:08 2008
@@ -39,7 +39,8 @@
 import threading
 
 # kaa.notifier imports
-from callback import Signal, Callback
+from callback import Callback
+from signals import Signal
 
 # get logging object
 log = logging.getLogger('notifier.async')

Modified: trunk/base/src/notifier/callback.py
==============================================================================
--- trunk/base/src/notifier/callback.py (original)
+++ trunk/base/src/notifier/callback.py Wed Feb 13 14:08:08 2008
@@ -30,7 +30,7 @@
 #
 # -----------------------------------------------------------------------------
 
-__all__ = [ 'Callback', 'WeakCallback', 'Signal', 'Signals' ]
+__all__ = [ 'Callback', 'WeakCallback' ]
 
 # Python imports
 import _weakref
@@ -252,222 +252,6 @@
 
 
 
-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._deferred_args = []
-
-
-    def __iter__(self):
-        for cb in self._callbacks:
-            yield cb
-
-
-    def __len__(self):
-        return len(self._callbacks)
-
-
-    def __nonzero__(self):
-        return True
-
-
-    def __contains__(self, key):
-        if not callable(key):
-            return False
-
-        for cb in self._callbacks:
-            if cb == key:
-                return True
-
-        return False
-
-    def _connect(self, callback, args = (), kwargs = {}, once = False, weak = 
False, pos = -1):
-        """
-        Connects a new callback to the signal.  args and kwargs will be bound
-        to the callback and merged with the args and kwargs passed during 
-        emit().  If weak is True, a WeakCallback will be created.  If once is
-        True, the callback will be automatically disconnected after the next
-        emit().
-
-        This method returns the Callback (or WeakCallback) object created.
-        """
-
-        assert(callable(callback))
-
-        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.
-            log.error("Signal callbacks exceeds 40.  Something's wrong!")
-            log.error("%s: %s", callback, args)
-            raise Exception("Signal callbacks exceeds 40")
-
-        if weak:
-            callback = WeakCallback(callback, *args, **kwargs)
-            # We create a callback for weakref destruction for both the
-            # signal callback as well as signal data.
-            destroy_cb = Callback(self._weakref_destroyed, callback)
-            callback.set_weakref_destroyed_cb(destroy_cb)
-        else:
-            callback = Callback(callback, *args, **kwargs)
-
-        callback._signal_once = once
-
-        if pos == -1:
-            pos = len(self._callbacks)
-
-        self._callbacks.insert(pos, callback)
-        if self._changed_cb:
-            self._changed_cb(self, Signal.SIGNAL_CONNECTED)
-
-        if self._deferred_args:
-            for args, kwargs in self._deferred_args:
-                self.emit(*args, **kwargs)
-            del self._deferred_args[:]
-
-        return callback
-
-
-    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):
-        assert(callable(callback))
-        new_callbacks = []
-        for cb in self._callbacks[:]:
-            if cb == callback and (len(args) == len(kwargs) == 0 or (args, 
kwargs) == cb.get_user_args()):
-                # This matches what we want to disconnect.
-                continue
-            new_callbacks.append(cb)
-
-        if len(new_callbacks) != len(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, 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):
-        """
-        Emits the signal, passing the args and kwargs to each signal handler.
-        The default return value is True, but if any of the signal handlers
-        return False, this method will return False.
-        """
-        if len(self._callbacks) == 0:
-            return True
-
-        retval = True
-        for cb in self._callbacks[:]:
-            if cb._signal_once:
-                self.disconnect(cb)
-
-            try:
-                if cb(*args, **kwargs) == False:
-                    retval = False
-            except (KeyboardInterrupt, SystemExit):
-                raise SystemExit
-            except Exception, e:
-                log.exception('signal.emit')
-        return retval
-
-
-    def emit_deferred(self, *args, **kwargs):
-        """
-        Queues the emission until after the next callback is connected.  This
-        allows a signal to be 'primed' by its creator, and the handler that
-        subsequently connects to it will be called with the given arguments.
-        """
-        self._deferred_args.append((args, kwargs))
-
-
-    def emit_when_handled(self, *args, **kwargs):
-        """
-        Emits the signal if there are callbacks connected, or defer it until
-        the first callback is connected.
-        """
-        if self.count():
-            return self.emit(*args, **kwargs)
-        else:
-            self.emit_deferred(*args, **kwargs)
-
-
-    def _weakref_destroyed(self, weakref, callback):
-        if _python_shutting_down == False:
-            self._disconnect(callback, (), {})
-
-    def count(self):
-        return len(self._callbacks)
-
-
-class Signals(dict):
-    """
-    Dict of Signal object.
-    """
-    def __init__(self, *signals):
-        dict.__init__(self)
-        for s in signals:
-            if isinstance(s, dict):
-                # parameter is a dict/Signals object
-                self.update(s)
-            elif isinstance(s, str):
-                # parameter is a string
-                self[s] = Signal()
-            else:
-                # parameter is something else, bad
-                raise AttributeError('signal key must be string')
-
-            
-    def __getattr__(self, attr):
-        """
-        Get attribute function from Signal().
-        """
-        if attr.startswith('_') or not hasattr(Signal, attr):
-            return dict.__getattr__(self, attr)
-        callback = Callback(self._callattr, attr)
-        callback.set_user_args_first(True)
-        return callback
-
-    
-    def _callattr(self, attr, signal, *args, **kwargs):
-        """
-        Call attribute function from Signal().
-        """
-        return getattr(self[signal], attr)(*args, **kwargs)
-
-
 def _shutdown_weakref_destroyed():
     global _python_shutting_down
     _python_shutting_down = True

Modified: trunk/base/src/notifier/jobserver.py
==============================================================================
--- trunk/base/src/notifier/jobserver.py        (original)
+++ trunk/base/src/notifier/jobserver.py        Wed Feb 13 14:08:08 2008
@@ -38,7 +38,8 @@
 import sys
 
 # kaa notifier imports
-from callback import Signal, Callback
+from callback import Callback
+from signals import Signal
 import thread
 
 # internal list of named threads

Modified: trunk/base/src/notifier/main.py
==============================================================================
--- trunk/base/src/notifier/main.py     (original)
+++ trunk/base/src/notifier/main.py     Wed Feb 13 14:08:08 2008
@@ -40,7 +40,7 @@
 import atexit
 
 import nf_wrapper as notifier
-from callback import Signal
+from signals import Signal
 from popen import proclist as _proclist
 from thread import is_mainthread, wakeup, set_as_mainthread
 from jobserver import killall as kill_jobserver

Modified: trunk/base/src/notifier/nf_wrapper.py
==============================================================================
--- trunk/base/src/notifier/nf_wrapper.py       (original)
+++ trunk/base/src/notifier/nf_wrapper.py       Wed Feb 13 14:08:08 2008
@@ -35,7 +35,8 @@
 import atexit
 
 # notifier import
-from callback import Callback, WeakCallback, Signal
+from callback import Callback, WeakCallback
+from signals import Signal
 
 # get logging object
 log = logging.getLogger('notifier')

Modified: trunk/base/src/notifier/popen.py
==============================================================================
--- trunk/base/src/notifier/popen.py    (original)
+++ trunk/base/src/notifier/popen.py    Wed Feb 13 14:08:08 2008
@@ -56,7 +56,8 @@
 
 # notifier imports
 import nf_wrapper as notifier
-from callback import Signal, Callback
+from callback import Callback
+from signals import Signal
 from thread import MainThreadCallback, is_mainthread
 from async import InProgress
 from sockets import IOMonitor, IO_WRITE

Added: trunk/base/src/notifier/signals.py
==============================================================================
--- (empty file)
+++ trunk/base/src/notifier/signals.py  Wed Feb 13 14:08:08 2008
@@ -0,0 +1,275 @@
+# -*- coding: iso-8859-1 -*-
+# -----------------------------------------------------------------------------
+# signals.py - Signal object
+# -----------------------------------------------------------------------------
+# $Id: callback.py 3068 2008-02-13 18:34:13Z dmeyer $
+#
+# -----------------------------------------------------------------------------
+# kaa.notifier - Mainloop and callbacks
+# Copyright (C) 2005-2008 Dirk Meyer, Jason Tackaberry, et al.
+#
+# First Version: Dirk Meyer <[EMAIL PROTECTED]>
+# Maintainer:    Dirk Meyer <[EMAIL PROTECTED]>
+#                Jason Tackaberry <[EMAIL PROTECTED]>
+#
+# Please see the file AUTHORS for a complete list of authors.
+#
+# This library is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version
+# 2.1 as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 USA
+#
+# -----------------------------------------------------------------------------
+
+__all__ = [ 'Signal', 'Signals' ]
+
+# Python imports
+import logging
+import atexit
+
+# callbacks from kaa.notifier
+from callback import Callback, WeakCallback
+
+# get logging object
+log = logging.getLogger('notifier')
+
+# Variable that is set to True (via atexit callback) when python interpreter
+# is in the process of shutting down.  If we're interested if the interpreter
+# is shutting down, we don't want to test that this variable is True, but
+# rather that it is not False, because as it is prefixed with an underscore,
+# the interpreter might already have deleted this variable in which case it
+# is None.
+_python_shutting_down = False
+
+
+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._deferred_args = []
+
+
+    def __iter__(self):
+        for cb in self._callbacks:
+            yield cb
+
+
+    def __len__(self):
+        return len(self._callbacks)
+
+
+    def __nonzero__(self):
+        return True
+
+
+    def __contains__(self, key):
+        if not callable(key):
+            return False
+
+        for cb in self._callbacks:
+            if cb == key:
+                return True
+
+        return False
+
+    def _connect(self, callback, args = (), kwargs = {}, once = False, weak = 
False, pos = -1):
+        """
+        Connects a new callback to the signal.  args and kwargs will be bound
+        to the callback and merged with the args and kwargs passed during 
+        emit().  If weak is True, a WeakCallback will be created.  If once is
+        True, the callback will be automatically disconnected after the next
+        emit().
+
+        This method returns the Callback (or WeakCallback) object created.
+        """
+
+        assert(callable(callback))
+
+        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.
+            log.error("Signal callbacks exceeds 40.  Something's wrong!")
+            log.error("%s: %s", callback, args)
+            raise Exception("Signal callbacks exceeds 40")
+
+        if weak:
+            callback = WeakCallback(callback, *args, **kwargs)
+            # We create a callback for weakref destruction for both the
+            # signal callback as well as signal data.
+            destroy_cb = Callback(self._weakref_destroyed, callback)
+            callback.set_weakref_destroyed_cb(destroy_cb)
+        else:
+            callback = Callback(callback, *args, **kwargs)
+
+        callback._signal_once = once
+
+        if pos == -1:
+            pos = len(self._callbacks)
+
+        self._callbacks.insert(pos, callback)
+        if self._changed_cb:
+            self._changed_cb(self, Signal.SIGNAL_CONNECTED)
+
+        if self._deferred_args:
+            for args, kwargs in self._deferred_args:
+                self.emit(*args, **kwargs)
+            del self._deferred_args[:]
+
+        return callback
+
+
+    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):
+        assert(callable(callback))
+        new_callbacks = []
+        for cb in self._callbacks[:]:
+            if cb == callback and (len(args) == len(kwargs) == 0 or (args, 
kwargs) == cb.get_user_args()):
+                # This matches what we want to disconnect.
+                continue
+            new_callbacks.append(cb)
+
+        if len(new_callbacks) != len(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, 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):
+        """
+        Emits the signal, passing the args and kwargs to each signal handler.
+        The default return value is True, but if any of the signal handlers
+        return False, this method will return False.
+        """
+        if len(self._callbacks) == 0:
+            return True
+
+        retval = True
+        for cb in self._callbacks[:]:
+            if cb._signal_once:
+                self.disconnect(cb)
+
+            try:
+                if cb(*args, **kwargs) == False:
+                    retval = False
+            except (KeyboardInterrupt, SystemExit):
+                raise SystemExit
+            except Exception, e:
+                log.exception('signal.emit')
+        return retval
+
+
+    def emit_deferred(self, *args, **kwargs):
+        """
+        Queues the emission until after the next callback is connected.  This
+        allows a signal to be 'primed' by its creator, and the handler that
+        subsequently connects to it will be called with the given arguments.
+        """
+        self._deferred_args.append((args, kwargs))
+
+
+    def emit_when_handled(self, *args, **kwargs):
+        """
+        Emits the signal if there are callbacks connected, or defer it until
+        the first callback is connected.
+        """
+        if self.count():
+            return self.emit(*args, **kwargs)
+        else:
+            self.emit_deferred(*args, **kwargs)
+
+
+    def _weakref_destroyed(self, weakref, callback):
+        if _python_shutting_down == False:
+            self._disconnect(callback, (), {})
+
+    def count(self):
+        return len(self._callbacks)
+
+
+class Signals(dict):
+    """
+    Dict of Signal object.
+    """
+    def __init__(self, *signals):
+        dict.__init__(self)
+        for s in signals:
+            if isinstance(s, dict):
+                # parameter is a dict/Signals object
+                self.update(s)
+            elif isinstance(s, str):
+                # parameter is a string
+                self[s] = Signal()
+            else:
+                # parameter is something else, bad
+                raise AttributeError('signal key must be string')
+
+            
+    def __getattr__(self, attr):
+        """
+        Get attribute function from Signal().
+        """
+        if attr.startswith('_') or not hasattr(Signal, attr):
+            return dict.__getattr__(self, attr)
+        callback = Callback(self._callattr, attr)
+        callback.set_user_args_first(True)
+        return callback
+
+    
+    def _callattr(self, attr, signal, *args, **kwargs):
+        """
+        Call attribute function from Signal().
+        """
+        return getattr(self[signal], attr)(*args, **kwargs)
+
+
+
+def _shutdown_weakref_destroyed():
+    global _python_shutting_down
+    _python_shutting_down = True
+
+atexit.register(_shutdown_weakref_destroyed)

Modified: trunk/base/src/notifier/sockets.py
==============================================================================
--- trunk/base/src/notifier/sockets.py  (original)
+++ trunk/base/src/notifier/sockets.py  Wed Feb 13 14:08:08 2008
@@ -38,7 +38,8 @@
 
 import nf_wrapper as notifier
 from decorators import threaded
-from callback import Callback, Signal
+from callback import Callback
+from signals import Signal
 from thread import MainThreadCallback, ThreadCallback, is_mainthread
 
 # get logging object

Modified: trunk/base/src/notifier/thread.py
==============================================================================
--- trunk/base/src/notifier/thread.py   (original)
+++ trunk/base/src/notifier/thread.py   Wed Feb 13 14:08:08 2008
@@ -60,7 +60,8 @@
 
 # notifier imports
 import nf_wrapper as notifier
-from callback import Callback, Signal
+from callback import Callback
+from signals import Signal
 from async import InProgress
 
 # get logging object

Modified: trunk/base/src/notifier/yieldfunc.py
==============================================================================
--- trunk/base/src/notifier/yieldfunc.py        (original)
+++ trunk/base/src/notifier/yieldfunc.py        Wed Feb 13 14:08:08 2008
@@ -62,7 +62,7 @@
 import sys
 import logging
 
-from callback import Signal
+from signals import Signal
 from timer import Timer
 from async import InProgress
 

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Freevo-cvslog mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/freevo-cvslog

Reply via email to