changeset 227:8540825f85e0

Replaced unmaintained pyPgSQL by Psycopg 2
author Goffi <goffi@goffi.org>
date Thu, 17 May 2012 00:31:36 +0200
parents b7018ec56ee5
children 77029ecf9817
files idavoll/pgsql_storage.py idavoll/tap.py idavoll/test/test_storage.py
diffstat 3 files changed, 42 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/idavoll/pgsql_storage.py	Sun Feb 13 21:46:33 2011 +0100
+++ b/idavoll/pgsql_storage.py	Thu May 17 00:31:36 2012 +0200
@@ -51,20 +51,20 @@
         if not row:
             raise error.NodeNotFound()
 
-        if row.node_type == 'leaf':
+        if row[0] == 'leaf':
             configuration = {
-                    'pubsub#persist_items': row.persist_items,
-                    'pubsub#deliver_payloads': row.deliver_payloads,
+                    'pubsub#persist_items': row[1],
+                    'pubsub#deliver_payloads': row[2],
                     'pubsub#send_last_published_item':
-                        row.send_last_published_item}
+                        row[3]}
             node = LeafNode(nodeIdentifier, configuration)
             node.dbpool = self.dbpool
             return node
-        elif row.node_type == 'collection':
+        elif row[0] == 'collection':
             configuration = {
-                    'pubsub#deliver_payloads': row.deliver_payloads,
+                    'pubsub#deliver_payloads': row[2],
                     'pubsub#send_last_published_item':
-                        row.send_last_published_item}
+                        row[3]}
             node = CollectionNode(nodeIdentifier, configuration)
             node.dbpool = self.dbpool
             return node
@@ -98,15 +98,15 @@
                             config['pubsub#deliver_payloads'],
                             config['pubsub#send_last_published_item'])
                            )
-        except cursor._pool.dbapi.OperationalError:
+        except cursor._pool.dbapi.IntegrityError:
             raise error.NodeExists()
 
         cursor.execute("""SELECT 1 from entities where jid=%s""",
-                       (owner))
+                       (owner,))
 
         if not cursor.fetchone():
             cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
-                           (owner))
+                           (owner,))
 
         cursor.execute("""INSERT INTO affiliations
                           (node_id, entity_id, affiliation)
@@ -144,9 +144,9 @@
         def toSubscriptions(rows):
             subscriptions = []
             for row in rows:
-                subscriber = jid.internJID('%s/%s' % (row.jid,
-                                                      row.resource))
-                subscription = Subscription(row.node, subscriber, row.state)
+                subscriber = jid.internJID('%s/%s' % (row[1],
+                                                      row[2]))
+                subscription = Subscription(row[0], subscriber, row[3])
                 subscriptions.append(subscription)
             return subscriptions
 
@@ -176,7 +176,7 @@
 
     def _checkNodeExists(self, cursor):
         cursor.execute("""SELECT node_id FROM nodes WHERE node=%s""",
-                       (self.nodeIdentifier))
+                       (self.nodeIdentifier,))
         if not cursor.fetchone():
             raise error.NodeNotFound()
 
@@ -263,7 +263,7 @@
         if not row:
             return None
         else:
-            return Subscription(self.nodeIdentifier, subscriber, row.state)
+            return Subscription(self.nodeIdentifier, subscriber, row[0])
 
 
     def getSubscriptions(self, state=None):
@@ -290,16 +290,16 @@
 
         subscriptions = []
         for row in rows:
-            subscriber = jid.JID('%s/%s' % (row.jid, row.resource))
+            subscriber = jid.JID('%s/%s' % (row[0], row[1]))
 
             options = {}
-            if row.subscription_type:
-                options['pubsub#subscription_type'] = row.subscription_type;
-            if row.subscription_depth:
-                options['pubsub#subscription_depth'] = row.subscription_depth;
+            if row[3]:
+                options['pubsub#subscription_type'] = row[3];
+            if row[4]:
+                options['pubsub#subscription_depth'] = row[4];
 
             subscriptions.append(Subscription(self.nodeIdentifier, subscriber,
-                                              row.state, options))
+                                              row[2], options))
 
         return subscriptions
 
@@ -320,9 +320,9 @@
 
         try:
             cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
-                           (userhost))
-        except cursor._pool.dbapi.OperationalError:
-            pass
+                           (userhost,))
+        except cursor._pool.dbapi.IntegrityError:
+            cursor._connection.rollback()
 
         try:
             cursor.execute("""INSERT INTO subscriptions
@@ -340,7 +340,7 @@
                             subscription_depth,
                             self.nodeIdentifier,
                             userhost))
-        except cursor._pool.dbapi.OperationalError:
+        except cursor._pool.dbapi.IntegrityError:
             raise error.SubscriptionExists()
 
 
