From 82279d6f12bc7aacc9413a3a546f1da1f09daca1 Mon Sep 17 00:00:00 2001 From: Roman Sichny Date: Fri, 5 Jun 2015 11:35:40 -0400 Subject: [PATCH 1/2] Python3 support with six --- README.rst | 1 - ari/__init__.py | 4 ++-- ari/client.py | 7 +++---- ari_test/client_test.py | 7 ++++--- ari_test/utils.py | 4 ++-- examples/bridge_example.py | 5 +++-- examples/cleanup_example.py | 7 ++++--- examples/originate_example.py | 5 +++-- examples/playback_example.py | 3 ++- setup.py | 2 +- 10 files changed, 24 insertions(+), 21 deletions(-) diff --git a/README.rst b/README.rst index e718171..0814947 100644 --- a/README.rst +++ b/README.rst @@ -145,7 +145,6 @@ TODO ==== * Create asynchronous bindings that can be used with Twisted, Tornado, etc. - * Add support for Python 3 License ------- diff --git a/ari/__init__.py b/ari/__init__.py index caafe24..0dfcfa0 100644 --- a/ari/__init__.py +++ b/ari/__init__.py @@ -7,7 +7,7 @@ import ari.client import swaggerpy.http_client -import urlparse +import six.moves.urllib as urllib Client = client.Client @@ -20,7 +20,7 @@ def connect(base_url, username, password): :param password: ARI password. :return: """ - split = urlparse.urlsplit(base_url) + split = urllib.parse.urlsplit(base_url) http_client = swaggerpy.http_client.SynchronousHttpClient() http_client.set_basic_auth(split.hostname, username, password) return Client(base_url, http_client) diff --git a/ari/client.py b/ari/client.py index c1ce6e7..5c2e999 100644 --- a/ari/client.py +++ b/ari/client.py @@ -7,7 +7,7 @@ import json import logging -import urlparse +import six.moves.urllib as urllib import swaggerpy.client from ari.model import * @@ -23,7 +23,7 @@ class Client(object): """ def __init__(self, base_url, http_client): - url = urlparse.urljoin(base_url, "ari/api-docs/resources.json") + url = urllib.parse.urljoin(base_url, "ari/api-docs/resources.json") self.swagger = swaggerpy.client.SwaggerClient( url, http_client=http_client) @@ -192,7 +192,7 @@ def extract_objects(event, *args, **kwargs): # If there's only one field in the schema, just pass that along if len(obj_fields) == 1: if obj: - obj = obj.values()[0] + obj = list(obj.values())[0] else: obj = None event_cb(obj, event, *args, **kwargs) @@ -296,4 +296,3 @@ def on_sound_event(self, event_type, fn, *args, **kwargs): """ return self.on_object_event(event_type, fn, Sound, 'Sound', *args, **kwargs) - diff --git a/ari_test/client_test.py b/ari_test/client_test.py index 8ce8227..5259d92 100644 --- a/ari_test/client_test.py +++ b/ari_test/client_test.py @@ -5,7 +5,8 @@ import json import requests import unittest -import urllib +import six.moves.urllib as urllib +import codecs from ari_test.utils import AriTestCase @@ -19,9 +20,9 @@ # noinspection PyDocstring class ClientTest(AriTestCase): def test_docs(self): - fp = urllib.urlopen("http://ari.py/ari/api-docs/resources.json") + fp = urllib.request.urlopen("http://ari.py/ari/api-docs/resources.json") try: - actual = json.load(fp) + actual = json.load(codecs.getreader('utf-8')(fp)) self.assertEqual(self.BASE_URL, actual['basePath']) finally: fp.close() diff --git a/ari_test/utils.py b/ari_test/utils.py index 3eb49e7..710b6f3 100644 --- a/ari_test/utils.py +++ b/ari_test/utils.py @@ -3,7 +3,7 @@ import httpretty import os import unittest -import urlparse +import six.moves.urllib as urllib import ari import requests @@ -41,7 +41,7 @@ def build_url(cls, *args): """ url = cls.BASE_URL for arg in args: - url = urlparse.urljoin(url + '/', arg) + url = urllib.parse.urljoin(url + '/', arg) return url def serve_api(self): diff --git a/examples/bridge_example.py b/examples/bridge_example.py index 58cd1a9..f85443e 100644 --- a/examples/bridge_example.py +++ b/examples/bridge_example.py @@ -11,6 +11,7 @@ # Copyright (c) 2013, Digium, Inc. # +from __future__ import print_function import ari client = ari.connect('http://localhost:8088/', 'hey', 'peekaboo') @@ -22,10 +23,10 @@ b.json['bridge_type'] == 'holding'] if bridges: bridge = bridges[0] - print "Using bridge %s" % bridge.id + print("Using bridge %s" % bridge.id) else: bridge = client.bridges.create(type='holding') - print "Created bridge %s" % bridge.id + print("Created bridge %s" % bridge.id) def on_enter(bridge, ev): diff --git a/examples/cleanup_example.py b/examples/cleanup_example.py index 2b4a02d..bf1b237 100644 --- a/examples/cleanup_example.py +++ b/examples/cleanup_example.py @@ -7,10 +7,11 @@ # Copyright (c) 2013, Digium, Inc. # +from __future__ import print_function import ari import logging import sys -import thread +import six.moves._thread as thread logging.basicConfig() @@ -68,8 +69,8 @@ def run(): thr = thread.start_new_thread(run, ()) -print "Press enter to exit" +print("Press enter to exit") sys.stdin.readline() client.close() sync.acquire() -print "Application finished" +print("Application finished") diff --git a/examples/originate_example.py b/examples/originate_example.py index 77b7eba..2d98f13 100644 --- a/examples/originate_example.py +++ b/examples/originate_example.py @@ -9,6 +9,7 @@ # import requests +from __future__ import print_function import ari from requests import HTTPError @@ -24,10 +25,10 @@ if b.json['bridge_type'] == 'holding'] if bridges: holding_bridge = bridges[0] - print "Using bridge %s" % holding_bridge.id + print("Using bridge %s" % holding_bridge.id) else: holding_bridge = client.bridges.create(type='holding') - print "Created bridge %s" % holding_bridge.id + print("Created bridge %s" % holding_bridge.id) def safe_hangup(channel): diff --git a/examples/playback_example.py b/examples/playback_example.py index 0371945..9644bf9 100644 --- a/examples/playback_example.py +++ b/examples/playback_example.py @@ -10,6 +10,7 @@ # Copyright (c) 2013, Digium, Inc. # +from __future__ import print_function import ari import sys @@ -52,7 +53,7 @@ def on_dtmf(channel, event): playback.stop() channel.continueInDialplan() else: - print >> sys.stderr, "Unknown DTMF %s" % digit + print("Unknown DTMF %s" % digit, file=sys.stderr) channel.on_event('ChannelDtmfReceived', on_dtmf) diff --git a/setup.py b/setup.py index 22d68ce..fece052 100755 --- a/setup.py +++ b/setup.py @@ -28,5 +28,5 @@ "Programming Language :: Python", ], tests_require=["coverage", "httpretty", "nose", "tissue"], - install_requires=["swaggerpy"], + install_requires=["swaggerpy", "six"], ) From ce592475666167a19389f6b56aaf2c02be16348b Mon Sep 17 00:00:00 2001 From: Roman Sichny Date: Mon, 22 Jun 2015 11:23:16 +0300 Subject: [PATCH 2/2] setup.py: update to version 0.1.4 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index fece052..cee5d63 100755 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name="ari", - version="0.1.3", + version="0.1.4", license="BSD 3-Clause License", description="Library for accessing the Asterisk REST Interface", long_description=open(os.path.join(os.path.dirname(__file__),