<commit_before># This file is part of fedmsg.
# Copyright (C) 2012-2014 Red Hat, Inc.
#
# fedmsg is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# fedmsg 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with fedmsg; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Authors:  <NAME> <<EMAIL>>
#           <NAME> <<EMAIL>>

import re

from fedmsg_meta_fedora_infrastructure import BaseProcessor
from fedmsg_meta_fedora_infrastructure.fasshim import avatar_url

from fedmsg_meta_fedora_infrastructure.conglomerators.bodhi import \
        requests as bodhi_requests
from fedmsg_meta_fedora_infrastructure.conglomerators.bodhi import \
        comments as bodhi_comments


def get_sync_product(msg):
    # Extract the product name from a ftpsync message.
    product = msg['topic'].split('.')[-2]
    if product == 'fedora':
        product = 'Fedora'
    elif product == 'epel':
        product = 'EPEL'
    else:
        pass
    return product


def is_ftp_sync(msg):
    return 'bodhi.updates.' in msg['topic'] and msg['topic'].endswith('.sync')


def author_link(username):
    return "<a href='https://bodhi.fedoraproject.org/" + \
        "users/{user}'>{user}</a>".format(user=username)


def update_link(alias, title):
    return "<a href='https://bodhi.fedoraproject.org/updates/" + \
        "{alias}'>{title}</a>".format(alias=alias, title=truncate(title))


def truncate(title):
    if len(title) >= 40:
        title = title[:40] + "..."
    return title


