view src/tools/common/data_objects.py @ 2316:7b448ac50a69

jp (pubsub): new search command: search is a kind of "grep for Pubsub". It's a powerful command which allows to look for specific data in a pubsub node, recurse sub nodes if requested, and execute an action on the result. search allows to look for items with following filter: - simple text search - regex - xpath - python code filters are read an applied in the order in which they appear on the command line. Then flags can be used to modify behaviour, currently there are: - ignore-case to specify if search must be case sensitive or not - invert to invert result of the search (i.e. don't match instead of match) - dot-all which is specific for regex, cf. re module - only-matching which return the matching part instead of the full item Once a item match filters, an action is applied to it, currenlty there are: - print, which do a simple output of the full item (default) - exec, which run a jp command, specifying the service, node and item corresponding to the match - exteral, which run a external command, sending the full item on stdin By default search is only done on requested node, but if max-depth is more than 0, sub nodes will be searched too.
author Goffi <goffi@goffi.org>
date Sat, 08 Jul 2017 21:54:24 +0200
parents 173d56315529
children a37457da2bb7
line wrap: on
line source

#!/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
        self._comments_items_list = 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 language(self):
        return self.mb_data.get(u'language')

    @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')

    @property
    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 = []
        self._comments_items_list.append(items)


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)


class Identity(object):

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

    @property
    def nick(self):
        return self.data.get(u'nick')


class Identities(object):

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

    def __getitem__(self, jid_str):
        try:
            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