[ZODB-Dev] minor cleanup for ZEO/zrpc/trigger.py

Christian Reis kiko@async.com.br
Sat, 5 Oct 2002 11:01:08 -0300


--mP3DRpeJDSE+ciuQ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline


Took care of an XXX in trigger.py. Do we really need "poll" and
"loopback" in the repr()? I've tested this lightly, should work.

Take care,
--
Christian Reis, Senior Engineer, Async Open Source, Brazil.
http://async.com.br/~kiko/ | [+55 16] 261 2331 | NMFL

--mP3DRpeJDSE+ciuQ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=zrpc-trigger-diff

Index: trigger.py
===================================================================
RCS file: /cvs-repository/ZODB3/ZEO/zrpc/trigger.py,v
retrieving revision 1.8
diff -u -r1.8 trigger.py
--- trigger.py	2 Oct 2002 18:37:17 -0000	1.8
+++ trigger.py	5 Oct 2002 13:58:38 -0000
@@ -18,9 +18,60 @@
 import string
 import thread
 
+class basetrigger(asyncore.file_dispatcher):
+
+    """base trigger class that is cross-platform-safe. Subclasses must
+define a constructor and a string attribute 'triggertype'."""
+
+    def __init__(self):
+        raise NotImplementedError
+
+    def __repr__(self):
+        if not self.triggertype:
+            raise NotImplementedError
+        return '<select-trigger (%s) at %x>' % (self.triggertype, id(self))
+
+    def _trigger_send(self):
+        raise NotImplementedError
+
+    def readable(self):
+        return 1
+
+    def writable(self):
+        return 0
+
+    def handle_connect(self):
+        pass
+
+    def handle_read(self):
+        self.recv(8192)
+        self.lock.acquire()
+        try:
+            for thunk in self.thunks:
+                try:
+                    thunk()
+                except:
+                    nil, t, v, tbinfo = asyncore.compact_traceback()
+                    print ('exception in trigger thunk:'
+                           ' (%s:%s %s)' % (t, v, tbinfo))
+            self.thunks = []
+        finally:
+            self.lock.release()
+
+    def pull_trigger(self, thunk=None):
+        if thunk:
+            self.lock.acquire()
+            try:
+                self.thunks.append(thunk)
+            finally:
+                self.lock.release()
+        self._trigger_send()
+
 if os.name == 'posix':
 
-    class trigger(asyncore.file_dispatcher):
+    class trigger(basetrigger):
+
+        triggertype = "poll" # just for repr
 
         "Wake up a call to select() running in the main thread"
 
@@ -73,56 +124,21 @@
                 for fd in self._fds:
                     os.close(fd)
 
-        def __repr__(self):
-            return '<select-trigger (pipe) at %x>' % id(self)
-
-        def readable(self):
-            return 1
-
-        def writable(self):
-            return 0
-
-        def handle_connect(self):
-            pass
-
-        def pull_trigger(self, thunk=None):
-            if thunk:
-                self.lock.acquire()
-                try:
-                    self.thunks.append(thunk)
-                finally:
-                    self.lock.release()
+        def _trigger_send(self):
             os.write(self.trigger, 'x')
 
-        def handle_read(self):
-            self.recv(8192)
-            self.lock.acquire()
-            try:
-                for thunk in self.thunks:
-                    try:
-                        thunk()
-                    except:
-                        nil, t, v, tbinfo = asyncore.compact_traceback()
-                        print ('exception in trigger thunk:'
-                               ' (%s:%s %s)' % (t, v, tbinfo))
-                self.thunks = []
-            finally:
-                self.lock.release()
-
 else:
 
-    # XXX Should define a base class that has the common methods and
-    # then put the platform-specific in a subclass named trigger.
-
     # win32-safe version
 
     HOST = '127.0.0.1'
     MINPORT = 19950
     NPORTS = 50
 
-    class trigger(asyncore.dispatcher):
+    class trigger(basetrigger):
 
         portoffset = 0
+        triggertype = "loopback" # just for repr
 
         def __init__(self):
             a = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -161,38 +177,6 @@
             self.thunks = []
             self._trigger_connected = 0
 
-        def __repr__(self):
-            return '<select-trigger (loopback) at %x>' % id(self)
-
-        def readable(self):
-            return 1
-
-        def writable(self):
-            return 0
-
-        def handle_connect(self):
-            pass
-
-        def pull_trigger(self, thunk=None):
-            if thunk:
-                self.lock.acquire()
-                try:
-                    self.thunks.append(thunk)
-                finally:
-                    self.lock.release()
+        def _trigger_send(self):
             self.trigger.send('x')
 
-        def handle_read(self):
-            self.recv(8192)
-            self.lock.acquire()
-            try:
-                for thunk in self.thunks:
-                    try:
-                        thunk()
-                    except:
-                        nil, t, v, tbinfo = asyncore.compact_traceback()
-                        print ('exception in trigger thunk:'
-                               ' (%s:%s %s)' % (t, v, tbinfo))
-                self.thunks = []
-            finally:
-                self.lock.release()

--mP3DRpeJDSE+ciuQ--