changeset 2770:2d353b74010e

tools (common/data_objects): moved BlogItems._parseMetadata to a parsePubSubMetadata function, so it can be used in a more generic way
author Goffi <goffi@goffi.org>
date Fri, 11 Jan 2019 21:38:20 +0100
parents 9282b289b964
children 003b8b4b56a7
files sat/tools/common/data_objects.py
diffstat 1 files changed, 39 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/sat/tools/common/data_objects.py	Fri Jan 11 19:49:01 2019 +0100
+++ b/sat/tools/common/data_objects.py	Fri Jan 11 21:38:20 2019 +0100
@@ -33,6 +33,44 @@
 q = lambda value: urllib.quote(value.encode("utf-8"), safe="@")
 
 
+def parsePubSubMetadata(metadata, items):
+    """Helper method to have nicer metadata while doing a PubSub request
+
+    The "rsm_index" and "rsm_count" keys will be converted to int
+    The "mam_stable" and "mam_complete" keys will be converted to boolean
+    A "complete" key can have the following values:
+        - True: we are on the last page, i.e. last items have been received
+        - False: we are not on the last page
+        - None: we don't have enough data fo know our position in the pagination
+    @param metadata(dict): dict retrieved fro bridge with request metadata
+    @param items(list): items retrieved (used to determine "complete" value)
+    @return (dict): parsed metadata
+    """
+    data = {}
+    assert "complete" not in metadata
+
+    for key, value in metadata.iteritems():
+        if key in (u"rsm_index", u"rsm_count"):
+            value = int(value)
+        elif key == u"mam_stable":
+            value = C.bool(value)
+        elif key == u"mam_complete":
+            key = u"complete"
+            value = C.bool(value)
+        data[key] = value
+    if u"complete" not in data:
+        index = data.get(u"rsm_index")
+        count = data.get(u"rsm_count")
+        if index is None or count is None:
+            # we don't have enough information to know if the data is complete or not
+            data[u"complete"] = None
+        else:
+            # normally we have a strict equality here but XEP-0059 states
+            # that index MAY be approximative, so just in case…
+            data[u"complete"] = index + len(items) >= count
+    return data
+
+
 class BlogItem(object):
     def __init__(self, mb_data, parent):
         self.mb_data = mb_data
@@ -150,7 +188,7 @@
 class BlogItems(object):
     def __init__(self, mb_data):
         self.items = [BlogItem(i, self) for i in mb_data[0]]
-        self.metadata = self._parseMetadata(mb_data[1])
+        self.metadata = parsePubSubMetadata(mb_data[1], self.items)
 
     @property
     def service(self):
@@ -189,30 +227,6 @@
     def complete(self):
         return self.metadata[u"complete"]
 
-    def _parseMetadata(self, metadata):
-        data = {}
-
-        for key, value in metadata.iteritems():
-            if key in (u"rsm_index", u"rsm_count"):
-                value = int(value)
-            elif key == u"mam_stable":
-                value = C.bool(value)
-            elif key == u"mam_complete":
-                key = u"complete"
-                value = C.bool(value)
-            data[key] = value
-        if u"complete" not in data:
-            index = data.get(u"rsm_index")
-            count = data.get(u"rsm_count")
-            if index is None or count is None:
-                # we don't have enough information to know if the data is complete or not
-                data[u"complete"] = None
-            else:
-                # normally we have a strict equality here but XEP-0059 states
-                # that index MAY be approximative, so just in case…
-                data[u"complete"] = index + len(self.items) >= count
-        return data
-
     def __len__(self):
         return self.items.__len__()