Skip to content

Commit 1e61135

Browse files
author
Sharoon Thomas
committed
Merge pull request #9 from sharoonthomas/py3
Python 3 compatibility
2 parents 94dd692 + 887615b commit 1e61135

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: python
22
python:
33
- "2.7"
4+
- "3.4"
45
install:
56
- pip install flake8
67
- python setup.py install

__init__.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,10 @@
1414

1515
try:
1616
import simplejson as json
17-
from simplejson import JSONDecodeError
1817
except ImportError:
1918
import json
2019

21-
class JSONDecodeError(Exception):
22-
pass
23-
# Ugly: No alternative because this exception class doesnt seem to be there
24-
# in the standard python module
25-
import urllib
26-
import urllib2
20+
import requests
2721
import warnings
2822
import logging
2923

@@ -98,15 +92,16 @@ def get_access_token(self):
9892
9993
:return: The access token to be used with subsequent requests
10094
"""
101-
args = urllib.urlencode({
95+
args = {
10296
'client_id': self.client_id,
10397
'client_secret': self.client_secret,
10498
'scope': self.scope,
10599
'grant_type': self.grant_type
106-
})
107-
response = json.loads(urllib.urlopen(
108-
'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13', args
109-
).read())
100+
}
101+
response = requests.post(
102+
'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13',
103+
data=args
104+
).json()
110105

111106
self.logger.debug(response)
112107

@@ -123,18 +118,19 @@ def call(self, url, params):
123118
if not self.access_token:
124119
self.access_token = self.get_access_token()
125120

126-
request = urllib2.Request(
127-
"%s?%s" % (url, urllib.urlencode(params)),
121+
resp = requests.get(
122+
"%s" % url,
123+
params=params,
128124
headers={'Authorization': 'Bearer %s' % self.access_token}
129125
)
130-
response = urllib2.urlopen(request).read()
131-
rv = json.loads(response.decode("UTF-8-sig"))
126+
resp.encoding = 'UTF-8-sig'
127+
rv = resp.json()
132128

133-
if isinstance(rv, basestring) and \
129+
if isinstance(rv, str) and \
134130
rv.startswith("ArgumentOutOfRangeException"):
135131
raise ArgumentOutOfRangeException(rv)
136132

137-
if isinstance(rv, basestring) and \
133+
if isinstance(rv, str) and \
138134
rv.startswith("TranslateApiException"):
139135
raise TranslateApiException(rv)
140136

setup.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,18 @@ def read(fname):
5151
url="http://openlabs.co.in/",
5252
include_package_data=True,
5353
classifiers=[
54-
"Development Status :: 4 - Beta",
54+
"Development Status :: 5 - Production/Stable",
5555
"Intended Audience :: Developers",
5656
"License :: OSI Approved :: BSD License",
5757
"Natural Language :: English",
5858
"Operating System :: OS Independent",
5959
"Topic :: Software Development :: Internationalization",
60-
"Topic :: Utilities"
60+
"Topic :: Utilities",
61+
"Programming Language :: Python :: 3",
62+
"Programming Language :: Python :: 2",
6163
],
6264
test_suite="microsofttranslator.test.test_all",
65+
install_requires=[
66+
'requests >= 1.2.3',
67+
]
6368
)

0 commit comments

Comments
 (0)