class BodhiProcessor(BaseProcessor):
    __name__ = "Bodhi"
    __description__ = "the Fedora update system"
    __link__ = "https://bodhi.fedoraproject.org/"
    __docs__ = "https://fedoraproject.org/wiki/Bodhi"
    __obj__ = "Package Updates"
    __icon__ = "https://apps.fedoraproject.org/img/icons/bodhi.png"
    conglomerators = [
        bodhi_requests.ByUserAndPackageTesting,
        bodhi_requests.ByUserAndPackageStable,
        bodhi_requests.ByPackage,
        bodhi_requests.ByUser,
        bodhi_comments.ByUpdate,
        bodhi_comments.ByUser,
    ]

    def _u2p(self, update):
        """ Take an update, and return the package name """
        # TODO -- make this unnecessary by having bodhi emit the
        # package name (and not just the update name) to begin with.
        return [build.rsplit('-', 2)[0] for build in update.split(',')]

    def title_or_alias(self, msg):
        value = msg['msg']['update'].get('alias')
        if not value:
            value = msg['msg']['update']['title']
        return value

    def secondary_icon(self, msg, **config):
        username = ''
        if 'bodhi.update.comment' in msg['topic']:
            username = msg['msg']['comment']['author']
        elif 'bodhi.buildroot_override' in msg['topic']:
            username = msg['msg']['override']['submitter']
        elif 'bodhi.stack' in msg['topic']:
            username = msg['msg']['agent']
        elif 'update' in msg['msg'] and 'submitter' in msg['msg']['update']:
            username = msg['msg']['update']['submitter']
        else:
            username = msg['msg'].get('agent')

        if isinstance(username, dict):
            username = username['name']

        avatar = self.__icon__
        if username:
            avatar = avatar_url(username)
        else:
            # If we don't have a user, then try with a package
            if 'update' in msg['msg']:
                packages = self._u2p(msg['msg']['update']['title'])
                tmpl = 'https://apps.fedoraproject.org/packages/' + \
                    'images/icons/%s.png'
                if packages:
                    avatar = tmpl % packages[0]

        return avatar

    def long_form(self, msg, **config):
        if 'bodhi.update.comment' in msg['topic']:
            return msg['msg']['comment']['text']
        elif 'bodhi.errata.publish' in msg['topic']:
            return msg['msg']['body']
        elif 'bodhi.masher.start' in msg['topic']:
            return "\n- " + "\n- ".join(msg['msg']['updates']) + "\n"

    def subtitle(self, msg, **config):
        markup = config.get('markup', False)

        if is_ftp_sync(msg):
            product = get_sync_product(msg)
            msg = msg['msg']
            tmpl = self._(
                'New {product} {release} {repo} content synced out '
                '({bytes} changed with {deleted} files deleted)')
            return tmpl.format(product=product, **msg)
        elif 'bodhi.masher.start' in msg['topic']:
            agent = msg['msg']['agent']
            updates = len(msg['msg']['updates'])
            tmpl = self._("{agent} requested a mash of {updates} updates")
            return tmpl.format(agent=agent, updates=updates)
        elif 'bodhi.update.comment' in msg['topic']:
            author = msg['msg']['comment']['author']
            karma = msg['msg']['comment']['karma']
            title = msg['msg']['comment']['update_title']
            alias = msg['msg']['comment'].get('update', {}).get('alias') or title

            tmpl = self._(
                "{author} commented on bodhi update {title} (karma: {karma})"
            )
            if not markup:
                return tmpl.format(author=author, karma=karma,
                                   title=truncate(title))
            else:
                return tmpl.format(
                    author=author_link(author),
                    title=update_link(alias, title),
                    karma=karma,
                )

        elif 'bodhi.update.complete.' in msg['topic']:
            author = msg['msg']['update']['submitter']
            if isinstance(author, dict):
                author = author['name']
            package = truncate(msg['msg']['update']['title'])
            status = msg['msg']['update']['status']
            tmpl = self._(<commit_msg>pep8.<commit_after># This file is part of fedmsg.
# Copyright (C) 2012-2014 Red Hat, Inc.
#
# fedmsg is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# fedmsg 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with fedmsg; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Authors:  <NAME> <<EMAIL>>
#           <NAME> <<EMAIL>>

import re

from fedmsg_meta_fedora_infrastructure import BaseProcessor
from fedmsg_meta_fedora_infrastructure.fasshim import avatar_url

from fedmsg_meta_fedora_infrastructure.conglomerators.bodhi import \
        requests as bodhi_requests
from fedmsg_meta_fedora_infrastructure.conglomerators.bodhi import \
        comments as bodhi_comments


def get_sync_product(msg):
    # Extract the product name from a ftpsync message.
    product = msg['topic'].split('.')[-2]
    if product == 'fedora':
        product = 'Fedora'
    elif product == 'epel':
        product = 'EPEL'
    else:
        pass
    return product


def is_ftp_sync(msg):
    return 'bodhi.updates.' in msg['topic'] and msg['topic'].endswith('.sync')


def author_link(username):
    return "<a href='https://bodhi.fedoraproject.org/" + \
        "users/{user}'>{user}</a>".format(user=username)


def update_link(alias, title):
    return "<a href='https://bodhi.fedoraproject.org/updates/" + \
        "{alias}'>{title}</a>".format(alias=alias, title=truncate(title))


def truncate(title):
    if len(title) >= 40:
        title = title[:40] + "..."
    return title


class BodhiProcessor(BaseProcessor):
    __name__ = "Bodhi"
    __description__ = "the Fedora update system"
    __link__ = "https://bodhi.fedoraproject.org/"
    __docs__ = "https://fedoraproject.org/wiki/Bodhi"
    __obj__ = "Package Updates"
    __icon__ = "https://apps.fedoraproject.org/img/icons/bodhi.png"
    conglomerators = [
        bodhi_requests.ByUserAndPackageTesting,
        bodhi_requests.ByUserAndPackageStable,
        bodhi_requests.ByPackage,
        bodhi_requests.ByUser,
        bodhi_comments.ByUpdate,
        bodhi_comments.ByUser,
    ]

    def _u2p(self, update):
        """ Take an update, and return the package name """
        # TODO -- make this unnecessary by having bodhi emit the
        # package name (and not just the update name) to begin with.
        return [build.rsplit('-', 2)[0] for build in update.split(',')]

    def title_or_alias(self, msg):
        value = msg['msg']['update'].get('alias')
        if not value:
            value = msg['msg']['update']['title']
        return value

    def secondary_icon(self, msg, **config):
        username = ''
        if 'bodhi.update.comment' in msg['topic']:
            username = msg['msg']['comment']['author']
        elif 'bodhi.buildroot_override' in msg['topic']:
            username = msg['msg']['override']['submitter']
        elif 'bodhi.stack' in msg['topic']:
            username = msg['msg']['agent']
        elif 'update' in msg['msg'] and 'submitter' in msg['msg']['update']:
            username = msg['msg']['update']['submitter']
        else:
            username = msg['msg'].get('agent')

        if isinstance(username, dict):
            username = username['name']

        avatar = self.__icon__
        if username:
            avatar = avatar_url(username)
        else:
            # If we don't have a user, then try with a package
            if 'update' in msg['msg']:
                packages = self._u2p(msg['msg']['update']['title'])
                tmpl = 'https://apps.fedoraproject.org/packages/' + \
                    'images/icons/%s.png'
                if packages:
                    avatar = tmpl % packages[0]

        return avatar

    def long_form(self, msg, **config):
        if 'bodhi.update.comment' in msg['topic']:
            return msg['msg']['comment']['text']
        elif 'bodhi.errata.publish' in msg['topic']:
            return msg['msg']['body']
        elif 'bodhi.masher.start' in msg['topic']:
            return "\n- " + "\n- ".join(msg['msg']['updates']) + "\n"

    def subtitle(self, msg, **config):
        markup = config.get('markup', False)

        if is_ftp_sync(msg):
            product = get_sync_product(msg)
            msg = msg['msg']
            tmpl = self._(
                'New {product} {release} {repo} content synced out '
                '({bytes} changed with {deleted} files deleted)')
            return tmpl.format(product=product, **msg)
        elif 'bodhi.masher.start' in msg['topic']:
            agent = msg['msg']['agent']
            updates = len(msg['msg']['updates'])
            tmpl = self._("{agent} requested a mash of {updates} updates")
            return tmpl.format(agent=agent, updates=updates)
        elif 'bodhi.update.comment' in msg['topic']:
            comment = msg['msg']['comment']
            author = comment['author']
            karma = comment['karma']
            title = comment['update_title']
            alias = comment.get('update', {}).get('alias') or title

            tmpl = self._(
                "{author} commented on bodhi update {title} (karma: {karma})"
            )
            if not markup:
                return tmpl.format(author=author, karma=karma,
                                   title=truncate(title))
            else:
                return tmpl.format(
                    author=author_link(author),
                    title=update_link(alias, title),
                    karma=karma,
                )

        elif 'bodhi.update.complete.' in msg['topic']:
            author = msg['msg']['update']['submitter']
            if isinstance(author, dict):
                author = author['name']
            package = truncate(msg['msg']['update']['title'])
            status = msg['msg']['update']['status']
            tmpl = self._(
{'fraction_non_alphanumeric': 0.13456672991777355, 'fraction_numerical': 0.004981024667931689, 'mean_word_length': 2.3829901043059643, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3736709', 'n_tokens_mistral': 3747, 'n_tokens_neox': 3593, 'n_words': 983}