<commit_before>"""
This module is currently an experiment in publishing messages to pulse.

It might become a real pulse publisher one day.
"""
import sys

from pulse_actions.authentication import (
    get_user_and_password,
    AuthenticationError,
)

from mozillapulse.publishers import GenericPublisher
from mozillapulse.config import PulseConfiguration
from mozillapulse.messages.base import GenericMessage


class ExperimentalPublisher(GenericPublisher):
    def __init__(self, user, **kwargs):
        super(ExperimentalPublisher, self).__init__(
            PulseConfiguration(**kwargs),
            'exchange/%s/pulse_actions' % user,
            **kwargs)


class MessageHandler:

    def __init__(self):
        """Create Publisher."""
        try:
            user, password = get_user_and_password()
            self.publisher = ExperimentalPublisher(
                user=user, password=password)
        except AuthenticationError as e:
            print(e.message)
            sys.exit(1)
        except Exception as e:
            # We continue without posting to pulse
            print('ERROR: We failed to post a pulse message with what we did')
            print(e.message)

    def publish_message(self, data, routing_key):
        """Publish a message to exchange/${pulse_user}/pulse_actions."""
        msg = GenericMessage()
        msg.routing_parts = routing_key.split('.')
        for key, value in data.iteritems():
            msg.set_data(key, value)
        self.publisher.publish(msg)<commit_msg>Issue 49 - Fix failure to publish<commit_after>"""
This module is currently an experiment in publishing messages to pulse.

It might become a real pulse publisher one day.
"""
import sys

from pulse_actions.authentication import (
    get_user_and_password,
    AuthenticationError,
)

from mozillapulse.publishers import GenericPublisher
from mozillapulse.config import PulseConfiguration
from mozillapulse.messages.base import GenericMessage


class MessageHandler:

    def __init__(self, **kwargs):
        """Create Publisher."""
        user, password = get_user_and_password()
        exchange = kwargs.get('exchange', 'exchange/%s/pulse_actions' % user)
        try:
            self.publisher = GenericPublisher(
                config=PulseConfiguration(user=user, password=password),
                exchange=exchange
            )

        except AuthenticationError as e:
            print(e.message)
            sys.exit(1)

        except Exception as e:
            # We continue without posting to pulse
            print('ERROR: We failed to post a pulse message with what we did')
            print(e.message)

    def publish_message(self, data, routing_key):
        """Publish a message to exchange/${pulse_user}/pulse_actions."""
        msg = GenericMessage()
        msg.routing_parts = routing_key.split('.')
        for key, value in data.iteritems():
            msg.set_data(key, value)
        self.publisher.publish(msg)
{'fraction_non_alphanumeric': 0.08523489932885905, 'fraction_numerical': 0.0013422818791946308, 'mean_word_length': 2.72625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3190054', 'n_tokens_mistral': 804, 'n_tokens_neox': 760, 'n_words': 230}