[GRASS-SVN] r55323 - grass/trunk/lib/python/pydispatch
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Mar 11 13:20:19 PDT 2013
Author: wenzeslaus
Date: 2013-03-11 13:20:19 -0700 (Mon, 11 Mar 2013)
New Revision: 55323
Added:
grass/trunk/lib/python/pydispatch/signal.py
Modified:
grass/trunk/lib/python/pydispatch/Makefile
grass/trunk/lib/python/pydispatch/dispatcher.py
grass/trunk/lib/python/pydispatch/pydispatchlib.dox
grass/trunk/lib/python/pydispatch/robust.py
Log:
libpython/pydispach: simple API (Signal) and small fix for unloading
Modified: grass/trunk/lib/python/pydispatch/Makefile
===================================================================
--- grass/trunk/lib/python/pydispatch/Makefile 2013-03-11 20:04:56 UTC (rev 55322)
+++ grass/trunk/lib/python/pydispatch/Makefile 2013-03-11 20:20:19 UTC (rev 55323)
@@ -8,7 +8,7 @@
GDIR = $(PYDIR)/grass
DSTDIR = $(GDIR)/pydispatch
-MODULES = dispatcher errors robustapply robust saferef signals
+MODULES = dispatcher errors robustapply robust saferef signal
PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__)
Modified: grass/trunk/lib/python/pydispatch/dispatcher.py
===================================================================
--- grass/trunk/lib/python/pydispatch/dispatcher.py 2013-03-11 20:04:56 UTC (rev 55322)
+++ grass/trunk/lib/python/pydispatch/dispatcher.py 2013-03-11 20:20:19 UTC (rev 55323)
@@ -27,11 +27,11 @@
"""
from __future__ import generators
import weakref
-from pydispatch import saferef, robustapply, errors
+from grass.pydispatch import saferef, robustapply, errors
__author__ = "Patrick K. O'Brien <pobrien at orbtech.com>"
-__cvsid__ = "$Id: dispatcher.py,v 1.1 2010/03/30 15:45:55 mcfletch Exp $"
-__version__ = "$Revision: 1.1 $"[11:-2]
+__cvsid__ = "Id: dispatcher.py,v 1.1 2010/03/30 15:45:55 mcfletch Exp"
+__version__ = "Revision: 1.1"
class _Parameter:
"""Used to represent default parameter values."""
@@ -367,7 +367,7 @@
def _removeReceiver(receiver):
"""Remove receiver from connections."""
- if not sendersBack:
+ if not sendersBack or not connections:
# During module cleanup the mapping will be replaced with None
return False
backKey = id(receiver)
Modified: grass/trunk/lib/python/pydispatch/pydispatchlib.dox
===================================================================
--- grass/trunk/lib/python/pydispatch/pydispatchlib.dox 2013-03-11 20:04:56 UTC (rev 55322)
+++ grass/trunk/lib/python/pydispatch/pydispatchlib.dox 2013-03-11 20:20:19 UTC (rev 55323)
@@ -4,10 +4,14 @@
\section pydispachIntro Introduction
-TODO
+Files dispatcher.py, errors.py, robustapply.py, robust.py and saferef.py are
+part of the original PyDispacher.
+File signal.py is the object-based easy to use interface created for GRASS.
\section pydispachAuthors Authors
Patrick K. O'Brien, Mike C. Fletcher and Contributors (original authors, see pydispatch/license.txt and pydispatch/PKG-INFO for details)
+Vaclav Petras (signal.py)
+Anna Kratochilova (signal.py)
*/
Modified: grass/trunk/lib/python/pydispatch/robust.py
===================================================================
--- grass/trunk/lib/python/pydispatch/robust.py 2013-03-11 20:04:56 UTC (rev 55322)
+++ grass/trunk/lib/python/pydispatch/robust.py 2013-03-11 20:20:19 UTC (rev 55323)
@@ -1,6 +1,6 @@
"""Module implementing error-catching version of send (sendRobust)"""
-from pydispatch.dispatcher import Any, Anonymous, liveReceivers, getAllReceivers
-from pydispatch.robustapply import robustApply
+from grass.pydispatch.dispatcher import Any, Anonymous, liveReceivers, getAllReceivers
+from grass.pydispatch.robustapply import robustApply
def sendRobust(
signal=Any,
Added: grass/trunk/lib/python/pydispatch/signal.py
===================================================================
--- grass/trunk/lib/python/pydispatch/signal.py (rev 0)
+++ grass/trunk/lib/python/pydispatch/signal.py 2013-03-11 20:20:19 UTC (rev 55323)
@@ -0,0 +1,74 @@
+# -*- coding: utf-8 -*-
+"""
+Created on Mon Mar 11 18:39:13 2013
+
+ at author Vaclav Petras <wenzeslaus gmail.com>
+"""
+
+
+from grass.pydispatch import dispatcher
+
+
+def _islambda(function):
+ """Tests if object is a lambda function.
+
+ Should work on the most of Python implementations where name of lambda
+ function is not unique.
+ """
+ return isinstance(function, type(lambda: None)) and function.__name__== (lambda: None).__name__
+
+
+class Signal(object):
+ def __init__(self, name):
+ self._name = name
+
+ def connect(self, handler, weak=None):
+ if weak is None:
+ if _islambda(handler):
+ weak = False
+ else:
+ weak = True
+ dispatcher.connect(receiver=handler, signal=self, weak=weak)
+
+ def disconnect(self, handler):
+ dispatcher.disconnect(receiver=handler, signal=self, weak=None)
+
+ def emit(self, *args, **kwargs):
+ dispatcher.send(signal=self, *args, **kwargs)
+
+ def __call__(self, *arguments, **named):
+ if 'signal' in named:
+ del named['signal']
+ self.emit(*arguments, **named)
+
+
+if __name__ == '__main__':
+ def handler1():
+ print "handler1"
+ def handler2(text):
+ print "handler2: %s" % text
+ class A(object):
+ def showText(self, text):
+ print "showing text:", text
+ def showMessage(self):
+ print "showing message"
+
+ def test():
+ import sys
+ signal1 = Signal('signal1')
+ signal2 = Signal('signal2')
+ signal3 = Signal('signal3')
+ signal1.connect(handler1)
+ signal2.connect(handler2)
+ signal2.connect(lambda text: sys.stdout.write('lambda handler 1: %s\n' % text))
+ signal2.connect(signal3)
+ signal3.connect(handler2)
+
+ a = A()
+ signal2.connect(a.showText)
+ signal2.connect(a.showMessage)
+
+ signal1.emit()
+ signal2.emit(text="Hello")
+
+ test()
\ No newline at end of file
More information about the grass-commit
mailing list