changeset 2160:e67e8cd24141

core (tools/common): data objects first draft: this module aims is to help manipulate complex data from bridge, mainly for the template system. It is in common and not only in frontends as it may be used in some case by backend, if it needs to use template system in the future.
author Goffi <goffi@goffi.org>
date Tue, 21 Feb 2017 21:01:40 +0100
parents 5734b0994cf0
children 62dfa6e02f54
files src/plugins/plugin_xep_0277.py src/tools/common/data_objects.py
diffstat 2 files changed, 156 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0277.py	Tue Feb 21 21:01:39 2017 +0100
+++ b/src/plugins/plugin_xep_0277.py	Tue Feb 21 21:01:40 2017 +0100
@@ -608,9 +608,9 @@
 
         Determine the fields comments_service and comments_node of a microblog data
         from the href attribute of an entry's link element. For example this input:
-        xmpp:sat-pubsub.libervia.org?;node=urn%3Axmpp%3Acomments%3A_c5c4a142-2279-4b2a-ba4c-1bc33aa87634__urn%3Axmpp%3Agroupblog%3Asouliane%40libervia.org
-        will return (JID(u'sat-pubsub.libervia.org'), 'urn:xmpp:comments:_c5c4a142-2279-4b2a-ba4c-1bc33aa87634__urn:xmpp:groupblog:souliane@libervia.org')
-        @return: a tuple (JID, str)
+        xmpp:sat-pubsub.example.net?;node=urn%3Axmpp%3Acomments%3A_af43b363-3259-4b2a-ba4c-1bc33aa87634__urn%3Axmpp%3Agroupblog%3Asomebody%40example.net
+        will return(JID(u'sat-pubsub.example.net'), 'urn:xmpp:comments:_af43b363-3259-4b2a-ba4c-1bc33aa87634__urn:xmpp:groupblog:somebody@example.net')
+        @return (tuple[jid.JID, unicode]): service and node
         """
         parsed_url = urlparse.urlparse(node_url, 'xmpp')
         service = jid.JID(parsed_url.path)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/tools/common/data_objects.py	Tue Feb 21 21:01:40 2017 +0100
@@ -0,0 +1,153 @@
+#!/usr/bin/env python2
+# -*- coding: utf-8 -*-
+
+# SAT: a jabber client
+# Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+""" Objects handling bridge data, with jinja2 safe markup handling"""
+
+from sat.tools.common import data_format
+try:
+    from jinja2 import Markup as safe
+except ImportError:
+    safe = unicode
+
+
+class BlogItem(object):
+
+    def __init__(self, mb_data):
+        self.mb_data = mb_data
+        self._tags = None
+        self._groups = None
+        self._comments = None
+
+    @property
+    def id(self):
+        return self.mb_data.get(u'id')
+
+    @property
+    def atom_id(self):
+        return self.mb_data.get(u'atom_id')
+
+    @property
+    def published(self):
+        return self.mb_data.get(u'published')
+
+    @property
+    def updated(self):
+        return self.mb_data.get(u'updated')
+
+    @property
+    def author(self):
+        return self.mb_data.get(u'author')
+
+    @property
+    def author_jid(self):
+        return self.mb_data.get(u'author_jid')
+
+    @property
+    def author_jid_verified(self):
+        return self.mb_data.get(u'author_jid_verified')
+
+    @property
+    def author_email(self):
+        return self.mb_data.get(u'author_email')
+
+    @property
+    def tags(self):
+        if self._tags is None:
+            self._tags = data_format.dict2iter('tag', self.mb_data)
+        return self._tags
+
+    @property
+    def groups(self):
+        if self._groups is None:
+            self._groups = data_format.dict2iter('group', self.mb_data)
+        return self._groups
+
+    @property
+    def title(self):
+        return self.mb_data.get(u'title')
+
+    @property
+    def title_xhtml(self):
+        try:
+            return safe(self.mb_data[u'title_xhtml'])
+        except KeyError:
+            return None
+
+    @property
+    def content(self):
+        return self.mb_data.get(u'content')
+
+    @property
+    def content_xhtml(self):
+        try:
+            return safe(self.mb_data[u'content_xhtml'])
+        except KeyError:
+            return None
+
+    @property
+    def comments(self):
+        if self._comments is None:
+            self._comments = data_format.dict2iterdict(u'comments', self.mb_data, (u'node', u'service'))
+        return self._comments
+
+    @property
+    def comments_service(self):
+        return self.mb_data.get(u'comments_service')
+
+    @property
+    def comments_node(self):
+        return self.mb_data.get(u'comments_node')
+
+
+class BlogItems(object):
+
+    def __init__(self, mb_data):
+        self.mb_data = mb_data
+        self.items = [BlogItem(i) for i in mb_data[0]]
+        self.metadata = mb_data[1]
+
+    @property
+    def service(self):
+        return self.metadata[u'service']
+
+    @property
+    def node(self):
+        return self.metadata[u'node']
+
+    @property
+    def uri(self):
+        return self.metadata[u'uri']
+
+    def __len__(self):
+        return self.items.__len__()
+
+    def __missing__(self, key):
+        return self.items.__missing__(key)
+
+    def __getitem__(self, key):
+        return self.items.__getitem__(key)
+
+    def __iter__(self):
+        return self.items.__iter__()
+
+    def __reversed__(self):
+        return self.items.__reversed__()
+
+    def __contains__(self, item):
+        return self.items.__contains__(item)