<commit_before># Copyright (C) 2011  Governo do Estado do Rio Grande do Sul
#
#   Author: <NAME> <<EMAIL>>
#
# 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/>.

"""Module to carry about all interaction between wordpress XMLRPC
interface and our system.
"""

from xmlrpclib import Server
from flask import url_for
from gd import conf
from gd.model import User
import re
import datetime
from urllib import urlopen

class Namespace(object):
    """Abstracts an XMLRPC namespace available in wordpress"""
    def __init__(self, name, conf):
        self.name = name
        self.conf = conf
        self.server = Server(conf['address'])

    def wrap(self, attr, method):
        """Wrapper that decorates XMLRPC methods that needs the user,
        password and blog id to be passed before anything"""
        def wrapper(*args, **kwargs):
            # Yes, I'm aware that `kwargs' is not being expanded when
            # called in the next line. The params are being sent packed
            # to the XMLRPC server and there they'll be expanded.  Maybe
            # it will change in the future, if I find any function that
            # doesn't fit this strategy
            ret = method(
                self.conf['user'], self.conf['password'],
                kwargs, *args)
            converter = 'convert_%s' % attr
            if converter in globals():
                return globals()[converter](ret)
            return ret
        return wrapper

    def __getattribute__(self, attr):
        try:
            return super(Namespace, self).__getattribute__(attr)
        except AttributeError:
            return self.wrap(
                attr, getattr(self.server, '%s.%s' % (self.name, attr)))


class Wordpress(object):
    """Represents the Wordpress from which we consume
    mostly via XML-RPC"""
    def __init__(self, address, blogid, user, password):
        self.default_namespace = 'exapi'
        self.known_namespaces = 'exapi', 'wpgd', 'wp'
        self.conf = {
            'address': address,
            'user': user,
            'password': password
        }

    def getRSS(self):
        return wp_links_to_flask(urlopen(conf.WORDPRESS_RSS).read())

    def __getattribute__(self, attr):
        try:
            return super(Wordpress, self).__getattribute__(attr)
        except AttributeError:
            pass

        if attr in self.known_namespaces:
            return Namespace(attr, self.conf)

        # Falling back to our default namespace
        return getattr(Namespace(self.default_namespace, self.conf), attr)


def convert_getComments(comments):
    """Converts the comments coming from wordpress API"""
    for comment in comments:
        link = url_for('post', pid=comment['post_id'])
        comment['link'] = '%s#coment-%s' % (link, comment['comment_id'])
        comment['user'] = User.get(comment['user_id'])
    return comments


def convert_getPageByPath(post):
    """Converts a json that represents a wordpress page into a Post
    instance.

    If the requested page does not exist in the wordpress database,
    this function will return None"""
    return post and Post(post) or None


def convert_getRecentPosts(posts):
    """Convert JSON dictionaries in Post instances"""
    return [Post(i) for i in posts]


def convert_getPostsByCategory(data):
    posts_and_pagination(data)


def convert_getPostsByTag(data):
    posts_and_pagination(data)


def convert_getPost(post):
    return Post(post)


def convert_getMainSidebar(sidebar):
    """Converts all links found in the html of the sidebar to flask
    links"""
    return wp_links_to_flask(sidebar)<commit_msg>Ops. esqueci de retornar os valores de post_and_pagination()<commit_after># Copyright (C) 2011  Governo do Estado do Rio Grande do Sul
#
#   Author: <NAME> <<EMAIL>>
#
# 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/>.

"""Module to carry about all interaction between wordpress XMLRPC
interface and our system.
"""

from xmlrpclib import Server
from flask import url_for
from gd import conf
from gd.model import User
import re
import datetime
from urllib import urlopen

class Namespace(object):
    """Abstracts an XMLRPC namespace available in wordpress"""
    def __init__(self, name, conf):
        self.name = name
        self.conf = conf
        self.server = Server(conf['address'])

    def wrap(self, attr, method):
        """Wrapper that decorates XMLRPC methods that needs the user,
        password and blog id to be passed before anything"""
        def wrapper(*args, **kwargs):
            # Yes, I'm aware that `kwargs' is not being expanded when
            # called in the next line. The params are being sent packed
            # to the XMLRPC server and there they'll be expanded.  Maybe
            # it will change in the future, if I find any function that
            # doesn't fit this strategy
            ret = method(
                self.conf['user'], self.conf['password'],
                kwargs, *args)
            converter = 'convert_%s' % attr
            if converter in globals():
                return globals()[converter](ret)
            return ret
        return wrapper

    def __getattribute__(self, attr):
        try:
            return super(Namespace, self).__getattribute__(attr)
        except AttributeError:
            return self.wrap(
                attr, getattr(self.server, '%s.%s' % (self.name, attr)))


class Wordpress(object):
    """Represents the Wordpress from which we consume
    mostly via XML-RPC"""
    def __init__(self, address, blogid, user, password):
        self.default_namespace = 'exapi'
        self.known_namespaces = 'exapi', 'wpgd', 'wp'
        self.conf = {
            'address': address,
            'user': user,
            'password': password
        }

    def getRSS(self):
        return wp_links_to_flask(urlopen(conf.WORDPRESS_RSS).read())

    def __getattribute__(self, attr):
        try:
            return super(Wordpress, self).__getattribute__(attr)
        except AttributeError:
            pass

        if attr in self.known_namespaces:
            return Namespace(attr, self.conf)

        # Falling back to our default namespace
        return getattr(Namespace(self.default_namespace, self.conf), attr)


def convert_getComments(comments):
    """Converts the comments coming from wordpress API"""
    for comment in comments:
        link = url_for('post', pid=comment['post_id'])
        comment['link'] = '%s#coment-%s' % (link, comment['comment_id'])
        comment['user'] = User.get(comment['user_id'])
    return comments


def convert_getPageByPath(post):
    """Converts a json that represents a wordpress page into a Post
    instance.

    If the requested page does not exist in the wordpress database,
    this function will return None"""
    return post and Post(post) or None


def convert_getRecentPosts(posts):
    """Convert JSON dictionaries in Post instances"""
    return [Post(i) for i in posts]


def convert_getPostsByCategory(data):
    return posts_and_pagination(data)


def convert_getPostsByTag(data):
    return posts_and_pagination(data)


def convert_getPost(post):
    return Post(post)


def convert_getMainSidebar(sidebar):
    """Converts all links found in the html of the sidebar to flask
    links"""
    return wp_links_to_flask(sidebar)
{'fraction_non_alphanumeric': 0.08633009253836242, 'fraction_numerical': 0.0011713716762328688, 'mean_word_length': 2.87562414888788, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1497604', 'n_tokens_mistral': 2335, 'n_tokens_neox': 2255, 'n_words': 894}