[py-svn] r51482 - in py/branch/event/py: . event test2 test2/rsession test2/rsession/testing test2/testing

hpk at codespeak.net hpk at codespeak.net
Thu Feb 14 11:27:39 CET 2008


Author: hpk
Date: Thu Feb 14 11:27:39 2008
New Revision: 51482

Added:
   py/branch/event/py/test2/eventbus.py
      - copied, changed from r51469, py/branch/event/py/event/hub.py
   py/branch/event/py/test2/testing/test_eventbus.py
      - copied, changed from r51469, py/branch/event/py/event/testing/test_hub.py
Removed:
   py/branch/event/py/event/
Modified:
   py/branch/event/py/__init__.py
   py/branch/event/py/test2/config.py
   py/branch/event/py/test2/genitem.py
   py/branch/event/py/test2/rsession/hostmanage.py
   py/branch/event/py/test2/rsession/master.py
   py/branch/event/py/test2/rsession/testing/test_masterslave.py
   py/branch/event/py/test2/rsession/testing/test_rsession.py
   py/branch/event/py/test2/session.py
   py/branch/event/py/test2/testing/suptest.py
Log:
removed py.event global namespace and and usin
a test2-local EventBus for now. 

subscriptions work like this: 

    bus.subscribe(callback) 
    bus.unsubscribe(callback) 

and notifications like this: 
    
    bus.notify(event) 

where event can be any object. 



Modified: py/branch/event/py/__init__.py
==============================================================================
--- py/branch/event/py/__init__.py	(original)
+++ py/branch/event/py/__init__.py	Thu Feb 14 11:27:39 2008
@@ -68,11 +68,6 @@
     'test.collect.Function'  : ('./test/item.py', 'Function'),
     'test2.collect.Function'  : ('./test2/item.py', 'Function'),
 
-    # Event related APIs
-    'event.Hub'              : ('./event/hub.py', 'Hub'), 
-    #'event.hub'              : ('./event/hub.py', 'hub'), 
-    #'event.notify'           : ('./event/hub.py', 'notify'), 
-
     # thread related API (still in early design phase)
     '_thread.WorkerPool'      : ('./thread/pool.py', 'WorkerPool'),
     '_thread.NamedThreadPool' : ('./thread/pool.py', 'NamedThreadPool'),

Modified: py/branch/event/py/test2/config.py
==============================================================================
--- py/branch/event/py/test2/config.py	(original)
+++ py/branch/event/py/test2/config.py	Thu Feb 14 11:27:39 2008
@@ -3,6 +3,7 @@
 import py
 from conftesthandle import Conftest
 from py.__.test2.defaultconftest import adddefaultoptions
+from py.__.test2.eventbus import EventBus
 
 optparse = py.compat.optparse
 
@@ -25,7 +26,7 @@
         return "<CmdOptions %r>" %(self.__dict__,)
 
 class Config(object): 
-    """ central hub for dealing with configuration/initialization data. """ 
+    """ central bus for dealing with configuration/initialization data. """ 
     Option = optparse.Option
 
     def __init__(self): 
@@ -34,7 +35,7 @@
             usage="usage: %prog [options] [query] [filenames of tests]")
         self._conftest = Conftest()
         self._initialized = False
