Skip to content
This repository has been archived by the owner on Jul 24, 2018. It is now read-only.

Commit

Permalink
PEP8 the fmn.consumer codebase
Browse files Browse the repository at this point in the history
This also adds a unit test that checks that all the code is
PEP8-compliant.

Signed-off-by: Jeremy Cline <jeremy@jcline.org>
  • Loading branch information
jeremycline committed Nov 3, 2016
1 parent c41cdbe commit 3009eee
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 116 deletions.
2 changes: 1 addition & 1 deletion fedmsg.d/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
high_water_mark=0,
io_threads=1,

## For the fedmsg-hub and fedmsg-relay. ##
# For the fedmsg-hub and fedmsg-relay. #

# We almost always want the fedmsg-hub to be sending messages with zmq as
# opposed to amqp or stomp.
Expand Down
8 changes: 4 additions & 4 deletions fedmsg.d/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
# infrastructure's bus. Cool, right? :)
"fedora-infrastructure": [
"tcp://hub.fedoraproject.org:9940",
#"tcp://stg.fedoraproject.org:9940",
# "tcp://stg.fedoraproject.org:9940",
],

# For other, more 'normal' services, fedmsg will try to guess the
# name of it's calling module to determine which endpoint definition
# to use. This can be overridden by explicitly providing the name in
# the initial call to fedmsg.init(...).
#"bodhi.%s" % hostname: ["tcp://127.0.0.1:3001"],
#"fas.%s" % hostname: ["tcp://127.0.0.1:3002"],
#"fedoratagger.%s" % hostname: ["tcp://127.0.0.1:3003"],
# "bodhi.%s" % hostname: ["tcp://127.0.0.1:3001"],
# "fas.%s" % hostname: ["tcp://127.0.0.1:3002"],
# "fedoratagger.%s" % hostname: ["tcp://127.0.0.1:3003"],
},
)
4 changes: 2 additions & 2 deletions fedmsg.d/fmn.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
"fmn": "purple",
},

## Backend stuff ##
# Backend stuff #

# This is the list of enabled backends (so we can turn one off globally)
#"fmn.backends": ['email', 'irc', 'android'],
# "fmn.backends": ['email', 'irc', 'android'],
"fmn.backends": ['email', 'irc'],
"fmn.backends.debug": False,

Expand Down
3 changes: 1 addition & 2 deletions fedmsg.d/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# Authors: Ralph Bean <rbean@redhat.com>
#
import os
import socket

SEP = os.path.sep
here = os.getcwd()
Expand All @@ -39,7 +38,7 @@
certnames={
# In prod/stg, map hostname to the name of the cert in ssldir.
# Unfortunately, we can't use socket.getfqdn()
#"app01.stg": "app01.stg.phx2.fedoraproject.org",
# "app01.stg": "app01.stg.phx2.fedoraproject.org",
},

# A mapping of fully qualified topics to a list of cert names for which
Expand Down
6 changes: 3 additions & 3 deletions fmn/consumer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from consumer import FMNConsumer
from producer import ConfirmationProducer
from producer import DigestProducer
from consumer import FMNConsumer # noqa
from producer import ConfirmationProducer # noqa
from producer import DigestProducer # noqa
14 changes: 5 additions & 9 deletions fmn/consumer/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@
import logging
import smtplib
import time
import random

logging.basicConfig(level=logging.DEBUG)

import pika
import fedmsg
import fedmsg.meta
import fedmsg_meta_fedora_infrastructure

from pika.adapters import twisted_connection
from twisted.internet import defer, reactor, protocol,task
from twisted.internet import defer, reactor, protocol, task

import fmn.lib
import fmn.rules.utils
Expand All @@ -26,6 +23,8 @@
import fmn.consumer.fmn_fasshim
from fedmsg_meta_fedora_infrastructure import fasshim


logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger("fmn")
log.setLevel('DEBUG')
CONFIG = fedmsg.config.load_config()
Expand Down Expand Up @@ -61,14 +60,12 @@
'email': fmn_backends.DebugBackend(**backend_kwargs),
'irc': fmn_backends.IRCBackend(**backend_kwargs),
'android': fmn_backends.DebugBackend(**backend_kwargs),
#'rss': fmn_backends.DebugBackend,
}
else:
backends = {
'email': fmn_backends.EmailBackend(**backend_kwargs),
'irc': fmn_backends.IRCBackend(**backend_kwargs),
'android': fmn_backends.GCMBackend(**backend_kwargs),
#'rss': fmn_backends.RSSBackend,
}

