diff sat_pubsub/pgsql_storage.py @ 414:ccb2a22ea0fc

Python 3 port: /!\ Python 3.6+ is now needed to use SàT Pubsub /!\ instability may occur and features may not be working anymore, this will improve with time The same procedure as in backend has been applied (check backend commit ab2696e34d29 logs for details). Python minimal version has been updated in setup.py
author Goffi <goffi@goffi.org>
date Fri, 16 Aug 2019 12:53:33 +0200
parents a58610ab2983
children c21f31355ab9
line wrap: on
line diff
--- a/sat_pubsub/pgsql_storage.py	Fri Aug 16 12:48:34 2019 +0200
+++ b/sat_pubsub/pgsql_storage.py	Fri Aug 16 12:53:33 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 #-*- coding: utf-8 -*-
 
 # Copyright (c) 2012-2019 Jérôme Poisson
@@ -53,7 +53,7 @@
 
 import copy, logging
 
-from zope.interface import implements
+from zope.interface import implementer
 
 from twisted.internet import reactor
 from twisted.internet import defer
@@ -77,7 +77,7 @@
 
 # parseXml manage str, but we get unicode
 parseXml = lambda unicode_data: generic.parseXml(unicode_data.encode('utf-8'))
-ITEMS_SEQ_NAME = u'node_{node_id}_seq'
+ITEMS_SEQ_NAME = 'node_{node_id}_seq'
 PEP_COL_NAME = 'pep'
 CURRENT_VERSION = '5'
 # retrieve the maximum integer item id + 1
@@ -102,9 +102,9 @@
     return "{} {}".format(query, pep_check), values
 
 
+@implementer(iidavoll.IStorage)
 class Storage:
 
-    implements(iidavoll.IStorage)
 
     defaultConfig = {
             'leaf': {
@@ -435,7 +435,7 @@
         if node_accesses != ('open',) or item_accesses != ('open',):
             raise NotImplementedError('only "open" access model is handled for now')
         if not pep:
-            raise NotImplementedError(u"getLastItems is only implemented for PEP at the moment")
+            raise NotImplementedError("getLastItems is only implemented for PEP at the moment")
         d = self.dbpool.runQuery("""SELECT DISTINCT ON (node_id) pep, node, data::text, items.access_model
                                     FROM items
                                     NATURAL JOIN nodes
@@ -452,9 +452,9 @@
         return d
 
 
+@implementer(iidavoll.INode)
 class Node:
 
-    implements(iidavoll.INode)
 
     def __init__(self, nodeDbId, nodeIdentifier, config, schema):
         self.nodeDbId = nodeDbId
@@ -488,10 +488,10 @@
         if self._config[const.OPT_SERIAL_IDS]:
             d = self.dbpool.runQuery("SELECT nextval('{seq_name}')".format(
                 seq_name = ITEMS_SEQ_NAME.format(node_id=self.nodeDbId)))
-            d.addCallback(lambda rows: unicode(rows[0][0]))
+            d.addCallback(lambda rows: str(rows[0][0]))
             return d
         else:
-            return defer.succeed(unicode(uuid.uuid4()))
+            return defer.succeed(str(uuid.uuid4()))
 
     @staticmethod
     def _configurationTriggers(cursor, node_id, old_config, new_config):
@@ -645,7 +645,7 @@
 
         subscriptions = []
         for row in rows:
-            subscriber = jid.JID(u'%s/%s' % (row.jid, row.resource))
+            subscriber = jid.JID('%s/%s' % (row.jid, row.resource))
 
             options = {}
             if row.subscription_type:
@@ -730,7 +730,7 @@
         values = []
         for subscription in subscriptions:
             entity_id = entities_map[subscription.subscriber].entity_id
-            resource = subscription.subscriber.resource or u''
+            resource = subscription.subscriber.resource or ''
             values.append((self.nodeDbId, entity_id, resource, subscription.state, None, None))
         # we use upsert so new values are inserted and existing one updated. This feature is only available for PostgreSQL >= 9.5
         cursor.execute("INSERT INTO subscriptions(node_id, entity_id, resource, state, subscription_type, subscription_depth) VALUES " + placeholders + " ON CONFLICT (entity_id, resource, node_id) DO UPDATE SET state=EXCLUDED.state", [v for v in values])
@@ -808,7 +808,8 @@
         # then we construct values for affiliations update according to entity_id we just got
         placeholders = ','.join(len(affiliations) * ["(%s,%s,%s)"])
         values = []
-        map(values.extend, ((e.entity_id, affiliations[jid.JID(e.jid)], self.nodeDbId) for e in entities))
+        for e in entities:
+            values.extend((e.entity_id, affiliations[jid.JID(e.jid)], self.nodeDbId))
 
         # we use upsert so new values are inserted and existing one updated. This feature is only available for PostgreSQL >= 9.5
         cursor.execute("INSERT INTO affiliations(entity_id,affiliation,node_id) VALUES " + placeholders + " ON CONFLICT  (entity_id,node_id) DO UPDATE SET affiliation=EXCLUDED.affiliation", values)
@@ -836,9 +837,9 @@
         return [row[0] for row in rows]
 
 
+@implementer(iidavoll.ILeafNode)
 class LeafNode(Node):
 
-    implements(iidavoll.ILeafNode)
 
     nodeType = 'leaf'
 
@@ -851,7 +852,7 @@
         """
         keys = ext_data.get('order_by')
         if not keys:
-            return u'ORDER BY updated ' + direction
+            return 'ORDER BY updated ' + direction
         cols_statmnt = []
         for key in keys:
             if key == 'creation':
@@ -859,11 +860,11 @@
             elif key == 'modification':
                 column = 'updated'
             else:
-                log.msg(u"WARNING: Unknown order by key: {key}".format(key=key))
+                log.msg("WARNING: Unknown order by key: {key}".format(key=key))
                 column = 'updated'
-            cols_statmnt.append(column + u' ' + direction)
+            cols_statmnt.append(column + ' ' + direction)
 
-        return u"ORDER BY " + u",".join([col for col in cols_statmnt])
+        return "ORDER BY " + ",".join([col for col in cols_statmnt])
 
     @defer.inlineCallbacks
     def storeItems(self, items_data, publisher):
@@ -918,10 +919,10 @@
                     cursor.execute(NEXT_ITEM_ID_QUERY.format(node_id=self.nodeDbId))
                     next_id = cursor.fetchone()[0]
                     # we update the sequence, so we can skip conflicting ids
-                    cursor.execute(u"SELECT setval('{seq_name}', %s)".format(
+                    cursor.execute("SELECT setval('{seq_name}', %s)".format(
                         seq_name = ITEMS_SEQ_NAME.format(node_id=self.nodeDbId)), [next_id])
                     # and now we can retry the query with the new id
-                    item['id'] = insert_data[1] = unicode(next_id)
+                    item['id'] = insert_data[1] = str(next_id)
                     # item saved in DB must also be updated with the new id
                     insert_data[3] = item.toXml()
                     cursor.execute(insert_query, insert_data)
@@ -1059,7 +1060,7 @@
                         args.append(filter_.value)
                     else:
                         query_filters.append("AND publisher LIKE %s")
-                        args.append(u"{}%".format(filter_.value))
+                        args.append("{}%".format(filter_.value))
                 elif filter_.var == const.MAM_FILTER_CATEGORY:
                     query.append("LEFT JOIN item_categories USING (item_id)")
                     query_filters.append("AND category=%s")