Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to Python 3 #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.error
import urllib.parse
import urllib.request

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
23 changes: 12 additions & 11 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.error
import urllib.parse
import urllib.request


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 Expand Up @@ -183,7 +184,7 @@ def get_stats(self, stat_type, stat_name, vhost_name):
's' if not stat_name else '',
vhost_name))

if stat_type not in('exchange', 'queue'):
if stat_type not in ('exchange', 'queue'):
raise ValueError("Unsupported stat type {0}".format(stat_type))
stat_name_func = getattr(self, 'get_{0}_names'.format(stat_type))
if not vhost_name:
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
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Mock(MagicMock):
@classmethod
def __getattr__(cls, name):
return MagicMock()


MOCK_MODULES = ['collectd']

sys.modules.update(('collectd', Mock()) for mod_name in MOCK_MODULES)
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

3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ tag = True
[wheel]
universal = 1

[flake8]
exclude = .git,__pycache__,vagrant,.tox
ignore = H404,H405,H301,H101,W504
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 test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
hacking>=0.5.6,<0.8
hacking>=0.5.6
discover
fixtures
python-subunit
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