# But, disable any of those backends that don't appear explicitly in
Expand Down Expand Up @@ -179,7 +176,7 @@ def read(queue_object):
for recipient in recipients:
user = recipient['user']
t = time.time()
pref = PREFS.get('%s__%s' %(user, context))
pref = PREFS.get('%s__%s' % (user, context))
print "pref retrieved in: %0.2fs" % (time.time() - t)

try:
Expand All @@ -192,8 +189,7 @@ def read(queue_object):
print " Queueing msg for digest"
fmn.lib.models.QueuedMessage.enqueue(
session, user, context, raw_msg)
if ('filter_oneshot' in recipient
and recipient['filter_oneshot']):
if ('filter_oneshot' in recipient and recipient['filter_oneshot']):
print " Marking one-shot filter as fired"
idx = recipient['filter_id']
fltr = session.query(fmn.lib.models.Filter).get(idx)
Expand Down
8 changes: 4 additions & 4 deletions fmn/consumer/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mail import EmailBackend
from irc import IRCBackend
from android import GCMBackend
from debug import DebugBackend
from mail import EmailBackend # noqa
from irc import IRCBackend # noqa
from android import GCMBackend # noqa
from debug import DebugBackend # noqa
53 changes: 26 additions & 27 deletions fmn/consumer/backends/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,39 @@ def __init__(self, *args, **kwargs):
self.api_key = self.config['fmn.gcm.api_key']

def _send_notification(self, sess, registration_id, data):
'''Immediately send a notification to a device. This does **NOT** check
whether or not the user has notifications enabled. The calling method
**MUST** do this itself.'''
'''Immediately send a notification to a device. This does **NOT** check
whether or not the user has notifications enabled. The calling method
**MUST** do this itself.'''

# Extra data that applies to all messages goes here. Try to keep it
# short to save users bandwidth!
data['fmn_base_url'] = self.config['fmn.base_url']
# Extra data that applies to all messages goes here. Try to keep it
# short to save users bandwidth!
data['fmn_base_url'] = self.config['fmn.base_url']

headers = {
'Authorization': 'key=%s' % self.api_key,
'content-type': 'application/json',
}

body = {
'registration_ids': [registration_id],
'data': data,
}
headers = {
'Authorization': 'key=%s' % self.api_key,
'content-type': 'application/json',
}

response = requests.post(
self.post_url,
data=json.dumps(body),
headers=headers)
body = {
'registration_ids': [registration_id],
'data': data,
}

self.log.debug(" * got %r %r" % (response.status_code, response.text))
response = requests.post(
self.post_url,
data=json.dumps(body),
headers=headers)

j = response.json()
self.log.debug(" * got %r %r" % (response.status_code, response.text))

if j.get("message_id") and j.get("message_id").get("registration_id"):
self.log.debug(" * Was informed by Google that the " +
" registration id is old. Updating.")
j = response.json()

pref = fmn.lib.models.Preference.by_detail(sess, registration_id)
pref.update_details(sess, j.get("message_id").get("registration_id"))
if j.get("message_id") and j.get("message_id").get("registration_id"):
self.log.debug(" * Was informed by Google that the " +
" registration id is old. Updating.")

pref = fmn.lib.models.Preference.by_detail(sess, registration_id)
pref.update_details(sess, j.get("message_id").get("registration_id"))

def handle(self, session, recipient, msg, streamline=False):
self.log.debug("Notifying via gcm/android %r" % recipient)
Expand All @@ -57,7 +56,7 @@ def handle(self, session, recipient, msg, streamline=False):
return

if self.disabled_for(detail_value=recipient['registration id']):
self.log.debug("Messages stopped for %r, not sending." % nickname)
self.log.debug("Messages stopped for %r, not sending." % recipient)
return

self._send_notification(session, recipient['registration id'], msg)
Expand Down
27 changes: 14 additions & 13 deletions fmn/consumer/backends/debug.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from fmn.consumer.backends.base import BaseBackend
import datetime

import fedmsg.meta

import datetime
from fmn.consumer.backends.base import BaseBackend


