From 0d0f6778db500eeb71aee763470b75489eee84c8 Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Mon, 26 Jun 2017 13:30:11 +0200 Subject: [PATCH 1/8] Change print from statement to function. --- moksha.hub/moksha/hub/amqp/qpid08.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/moksha.hub/moksha/hub/amqp/qpid08.py b/moksha.hub/moksha/hub/amqp/qpid08.py index bd802c4d..72912692 100644 --- a/moksha.hub/moksha/hub/amqp/qpid08.py +++ b/moksha.hub/moksha/hub/amqp/qpid08.py @@ -58,7 +58,7 @@ 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): @@ -66,7 +66,7 @@ def create_queue(self, queue, routing_key, exchange='amq.topic', 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, @@ -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() From e400abc0e9b4df6fc7d14cd1005e0b28428e2f82 Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Mon, 26 Jun 2017 13:30:48 +0200 Subject: [PATCH 2/8] Remove unused import. --- moksha.common/moksha/common/lib/helpers.py | 1 - 1 file changed, 1 deletion(-) diff --git a/moksha.common/moksha/common/lib/helpers.py b/moksha.common/moksha/common/lib/helpers.py index 1180abec..5f865f59 100644 --- a/moksha.common/moksha/common/lib/helpers.py +++ b/moksha.common/moksha/common/lib/helpers.py @@ -19,7 +19,6 @@ import re import os import logging -import warnings try: import configparser From 2f5a09a8b7e346c2b9816e15d1ff6ad7c2efb63c Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Mon, 26 Jun 2017 13:32:43 +0200 Subject: [PATCH 3/8] Use six.moves pseudo-module for compatible import instead of exceptions handling. --- moksha.common/moksha/common/config.py | 5 +---- moksha.hub/moksha/hub/api/consumer.py | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/moksha.common/moksha/common/config.py b/moksha.common/moksha/common/config.py index 75b41249..05b3f54a 100644 --- a/moksha.common/moksha/common/config.py +++ b/moksha.common/moksha/common/config.py @@ -1,9 +1,6 @@ import os -try: - import configparser -except ImportError: - import ConfigParser as configparser +from six.moves import configparser class EnvironmentConfigParser(configparser.ConfigParser): diff --git a/moksha.hub/moksha/hub/api/consumer.py b/moksha.hub/moksha/hub/api/consumer.py index 43ebc651..fa4d30a8 100644 --- a/moksha.hub/moksha/hub/api/consumer.py +++ b/moksha.hub/moksha/hub/api/consumer.py @@ -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 From a8887ce8a516c161d0870ebabf0ee549b2f9451c Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Mon, 26 Jun 2017 13:33:58 +0200 Subject: [PATCH 4/8] Use better (PEP8) way to check membership. --- moksha.common/moksha/common/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moksha.common/moksha/common/config.py b/moksha.common/moksha/common/config.py index 05b3f54a..7b18bc71 100644 --- a/moksha.common/moksha/common/config.py +++ b/moksha.common/moksha/common/config.py @@ -25,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 From cae31992545c78da843db0ea92dfb647f6e908a2 Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Mon, 26 Jun 2017 13:34:43 +0200 Subject: [PATCH 5/8] `ConfigParser.get()` should accept additional keywords for Python 3 compatibility. --- moksha.common/moksha/common/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moksha.common/moksha/common/config.py b/moksha.common/moksha/common/config.py index 7b18bc71..3558b2da 100644 --- a/moksha.common/moksha/common/config.py +++ b/moksha.common/moksha/common/config.py @@ -7,7 +7,7 @@ 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) From eaad1b80ff55572bf46f336bf6a1fc6bb1363b1c Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Mon, 26 Jun 2017 13:36:57 +0200 Subject: [PATCH 6/8] Fix Python 3 related ConfigParser issue. For some reason, in Python 3, values from config file are not replaced by default values given to constructor. It means that for example %(here)s is not replaced by path to folder with config file and `EnvironmentConfigParser._interpolate()` is trying to find this name in environment variables. This fix is based on manual look into ConfigParser defaults and manual replacing default values in case they are not in environment variables. --- moksha.common/moksha/common/config.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/moksha.common/moksha/common/config.py b/moksha.common/moksha/common/config.py index 3558b2da..eed30a9a 100644 --- a/moksha.common/moksha/common/config.py +++ b/moksha.common/moksha/common/config.py @@ -50,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) From a3e523c391126b6d956f4da3a1caa13f5bf76ff3 Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Mon, 26 Jun 2017 14:22:20 +0200 Subject: [PATCH 7/8] Fix DeprecationWarning. DeprecationWarning: The 'warn' method is deprecated, use 'warning' instead --- moksha.common/moksha/common/lib/helpers.py | 4 ++-- moksha.hub/moksha/hub/amqp/__init__.py | 6 +++--- moksha.hub/moksha/hub/api/consumer.py | 2 +- moksha.hub/moksha/hub/zeromq/zeromq.py | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/moksha.common/moksha/common/lib/helpers.py b/moksha.common/moksha/common/lib/helpers.py index 5f865f59..1e34df78 100644 --- a/moksha.common/moksha/common/lib/helpers.py +++ b/moksha.common/moksha/common/lib/helpers.py @@ -39,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 @@ -56,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(): diff --git a/moksha.hub/moksha/hub/amqp/__init__.py b/moksha.hub/moksha/hub/amqp/__init__.py index d40e2a5b..9ba00752 100644 --- a/moksha.hub/moksha/hub/amqp/__init__.py +++ b/moksha.hub/moksha/hub/amqp/__init__.py @@ -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 diff --git a/moksha.hub/moksha/hub/api/consumer.py b/moksha.hub/moksha/hub/api/consumer.py index fa4d30a8..a917eedd 100644 --- a/moksha.hub/moksha/hub/api/consumer.py +++ b/moksha.hub/moksha/hub/api/consumer.py @@ -194,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: diff --git a/moksha.hub/moksha/hub/zeromq/zeromq.py b/moksha.hub/moksha/hub/zeromq/zeromq.py index 5f39dc64..11edfccd 100644 --- a/moksha.hub/moksha/hub/zeromq/zeromq.py +++ b/moksha.hub/moksha/hub/zeromq/zeromq.py @@ -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(',') @@ -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) @@ -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): From 9bcc247841219d7419f3c2dacef7aacf71927d0e Mon Sep 17 00:00:00 2001 From: Lumir Balhar Date: Tue, 27 Jun 2017 14:15:16 +0200 Subject: [PATCH 8/8] `recv()` methods returns `str` for text data so it is not necessary to decode output to utf-8. --- moksha.hub/moksha/hub/tests/test_websockets.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/moksha.hub/moksha/hub/tests/test_websockets.py b/moksha.hub/moksha/hub/tests/test_websockets.py index 6b917c40..b864e0db 100644 --- a/moksha.hub/moksha/hub/tests/test_websockets.py +++ b/moksha.hub/moksha/hub/tests/test_websockets.py @@ -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() @@ -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 @@ -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 @@ -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