Author: cito Date: Sun Jan 6 14:55:43 2013 New Revision: 506 Log: Rename pgnotify to WhenNotified. Old name gives deprecation warning. Add helper method for sending notifications.
Modified: trunk/module/TEST_PyGreSQL_classic.py trunk/module/pg.py Modified: trunk/module/TEST_PyGreSQL_classic.py ============================================================================== --- trunk/module/TEST_PyGreSQL_classic.py Sun Jan 6 12:44:25 2013 (r505) +++ trunk/module/TEST_PyGreSQL_classic.py Sun Jan 6 14:55:43 2013 (r506) @@ -235,10 +235,10 @@ self.notify_timeout = True def test_notify(self): - for test_method in False, True: + for as_method in False, True: db = opendb() # Get function under test, can be standalone or DB method. - fut = db.pgnotify if test_method else partial(pgnotify, db) + fut = db.when_notified if as_method else partial(WhenNotified, db) arg_dict = dict(event=None, called=False) self.notify_timeout = False # Listen for 'event_1' @@ -255,14 +255,14 @@ # Open another connection for sending notifications. db2 = opendb() # Generate notification from the other connection. - db2.query("notify event_1, 'payload_1'") + db2.query("notify event_1, 'payload 1'") # Wait until the notification has been caught. for n in xrange(500): if arg_dict['event'] == 'event_1': break sleep(0.01) self.assertEqual(arg_dict['event'], 'event_1') - self.assertEqual(arg_dict['extra'], 'payload_1') + self.assertEqual(arg_dict['extra'], 'payload 1') self.assertTrue(isinstance(arg_dict['pid'], int)) # Check that callback has been invoked. self.assertTrue(arg_dict.get('called')) @@ -270,7 +270,7 @@ arg_dict['called'] = False self.assertTrue(thread.isAlive()) # Generate stop notification. - db2.query("notify stop_event_1, 'payload_2'") + db.query("notify stop_event_1, 'payload 2'") db2.close() # Wait until the notification has been caught. for n in xrange(500): @@ -278,7 +278,7 @@ break sleep(0.01) self.assertEqual(arg_dict['event'], 'stop_event_1') - self.assertEqual(arg_dict['extra'], 'payload_2') + self.assertEqual(arg_dict['extra'], 'payload 2') self.assertTrue(isinstance(arg_dict['pid'], int)) # Check that callback has been invoked. self.assertTrue(arg_dict.get('called')) @@ -289,10 +289,10 @@ target.close() def test_notify_timeout(self): - for test_method in False, True: + for as_method in False, True: db = opendb() # Get function under test, can be standalone or DB method. - fut = db.pgnotify if test_method else partial(pgnotify, db) + fut = db.when_notified if as_method else partial(WhenNotified, db) arg_dict = dict(event=None, called=False) self.notify_timeout = False # Listen for 'event_1' with timeout of 10ms Modified: trunk/module/pg.py ============================================================================== --- trunk/module/pg.py Sun Jan 6 12:44:25 2013 (r505) +++ trunk/module/pg.py Sun Jan 6 14:55:43 2013 (r506) @@ -28,9 +28,10 @@ # both that copyright notice and this permission notice appear in # supporting documentation. -import select - from _pg import * + +import select +import warnings try: frozenset except NameError: # Python < 2.4 @@ -139,7 +140,7 @@ return _db_error(msg, ProgrammingError) -class pgnotify(object): +class WhenNotified(object): """A PostgreSQL client-side asynchronous notification handler.""" def __init__(self, db, event, callback, arg_dict=None, timeout=None): @@ -153,6 +154,8 @@ fractions of seconds. If it is absent or None, the callers will never time out.""" + if isinstance(db, DB): + db = db.db self.db = db self.event = event self.stop = 'stop_%s' % event @@ -184,18 +187,26 @@ self.db.query('unlisten "%s"' % self.stop) self.listening = False + def notify(self, stop=False, payload=None): + if self.listening: + q = 'notify "%s"' % (stop and self.stop or self.event) + if payload: + q += ", '%s'" % payload + return self.db.query(q) + def __call__(self): """Invoke the handler. - The handler actually LISTENs for two NOTIFY messages: + The handler is a loop that actually LISTENs for two NOTIFY messages: <event> and stop_<event>. When either of these NOTIFY messages are received, its associated 'pid' and 'event' are inserted into <arg_dict>, and the callback is invoked with <arg_dict>. If the NOTIFY message is stop_<event>, the - handler UNLISTENs both <event> and stop_<event> and exits.""" + handler UNLISTENs both <event> and stop_<event> and exits. + """ self.listen() _ilist = [self.db.fileno()] @@ -225,7 +236,15 @@ % (self.event, self.stop, event)) -# The PostGreSQL database connection interface: +def pgnotify(*args, **kw): + """Same as WhenNotified, under the traditional name.""" + warnings.warn("pgnotify is deprecated, use WhenNotified instead.", + DeprecationWarning, stacklevel=2) + return WhenNotified(*args, **kw) + + +# The actual PostGreSQL database connection interface: + class DB(object): """Wrapper class for the _pg connection type.""" @@ -925,8 +944,10 @@ self._do_debug(q) return int(self.db.query(q)) - def pgnotify(self, event, callback, arg_dict={}, timeout=None): - return pgnotify(self.db, event, callback, arg_dict, timeout) + def when_notified(self, event, callback, arg_dict={}, timeout=None): + """Get notification handler that will run the given callback.""" + return WhenNotified(self.db, event, callback, arg_dict, timeout) + # if run as script, print some information _______________________________________________ PyGreSQL mailing list PyGreSQL@Vex.Net https://mail.vex.net/mailman/listinfo.cgi/pygresql