@@ -399,7 +399,7 @@
                           NATURAL JOIN affiliations
                           NATURAL JOIN entities
                           WHERE node=%s""",
-                       self.nodeIdentifier)
+                       (self.nodeIdentifier,))
         result = cursor.fetchall()
 
         return [(jid.internJID(r[0]), r[1]) for r in result]
@@ -481,7 +481,7 @@
                            (self.nodeIdentifier,
                             maxItems))
         else:
-            cursor.execute(query, (self.nodeIdentifier))
+            cursor.execute(query, (self.nodeIdentifier,))
 
         result = cursor.fetchall()
         items = [stripNamespace(parseXml(r[0])) for r in result]
--- a/idavoll/tap.py	Sun Feb 13 21:46:33 2011 +0100
+++ b/idavoll/tap.py	Thu May 17 00:31:36 2012 +0200
@@ -49,7 +49,7 @@
     if config['backend'] == 'pgsql':
         from twisted.enterprise import adbapi
         from idavoll.pgsql_storage import Storage
-        dbpool = adbapi.ConnectionPool('pyPgSQL.PgSQL',
+        dbpool = adbapi.ConnectionPool('psycopg2',
                                        user=config['dbuser'],
                                        password=config['dbpass'],
                                        database=config['dbname'],
--- a/idavoll/test/test_storage.py	Sun Feb 13 21:46:33 2011 +0100
+++ b/idavoll/test/test_storage.py	Thu May 17 00:31:36 2012 +0200
@@ -480,7 +480,7 @@
         from idavoll.pgsql_storage import Storage
         from twisted.enterprise import adbapi
         if self.dbpool is None:
-            self.__class__.dbpool = adbapi.ConnectionPool('pyPgSQL.PgSQL',
+            self.__class__.dbpool = adbapi.ConnectionPool('psycopg2',
                                             database='pubsub_test',
                                             cp_reconnect=True,
                                             client_encoding='utf-8',
@@ -505,15 +505,15 @@
         cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-reconfigured')""")
         cursor.execute("""INSERT INTO nodes (node) VALUES ('to-be-purged')""")
         cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
-                       OWNER.userhost())
+                       (OWNER.userhost(),))
         cursor.execute("""INSERT INTO affiliations
                           (node_id, entity_id, affiliation)
                           SELECT node_id, entity_id, 'owner'
                           FROM nodes, entities
                           WHERE node='pre-existing' AND jid=%s""",
-                       OWNER.userhost())
+                       (OWNER.userhost(),))
         cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
-                       SUBSCRIBER.userhost())
+                       (SUBSCRIBER.userhost(),))
         cursor.execute("""INSERT INTO subscriptions
                           (node_id, entity_id, resource, state)
                           SELECT node_id, entity_id, %s, 'subscribed'
@@ -522,7 +522,7 @@
                        (SUBSCRIBER.resource,
                         SUBSCRIBER.userhost()))
         cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
-                       SUBSCRIBER_TO_BE_DELETED.userhost())
+                       (SUBSCRIBER_TO_BE_DELETED.userhost(),))
         cursor.execute("""INSERT INTO subscriptions
                           (node_id, entity_id, resource, state)
                           SELECT node_id, entity_id, %s, 'subscribed'
@@ -531,7 +531,7 @@
                        (SUBSCRIBER_TO_BE_DELETED.resource,
                         SUBSCRIBER_TO_BE_DELETED.userhost()))
         cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
-                       SUBSCRIBER_PENDING.userhost())
+                       (SUBSCRIBER_PENDING.userhost(),))
         cursor.execute("""INSERT INTO subscriptions
                           (node_id, entity_id, resource, state)
                           SELECT node_id, entity_id, %s, 'pending'
@@ -540,7 +540,7 @@
                        (SUBSCRIBER_PENDING.resource,
                         SUBSCRIBER_PENDING.userhost()))
         cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
-                       PUBLISHER.userhost())
+                       (PUBLISHER.userhost(),))
         cursor.execute("""INSERT INTO items
                           (node_id, publisher, item, data, date)
                           SELECT node_id, %s, 'to-be-deleted', %s,
@@ -569,18 +569,17 @@
                            'new 1', 'new 2', 'new 3', 'to-be-reconfigured',
                            'to-be-purged')""")
         cursor.execute("""DELETE FROM entities WHERE jid=%s""",
-                       OWNER.userhost())
+                       (OWNER.userhost(),))
         cursor.execute("""DELETE FROM entities WHERE jid=%s""",
-                       SUBSCRIBER.userhost())
+                       (SUBSCRIBER.userhost(),))
         cursor.execute("""DELETE FROM entities WHERE jid=%s""",
-                       SUBSCRIBER_TO_BE_DELETED.userhost())
+                       (SUBSCRIBER_TO_BE_DELETED.userhost(),))
         cursor.execute("""DELETE FROM entities WHERE jid=%s""",
-                       SUBSCRIBER_PENDING.userhost())
+                       (SUBSCRIBER_PENDING.userhost(),))
         cursor.execute("""DELETE FROM entities WHERE jid=%s""",
-                       PUBLISHER.userhost())
+                       (PUBLISHER.userhost(),))
 
 try:
-    import pyPgSQL
-    pyPgSQL
+    import psycopg2
 except ImportError:
-    PgsqlStorageStorageTestCase.skip = "pyPgSQL not available"
+    PgsqlStorageStorageTestCase.skip = "Psycopg2 not available"