-        self.hub = py.event.Hub()
+        self.bus = EventBus()
 
     def parse(self, args): 
         """ parse cmdline arguments into this config object. 

Copied: py/branch/event/py/test2/eventbus.py (from r51469, py/branch/event/py/event/hub.py)
==============================================================================
--- py/branch/event/py/event/hub.py	(original)
+++ py/branch/event/py/test2/eventbus.py	Thu Feb 14 11:27:39 2008
@@ -1,11 +1,19 @@
 
-class Hub(list):
-    """ General Event Hub """ 
+class EventBus(object): 
+    """ General Event Bus for distributing and processing events. """ 
+    def __init__(self):
+        self._subscribers = []
+    
+    def subscribe(self, callback):
+        """ subscribe given callback to bus events. """ 
+        self._subscribers.append(callback) 
+
+    def unsubscribe(self, callback):
+        """ unsubscribe given callback from bus events. """ 
+        self._subscribers.remove(callback) 
+    
     def notify(self, event):
-        for subscriber in self:
+        for subscriber in self._subscribers:
             subscriber(event) 
 
 
-# XXX do we want a global hub? 
-#hub = Hub()
-#notify = hub.notify 

Modified: py/branch/event/py/test2/genitem.py
==============================================================================
--- py/branch/event/py/test2/genitem.py	(original)
+++ py/branch/event/py/test2/genitem.py	Thu Feb 14 11:27:39 2008
@@ -6,18 +6,18 @@
 def genitems(config, colitems, keywordexpr=None, 
              stopitems=(py.test2.collect.Item,)):
     """ yield Items from iterating over the given colitems. """
-    hub = config.hub
+    bus = config.bus
     while colitems: 
         next = colitems.pop(0)
         if isinstance(next, stopitems):
             if next._skipbykeyword(keywordexpr):
-                hub.notify(repevent.DeselectedTest(next, keywordexpr))
+                bus.notify(repevent.DeselectedTest(next, keywordexpr))
                 if config.option.keyword_oneshot:
                     keywordexpr = None
             else:
                 yield next 
         else:
-            hub.notify(repevent.CollectionStart(next))
+            bus.notify(repevent.CollectionStart(next))
             excinfo = None
             try:
                 cols = [next.join(x) for x in next.run()]
@@ -27,4 +27,4 @@
                 raise
             except:
                 excinfo = py.code.ExceptionInfo()
-            hub.notify(repevent.CollectionFinish(next, excinfo))
+            bus.notify(repevent.CollectionFinish(next, excinfo))

Modified: py/branch/event/py/test2/rsession/hostmanage.py
==============================================================================
--- py/branch/event/py/test2/rsession/hostmanage.py	(original)
+++ py/branch/event/py/test2/rsession/hostmanage.py	Thu Feb 14 11:27:39 2008
@@ -133,7 +133,7 @@
         python = self.config.getvalue("dist_remotepython")
         for host in self.hosts:
             host.initgateway(python=python)
-            self.config.hub.notify(repevent.HostGatewayReady(host, self.roots))
+            self.config.bus.notify(repevent.HostGatewayReady(host, self.roots))
             host.gw.host = host
 
     def init_rsync(self):
@@ -150,7 +150,7 @@
             for host in self.hosts:
                 rsync.add_target_host(host, destrelpath)
             rsync.send(raises=False)
-        self.config.hub.notify(repevent.RsyncFinished())
+        self.config.bus.notify(repevent.RsyncFinished())
 
     def setup_hosts(self):
         self.init_rsync()
@@ -163,7 +163,7 @@
         def hostdown(event):
             if isinstance(event, repevent.HostDown):
                 queue.put(event) 
-        self.config.hub.append(hostdown)
+        self.config.bus.subscribe(hostdown)
 
         pending_hosts = []
         for host in self.hosts: 

Modified: py/branch/event/py/test2/rsession/master.py
==============================================================================
--- py/branch/event/py/test2/rsession/master.py	(original)
+++ py/branch/event/py/test2/rsession/master.py	Thu Feb 14 11:27:39 2008
@@ -8,7 +8,7 @@
     def __init__(self, host, config):
         self.host = host 
         self.config = config 
-        self.notify = config.hub.notify 
+        self.notify = config.bus.notify 
         self.channel = setup_slave(host, config)  
         self.channel.setcallback(self._callback)
         self.pending = []

Modified: py/branch/event/py/test2/rsession/testing/test_masterslave.py
==============================================================================
--- py/branch/event/py/test2/rsession/testing/test_masterslave.py	(original)
+++ py/branch/event/py/test2/rsession/testing/test_masterslave.py	Thu Feb 14 11:27:39 2008
@@ -12,7 +12,7 @@
         def append(event):
             if isinstance(event, filterevent):
                 queue.put(event) 
-        self.config.hub.append(append) 
+        self.config.bus.subscribe(append) 
         return queue 
 
     def test_node_down(self):

Modified: py/branch/event/py/test2/rsession/testing/test_rsession.py
==============================================================================
--- py/branch/event/py/test2/rsession/testing/test_rsession.py	(original)
+++ py/branch/event/py/test2/rsession/testing/test_rsession.py	Thu Feb 14 11:27:39 2008
@@ -89,7 +89,7 @@
         hosts = [HostInfo('localhost:%s' % self.dest)]
 
         queue = py.std.Queue.Queue()
-        self.config.hub.append(queue.put) 
+        self.config.bus.subscribe(queue.put) 
         hm = HostManager(self.config, hosts=hosts)
         hm.setup_hosts()
         

Modified: py/branch/event/py/test2/session.py
==============================================================================
--- py/branch/event/py/test2/session.py	(original)
+++ py/branch/event/py/test2/session.py	Thu Feb 14 11:27:39 2008
@@ -63,14 +63,14 @@
                 failurelist.append(event) 
                 if self.config.option.exitfirst: 
                     self.shouldstop = True 
-        self.config.hub.append(processfailures) 
+        self.config.bus.subscribe(processfailures) 
         return failurelist 
 
     def main(self):
         """ main loop for running tests. """
         self.shouldstop = False 
         self.setup()
-        self.config.hub.notify(repevent.SessionStart(self))
+        self.config.bus.notify(repevent.SessionStart(self))
         try:
             for item in self.collect(): 
                 if self.shouldstop: 
@@ -79,7 +79,7 @@
                     self.runtest(item)
         finally:
             failures = self.teardown()
-        self.config.hub.notify(repevent.SessionFinish(self))
+        self.config.bus.notify(repevent.SessionFinish(self))
         return failures 
 
     def runtest(self, item):
@@ -95,8 +95,8 @@
                 raise ValueError
             except ValueError: 
                 excinfo = py.code.ExceptionInfo()
-        self.config.hub.notify(repevent.ItemFinish(item, excinfo)) 
-        self.config.hub.notify(testrep) 
+        self.config.bus.notify(repevent.ItemFinish(item, excinfo)) 
+        self.config.bus.notify(testrep) 
 
 class Exit(Exception):
     """ for immediate program exits without tracebacks and reporter/summary. """

Modified: py/branch/event/py/test2/testing/suptest.py
==============================================================================
--- py/branch/event/py/test2/testing/suptest.py	(original)
+++ py/branch/event/py/test2/testing/suptest.py	Thu Feb 14 11:27:39 2008
@@ -19,7 +19,7 @@
     def app(ev):
         print ev
         l.append(ev)
-    config.hub.append(app)
+    config.bus.subscribe(app)
     return l 
 
 def initsorter_from_cmdline(args=None):
@@ -55,7 +55,7 @@
             for cls in py.std.inspect.getmro(event.__class__):
                 if cls is not object: 
                     d.setdefault(cls, []).append(event) 
-        config.hub.append(app)
+        config.bus.subscribe(app)
 
     def get(self, cls):
         return self.cls2events.get(cls, [])

Copied: py/branch/event/py/test2/testing/test_eventbus.py (from r51469, py/branch/event/py/event/testing/test_hub.py)
==============================================================================
--- py/branch/event/py/event/testing/test_hub.py	(original)
+++ py/branch/event/py/test2/testing/test_eventbus.py	Thu Feb 14 11:27:39 2008
@@ -1,34 +1,34 @@
 import py
+from py.__.test2.eventbus import EventBus
 
-class TestHub:
-
+class TestEventBus:
     def test_simple(self):
-        hub = py.event.Hub()
+        bus = EventBus()
         l = []
-        hub.append(l.append) 
-        hub.notify(1)
-        hub.notify(2)
-        hub.notify(3)
+        bus.subscribe(l.append) 
+        bus.notify(1)
+        bus.notify(2)
+        bus.notify(3)
         assert l == [1,2,3]
 
     def test_multi_sub(self):
-        hub = py.event.Hub()
+        bus = EventBus()
         l1 = []
         l2 = []
-        hub.append(l1.append) 
-        hub.append(l2.append) 
-        hub.notify(1)
-        hub.notify(2)
-        hub.notify(3)
+        bus.subscribe(l1.append) 
+        bus.subscribe(l2.append) 
+        bus.notify(1)
+        bus.notify(2)
+        bus.notify(3)
         assert l1 == [1,2,3]
         assert l2 == [1,2,3]
 
     def test_remove(self):
-        hub = py.event.Hub()
+        bus = EventBus()
         l = []
-        hub.append(l.append) 
-        hub.notify(1)
-        hub.remove(l.append) 
-        hub.notify(2)
+        bus.subscribe(l.append) 
+        bus.notify(1)
+        bus.unsubscribe(l.append) 
+        bus.notify(2)
         assert l == [1]
 


More information about the py-svn mailing list