Skip to content

Commit

Permalink
Port to Python 3
Browse files Browse the repository at this point in the history
Fixes: #75

Signed-off-by: Aurélien Bompard <[email protected]>
  • Loading branch information
abompard committed Jul 2, 2020
1 parent 63822f6 commit dfedf14
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 136 deletions.
19 changes: 13 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ deploy:
secure: LV+DMK3jdFoVlOnFn9OKwDjfCiPfK/3XPna9W98SIPZVuRxFEf0Q7lt/H0vnh2diqlNjvfTh7HbWcCEztBBFZaOPURHM2yToIo2Upn6hSXMWYAHdZz7gLQ45DexhDItQFJOoJChl7Q7ChEbltj/asUiHqxSHSdsfTJhDgRPFzWk=
provider: pypi
user: jimbydamonk
env:
- TOXENV=py27
- TOXENV=pep8
install:
- pip install -r requirements.txt
- pip install -U tox twine wheel
- pip install coveralls
language: python
python:
- "2.7"
script: tox
# https://docs.travis-ci.com/user/languages/python/#using-tox-as-the-build-script
matrix:
include:
- python: 3.8
env: TOXENV=pep8
- python: 3.5
env: TOXENV=py35
- python: 3.6
env: TOXENV=py36
- python: 3.7
env: TOXENV=py37
- python: 3.8
env: TOXENV=py38
12 changes: 7 additions & 5 deletions collectd_rabbitmq/collectd_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

import collectd
import re
import urllib
import urllib.request
import urllib.parse
import urllib.error

from collectd_rabbitmq import rabbit
from collectd_rabbitmq import utils
Expand Down Expand Up @@ -136,7 +138,7 @@ def generate_vhost_name(self, name):
Generate a "normalized" vhost name without / (or escaped /).
"""
if name:
name = urllib.unquote(name)
name = urllib.parse.unquote(name)

if not name or name == '/':
name = 'default'
Expand Down Expand Up @@ -282,7 +284,7 @@ def dispatch_queue_stats(self, data, vhost, plugin, plugin_instance):
value = data[name]
except KeyError:
continue
if name is 'consumer_utilisation':
if name == 'consumer_utilisation':
if value is None:
value = 0
self.dispatch_values(value, vhost, plugin, plugin_instance, name)
Expand All @@ -293,7 +295,7 @@ def dispatch_exchanges(self, vhost_name):
"""
collectd.debug("Dispatching exchange data for {0}".format(vhost_name))
stats = self.rabbit.get_exchange_stats(vhost_name=vhost_name)
for exchange_name, value in stats.iteritems():
for exchange_name, value in stats.items():
self.dispatch_message_stats(value, vhost_name, 'exchanges',
exchange_name)

Expand All @@ -303,7 +305,7 @@ def dispatch_queues(self, vhost_name):
"""
collectd.debug("Dispatching queue data for {0}".format(vhost_name))
stats = self.rabbit.get_queue_stats(vhost_name=vhost_name)
for queue_name, value in stats.iteritems():
for queue_name, value in stats.items():
self.dispatch_message_stats(value, vhost_name, 'queues',
queue_name)
self.dispatch_queue_stats(value, vhost_name, 'queues',
Expand Down
21 changes: 11 additions & 10 deletions collectd_rabbitmq/rabbit.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import collectd
import json
import ssl
import urllib
import urllib2
import urllib.request
import urllib.parse
import urllib.error


class RabbitMQStats(object):
Expand All @@ -43,7 +44,7 @@ def get_names(items):
for item in items:
name = item.get('name', None)
if name:
name = urllib.quote(name, '')
name = urllib.parse.quote(name, '')
names.append(name)
return names

Expand All @@ -58,28 +59,28 @@ def get_info(self, *args):
ctx.options |= ssl.OP_NO_SSLv3
ctx.verify_mode = ssl.CERT_NONE
ctx.check_hostname = False
handlers.append(urllib2.HTTPSHandler(context=ctx))
handlers.append(urllib.request.HTTPSHandler(context=ctx))

url = "{0}/{1}".format(self.api, '/'.join(args))
collectd.debug("Getting info for %s" % url)

auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm=self.config.auth.realm,
uri=self.api,
user=self.config.auth.username,
passwd=self.config.auth.password)

handlers.append(auth_handler)

opener = urllib2.build_opener(*handlers)
urllib2.install_opener(opener)
opener = urllib.request.build_opener(*handlers)
urllib.request.install_opener(opener)

try:
info = urllib2.urlopen(url)
except urllib2.HTTPError as http_error:
info = urllib.request.urlopen(url)
except urllib.error.HTTPError as http_error:
collectd.error("HTTP Error: %s" % http_error)
return None
except urllib2.URLError as url_error:
except urllib.error.URLError as url_error:
collectd.error("URL Error: %s" % url_error)
return None
except ValueError as value_error:
Expand Down
2 changes: 1 addition & 1 deletion collectd_rabbitmq/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
""" Module that contains utility classes and functions """

import re
from urlparse import urlparse
from urllib.parse import urlparse


class Auth(object):
Expand Down
14 changes: 7 additions & 7 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
bumpversion==0.5.3
wheel==0.23.0
watchdog==0.8.3
flake8==2.4.1
tox==2.1.1
coverage==4.0
cryptography==1.3.2
bumpversion
wheel
watchdog
flake8
tox
coverage
cryptography
PyYAML>=4.2b1

8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@
'Intended Audience :: Developers',
'License :: OSI Approved :: Apache Software License',
'Natural Language :: English',
"Programming Language :: Python :: 2",
'Programming Language :: Python :: 2.7',
"Programming Language :: Python :: 3",
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
],
test_suite='tests',
tests_require=test_requirements,
Expand Down
2 changes: 1 addition & 1 deletion tests/collectd.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,5 @@ def test_loggable(msg):
"""
Ensure that logging messages are string to statisfy collectd.
"""
if not isinstance(msg, basestring):
if not isinstance(msg, str):
raise TypeError("Collectd requires that messages be strings")
Loading

0 comments on commit dfedf14

Please sign in to comment.