view src/tools/common/ @ 2469:adcc35625e17

tools (common/data_objects): added Room class
author Goffi <>
date Fri, 05 Jan 2018 13:00:34 +0100
parents 8788c217a49e
children 6607bb29ab7b
line wrap: on
line source

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

# SAT: a jabber client
# Copyright (C) 2009-2017 Jérôme Poisson (

# 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
# 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 <>.

"""Objects handling bridge data, with jinja2 safe markup handling"""

from import data_format
    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
        self._comments_items_list = None

    def id(self):
        return self.mb_data.get(u'id')

    def atom_id(self):
        return self.mb_data.get(u'atom_id')

    def published(self):
        return self.mb_data.get(u'published')

    def updated(self):
        return self.mb_data.get(u'updated')

    def language(self):
        return self.mb_data.get(u'language')

    def author(self):
        return self.mb_data.get(u'author')

    def author_jid(self):
        return self.mb_data.get(u'author_jid')

    def author_jid_verified(self):
        return self.mb_data.get(u'author_jid_verified')

    def author_email(self):
        return self.mb_data.get(u'author_email')

    def tags(self):
        if self._tags is None:
            self._tags = data_format.dict2iter('tag', self.mb_data)
        return self._tags

    def groups(self):
        if self._groups is None:
            self._groups = data_format.dict2iter('group', self.mb_data)
        return self._groups

    def title(self):
        return self.mb_data.get(u'title')

    def title_xhtml(self):
            return safe(self.mb_data[u'title_xhtml'])
        except KeyError:
            return None

    def content(self):
        return self.mb_data.get(u'content')

    def content_xhtml(self):
            return safe(self.mb_data[u'content_xhtml'])
        except KeyError:
            return None

    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

    def comments_service(self):
        return self.mb_data.get(u'comments_service')

    def comments_node(self):
        return self.mb_data.get(u'comments_node')

    def comments_items_list(self):
        return [] if self._comments_items_list is None else self._comments_items_list

    def appendCommentsItems(self, items):
        """append comments items to self.comments_items"""
        if self._comments_items_list is None:
            self._comments_items_list = []

class BlogItems(object):

    def __init__(self, mb_data):
        self.items = [BlogItem(i) for i in mb_data[0]]
        self.metadata = mb_data[1]

    def service(self):
        return self.metadata[u'service']

    def node(self):
        return self.metadata[u'node']

    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)

class Message(object):

    def __init__(self, msg_data):
        self._uid = msg_data[0]
        self._timestamp = msg_data[1]
        self._from_jid = msg_data[2]
        self._to_jid = msg_data[3]
        self._message_data = msg_data[4]
        self._subject_data = msg_data[5]
        self._type = msg_data[6]
        self._extra = msg_data[7]
        self._html = dict(data_format.getSubDict('xhtml', self._extra))

    def id(self):
        return self._uid

    def timestamp(self):
        return self._timestamp

    def from_(self):
        return self._from_jid

    def text(self):
            return self._message_data['']
        except KeyError:
            return next(self._message_data.itervalues())

    def subject(self):
            return self._subject_data['']
        except KeyError:
            return next(self._subject_data.itervalues())

    def type(self):
        return self._type

    def thread(self):
        return self._extra.get('thread')

    def thread_parent(self):
        return self._extra.get('thread_parent')

    def received(self):
        return self._extra.get('received_timestamp', self._timestamp)

    def delay_sender(self):
        return self._extra.get('delay_sender')

    def info_type(self):
        return self._extra.get('info_type')

    def html(self):
        if not self._html:
            return None
            return safe(self._html[''])
        except KeyError:
            return safe(next(self._html.itervalues()))

class Messages(object):

    def __init__(self, msgs_data):
        self.messages = [Message(m) for m in msgs_data]

    def __len__(self):
        return self.messages.__len__()

    def __missing__(self, key):
        return self.messages.__missing__(key)

    def __getitem__(self, key):
        return self.messages.__getitem__(key)

    def __iter__(self):
        return self.messages.__iter__()

    def __reversed__(self):
        return self.messages.__reversed__()

    def __contains__(self, item):
        return self.messages.__contains__(item)

class Room(object):

    def __init__(self, jid, name=None, url=None):
        self.jid = jid = name or jid
        if url is not None:
            self.url = url

class Identity(object):

    def __init__(self, jid_str, data=None):
        self.jid_str = jid_str = data if data is not None else {}

    def nick(self):

class Identities(object):

    def __init__(self):
        self.identities = {}

    def __getitem__(self, jid_str):
            return self.identities[jid_str]
        except KeyError:
            return None

    def __setitem__(self, jid_str, data):
        self.identities[jid_str] = Identity(jid_str, data)

    def __contains__(self, jid_str):
        return jid_str in self.identities

class OnClick(object):
    """Class to handle clickable elements targets"""

    def __init__(self, url=None):
        self.url = url

    def formatUrl(self, item):
        """Use an item to format URL

        item will be formatted using "item=item" in format()
        return self.url.format(item=item)