Skip to content

Commit

Permalink
Python3 support with six
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Sichny committed Jun 5, 2015
1 parent 1647d79 commit 82279d6
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 21 deletions.
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ TODO
====

* Create asynchronous bindings that can be used with Twisted, Tornado, etc.
* Add support for Python 3

License
-------
Expand Down
4 changes: 2 additions & 2 deletions ari/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import ari.client
import swaggerpy.http_client
import urlparse
import six.moves.urllib as urllib

Client = client.Client

Expand All @@ -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)
7 changes: 3 additions & 4 deletions ari/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import json
import logging
import urlparse
import six.moves.urllib as urllib
import swaggerpy.client

from ari.model import *
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

7 changes: 4 additions & 3 deletions ari_test/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions ari_test/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import httpretty
import os
import unittest
import urlparse
import six.moves.urllib as urllib
import ari
import requests

Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions examples/bridge_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# Copyright (c) 2013, Digium, Inc.
#

from __future__ import print_function
import ari

client = ari.connect('http://localhost:8088/', 'hey', 'peekaboo')
Expand All @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions examples/cleanup_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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")
5 changes: 3 additions & 2 deletions examples/originate_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#
import requests

from __future__ import print_function
import ari

from requests import HTTPError
Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion examples/playback_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# Copyright (c) 2013, Digium, Inc.
#

from __future__ import print_function
import ari
import sys

Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
"Programming Language :: Python",
],
tests_require=["coverage", "httpretty", "nose", "tissue"],
install_requires=["swaggerpy"],
install_requires=["swaggerpy", "six"],
)

0 comments on commit 82279d6

Please sign in to comment.