CONFIRMATION_TEMPLATE = u"""
{username} has requested that notifications be sent to this email address
Expand All @@ -20,7 +22,6 @@ class DebugBackend(BaseBackend):
def __init__(self, *args, **kwargs):
super(DebugBackend, self).__init__(*args, **kwargs)


def handle(self, session, recipient, msg, streamline=False):
topic = msg['topic']
category = topic.split('.')[3]
Expand Down Expand Up @@ -54,12 +55,12 @@ def _format_line(msg):

return timestamp.strftime("%c") + ", " + payload + "\n\t" + link

n = len(queued_messages)
n = len(messages)
subject = u"Fedora Notifications Digest (%i updates)" % n
summary = u"Digest summary:\n"
for msg in queued_messages:
for i, msg in enumerate(messages):
line = fedmsg.meta.msg2subtitle(msg, **self.config) or u''
summary += str(i+1) + ".\t" + line + "\n"
summary += str(i + 1) + ".\t" + line + "\n"

separator = "\n\n" + "-"*79 + "\n\n"
if recipient.get('verbose', True):
Expand All @@ -69,18 +70,20 @@ def _format_line(msg):

content += separator.join([
_format_line(msg)
for msg in queued_messages])
for msg in messages])

topics = set([msg['topic'] for msg in queued_messages])
topics = set([msg['topic'] for msg in messages])
categories = set([topic.split('.')[3] for topic in topics])

squash = lambda items: reduce(set.union, items, set())
def squash(items):
reduce(set.union, items, set())

usernames = squash([
fedmsg.meta.msg2usernames(msg, **self.config)
for msg in queued_messages])
for msg in messages])
packages = squash([
fedmsg.meta.msg2packages(msg, **self.config)
for msg in queued_messages])
for msg in messages])

print ' -- Batch --'
print 'recipient: ', recipient
Expand All @@ -91,7 +94,6 @@ def _format_line(msg):
print 'usernames: ', usernames
print 'packages: ', packages


def handle_confirmation(self, session, confirmation):
confirmation.set_status(session, 'valid')
acceptance_url = self.config['fmn.acceptance_url'].format(
Expand All @@ -117,4 +119,3 @@ def handle_confirmation(self, session, confirmation):
print ' -- Confirmation --'
print 'recipient: ', recipient
print 'content: ', content

6 changes: 4 additions & 2 deletions fmn/consumer/backends/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _format_message(msg, recipient, config):
# Here we have to distinguish between two different kinds of messages that
# might arrive: the `raw` message from fedmsg itself and the product of a
# call to `fedmsg.meta.conglomerate(..)`
if not 'subtitle' in msg:
if 'subtitle' not in msg:
# This handles normal, 'raw' messages which get passed through msg2*.
title = fedmsg.meta.msg2title(msg, **config)
subtitle = fedmsg.meta.msg2subtitle(msg, **config)
Expand Down Expand Up @@ -117,7 +117,9 @@ def _format_message(msg, recipient, config):
flt = " ( triggered by %s )" % flt_link

if recipient['markup_messages']:
markup = lambda s, color: "\x03%i%s\x03" % (mirc_colors[color], s)
def markup(s, color):
return "\x03%i%s\x03" % (mirc_colors[color], s)

color_lookup = config.get('irc_color_lookup', {})
title_color = color_lookup.get(title.split('.')[0], "light grey")
title = markup(title, title_color)
Expand Down
14 changes: 7 additions & 7 deletions fmn/consumer/backends/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def __init__(self, *args, **kwargs):
self.mailserver = self.config['fmn.email.mailserver']
self.from_address = self.config['fmn.email.from_address']


def _get_mailserver(self, address, tries=0):
""" Connect to our mailserver, but retry a few times if we fail. """
try:
Expand Down Expand Up @@ -170,28 +169,29 @@ def _format_line(msg):
topics = set([message['topic'] for message in queued_messages])
categories = set([topic.split('.')[3] for topic in topics])

squash = lambda items: reduce(set.union, items, set())
def squash(items):
return reduce(set.union, items, set())

usernames = squash([
fedmsg.meta.msg2usernames(msg, **self.config)
for msg in queued_messages])
packages = squash([
fedmsg.meta.msg2packages(msg, **self.config)
for msg in queued_messages])

logger.info('Breaking email')
logger.info('Length: %d' % len(content))
self.log.info('Breaking email')
self.log.info('Length: %d' % len(content))

# Break email
split = 20000000
contents = [content[i:i+split] for i in range(0, len(content), split)]
contents = [content[x:x+split] for x in range(0, len(content), split)]

logger.info('Broken email into %d parts' % len(contents))
self.log.info('Broken email into %d parts' % len(contents))

for bodycnt in contents:
self.send_mail(session, recipient, subject, bodycnt,
topics, categories, usernames, packages)


def handle_confirmation(self, session, confirmation):
confirmation.set_status(session, 'valid')
acceptance_url = self.config['fmn.acceptance_url'].format(
Expand Down
9 changes: 4 additions & 5 deletions fmn/consumer/consumer.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
# An example fedmsg koji consumer

import datetime
import threading
import logging
import time
import random
import uuid

import fedmsg.consumers
import pika

import fmn.lib
import fmn.rules.utils

from fmn.consumer.util import (
new_packager,
new_badges_user,
get_fas_email,
)

import logging

log = logging.getLogger("fmn")

import pika
OPTS = pika.ConnectionParameters(
heartbeat_interval=0,
retry_delay=2,
Expand Down
Loading

0 comments on commit 3009eee

Please sign in to comment.