Skip to content

Commit

Permalink
Merge pull request #48 from frenzymadness/python3
Browse files Browse the repository at this point in the history
Python 3 compatibility for moksha.hub
  • Loading branch information
ralphbean authored Jul 11, 2017
2 parents 1c5d9f8 + 9bcc247 commit 3b3ed7d
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 29 deletions.
12 changes: 6 additions & 6 deletions moksha.common/moksha/common/config.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import os

try:
import configparser
except ImportError:
import ConfigParser as configparser
from six.moves import configparser


class EnvironmentConfigParser(configparser.ConfigParser):
"""ConfigParser which is able to substitute environment variables.
"""

def get(self, section, option, raw=0, vars=None):
def get(self, section, option, raw=0, vars=None, **kwargs):
try:
val = configparser.ConfigParser.get(
self, section, option, raw=raw, vars=vars)
Expand All @@ -28,7 +25,7 @@ def _interpolate(self, section, option, rawval, vars):
vars = {}

for k, v in os.environ.items():
if not k in vars.keys():
if k not in vars.keys():
vars[k] = v

value = rawval
Expand All @@ -53,6 +50,9 @@ def _interpolate(self, section, option, rawval, vars):

if key in vars:
value = value.replace("%(" + rawkey + ")s", vars[key], 1)
elif rawkey in self.defaults():
value = value.replace("%(" + rawkey + ")s",
self.defaults()[rawkey], 1)
else:
if default:
value = value.replace("%(" + rawkey + ")s", default, 1)
Expand Down
5 changes: 2 additions & 3 deletions moksha.common/moksha/common/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import re
import os
import logging
import warnings

try:
import configparser
Expand All @@ -40,7 +39,7 @@ def get_moksha_config_path():
if os.path.isfile(cfg):
return cfg

log.warn('No moksha configuration file found, make sure the '
log.warning('No moksha configuration file found, make sure the '
'controlling app is fully configured')

return None
Expand All @@ -57,7 +56,7 @@ def get_moksha_dev_config():
for cfg in cfgs:
if os.path.isfile(cfg):
return cfg
log.warn("Cannot find configuration in %r" % cfgs)
log.warning("Cannot find configuration in %r" % cfgs)


def get_moksha_appconfig():
Expand Down
6 changes: 3 additions & 3 deletions moksha.hub/moksha/hub/amqp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
from moksha.hub.amqp.qpid010 import QpidAMQPHubExtension
AMQPHubExtension = QpidAMQPHubExtension
except ImportError:
log.warn("Cannot find qpid python module. Make sure you have python-qpid installed.")
log.warning("Cannot find qpid python module. Make sure you have python-qpid installed.")
try:
from moksha.hub.amqp.pyamqplib import AMQPLibHubExtension
AMQPHubExtension = AMQPLibHubExtension
except ImportError:
log.warn("Cannot find pyamqplib")
log.warn("Using FakeHub AMQP broker. Don't expect AMQP to work")
log.warning("Cannot find pyamqplib")
log.warning("Using FakeHub AMQP broker. Don't expect AMQP to work")
class FakeHub(object):
pass
AMQPHub = FakeHub
Expand Down
10 changes: 5 additions & 5 deletions moksha.hub/moksha/hub/amqp/qpid08.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ def init_qpid_connection(self):
self.client.start({'LOGIN': self.user, 'PASSWORD': self.password})
self.conn = self.client.channel(1)
self.conn.channel_open()
print "opened channel!"
print("opened channel!")

def create_queue(self, queue, routing_key, exchange='amq.topic',
auto_delete=False, durable=True, **kw):
self.conn.queue_declare(queue=queue, auto_delete=auto_delete,
durable=durable, **kw)
self.conn.queue_bind(queue=queue, exchange=exchange,
routing_key=routing_key)
print "Created %s queue" % queue
print("Created %s queue" % queue)

def send_message(self, message, exchange='amq.topic', routing_key=''):
self.conn.basic_publish(routing_key=routing_key,
Expand All @@ -75,14 +75,14 @@ def send_message(self, message, exchange='amq.topic', routing_key=''):

def get(self, queue):
t = self.conn.basic_consume(queue=queue, no_ack=True)
print "t.consumer_tag =", t.consumer_tag
print("t.consumer_tag =", t.consumer_tag)
q = self.client.queue(t.consumer_tag)
msg = q.get()
print "got message: ", msg
print("got message: ", msg)
return msg.content.body
q.close()

def close(self):
if self.conn:
print "Closing connection"
print("Closing connection")
self.conn.close()
7 changes: 2 additions & 5 deletions moksha.hub/moksha/hub/api/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
import logging
log = logging.getLogger('moksha.hub')

try:
import queue # py3
except ImportError:
import Queue as queue # py2
import six.moves.queue as queue

from kitchen.iterutils import iterate
from moksha.common.lib.helpers import create_app_engine
Expand Down Expand Up @@ -197,7 +194,7 @@ def _do_work(self, message):
try:
self.validate(message)
except Exception as e:
log.warn("Received invalid message %r" % e)
log.warning("Received invalid message %r" % e)
return False # Not handled

try:
Expand Down
8 changes: 4 additions & 4 deletions moksha.hub/moksha/hub/tests/test_websockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def run(thread):
)))

# Receive that..
message = ws.recv().decode('utf-8')
message = ws.recv()
self.received_message = json.loads(message)['body']
ws.close()

Expand Down Expand Up @@ -150,7 +150,7 @@ def run(thread):
for i in range(num_topics):
try:
self.received_messages.append(
json.loads(ws.recv().decode('utf-8'))['body']
json.loads(ws.recv())['body']
)
except Exception:
pass
Expand Down Expand Up @@ -206,7 +206,7 @@ def run(thread):
for i in range(num_topics + 1):
try:
self.received_messages.append(
json.loads(ws.recv().decode('utf-8'))['body']
json.loads(ws.recv())['body']
)
except Exception:
pass
Expand Down Expand Up @@ -262,7 +262,7 @@ def run(thread):
for i in range(num_topics + 2):
try:
thread.received_messages.append(
json.loads(ws.recv().decode('utf-8'))['body']
json.loads(ws.recv())['body']
)
except Exception:
pass
Expand Down
6 changes: 3 additions & 3 deletions moksha.hub/moksha/hub/zeromq/zeromq.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def validate_config(self, config):
required_attrs = ['zmq_publish_endpoints', 'zmq_subscribe_endpoints']
for attr in required_attrs:
if not config.get(attr, None):
log.warn("No '%s' set. Are you sure?" % attr)
log.warning("No '%s' set. Are you sure?" % attr)
continue

endpoints = config[attr].split(',')
Expand All @@ -147,7 +147,7 @@ def send_message(self, topic, message, **headers):
try:
self.pub_socket.send_multipart([topic, message])
except zmq.ZMQError as e:
log.warn("Couldn't send message: %r" % e)
log.warning("Couldn't send message: %r" % e)

super(ZMQHubExtension, self).send_message(topic, message, **headers)

Expand Down Expand Up @@ -178,7 +178,7 @@ def subscribe(self, topic, callback):
self.connection_cls(
self.twisted_zmq_factory, endpoint)
except zmq.ZMQError as e:
log.warn("Failed txzmq create on %r %r" % (endpoint, e))
log.warning("Failed txzmq create on %r %r" % (endpoint, e))
continue

def chain_over_moksha_callbacks(*parts):
Expand Down

0 comments on commit 3b3ed7d

Please sign in to comment.