From 5bae039584ac4ca95a0677e0bed73efa138b14c9 Mon Sep 17 00:00:00 2001 From: Marco Bonetti Date: Fri, 19 Jul 2013 10:01:37 +0200 Subject: [PATCH 1/3] Python3 compatibility --- __init__.py | 54 ++++++++++++++++++++++++----------------------------- setup.py | 13 ++++++++----- 2 files changed, 32 insertions(+), 35 deletions(-) diff --git a/__init__.py b/__init__.py index 362f4c8..a00918a 100644 --- a/__init__.py +++ b/__init__.py @@ -12,16 +12,7 @@ __all__ = ['Translator', 'TranslateApiException'] -try: - import simplejson as json - from simplejson import JSONDecodeError -except ImportError: - import json - class JSONDecodeError(Exception): pass - # Ugly: No alternative because this exception class doesnt seem to be there - # in the standard python module -import urllib -import urllib2 +import requests import warnings import logging @@ -96,15 +87,16 @@ def get_access_token(self): :return: The access token to be used with subsequent requests """ - args = urllib.urlencode({ + args = { 'client_id': self.client_id, 'client_secret': self.client_secret, 'scope': self.scope, 'grant_type': self.grant_type - }) - response = json.loads(urllib.urlopen( - 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13', args - ).read()) + } + response = requests.post( + 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13', + data=args + ).json() self.logger.debug(response) @@ -121,18 +113,20 @@ def call(self, url, params): if not self.access_token: self.access_token = self.get_access_token() - request = urllib2.Request( - "%s?%s" % (url, urllib.urlencode(params)), + resp = requests.get( + "%s" % url, + params=params, headers={'Authorization': 'Bearer %s' % self.access_token} ) - response = urllib2.urlopen(request).read() - rv = json.loads(response.decode("UTF-8-sig")) + resp.encoding = 'UTF-8-sig' + rv = resp.json() + #rv = json.loads(response.decode("UTF-8-sig")) - if isinstance(rv, basestring) and \ + if isinstance(rv, str) and \ rv.startswith("ArgumentOutOfRangeException"): raise ArgumentOutOfRangeException(rv) - if isinstance(rv, basestring) and \ + if isinstance(rv, str) and \ rv.startswith("TranslateApiException"): raise TranslateApiException(rv) @@ -170,23 +164,23 @@ def translate_array(self, texts, to_lang, from_lang=None, **options): """Translates an array of text strings from one language to another. :param texts: A list containing texts for translation. - :param to_lang: A string representing the language code to + :param to_lang: A string representing the language code to translate the text into. - :param from_lang: A string representing the language code of the - translation text. If left None the response will include the + :param from_lang: A string representing the language code of the + translation text. If left None the response will include the result of language auto-detection. (Default: None) - :param options: A TranslateOptions element containing the values below. + :param options: A TranslateOptions element containing the values below. They are all optional and default to the most common settings. - Category: A string containing the category (domain) of the + Category: A string containing the category (domain) of the translation. Defaults to "general". - ContentType: The format of the text being translated. The - supported formats are "text/plain" and "text/html". Any + ContentType: The format of the text being translated. The + supported formats are "text/plain" and "text/html". Any HTML needs to be well-formed. - Uri: A string containing the content location of this + Uri: A string containing the content location of this translation. User: A string used to track the originator of the submission. - State: User state to help correlate request and response. The + State: User state to help correlate request and response. The same contents will be returned in the response. """ options = { diff --git a/setup.py b/setup.py index 9457099..b1e7aa0 100644 --- a/setup.py +++ b/setup.py @@ -2,10 +2,10 @@ """ Microsoft translator API - The Microsoft Translator services can be used in web or client - applications to perform language translation operations. The services - support users who are not familiar with the default language of a page or - application, or those desiring to communicate with people of a different + The Microsoft Translator services can be used in web or client + applications to perform language translation operations. The services + support users who are not familiar with the default language of a page or + application, or those desiring to communicate with people of a different language group. This module implements the AJAX API for the Microsoft Translator service. @@ -17,7 +17,7 @@ >>> print translator.translate("Hello", "pt") "Olá" - The documentation for the service can be obtained here: + The documentation for the service can be obtained here: http://msdn.microsoft.com/en-us/library/ff512423.aspx The project is hosted on GitHub where your could fork the project or report @@ -59,4 +59,7 @@ def read(fname): "Topic :: Utilities" ], test_suite = "microsofttranslator.test.test_all", + install_requires=[ + 'requests >= 1.2.3', + ] ) From e2e43a31100b89b4a5030ff41b46401294398bab Mon Sep 17 00:00:00 2001 From: Sharoon Thomas Date: Mon, 2 Jun 2014 10:35:58 +0530 Subject: [PATCH 2/3] Add python 3.3 to travis env --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 8e31262..b869767 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: python python: - "2.7" + - "3.4" install: - pip install flake8 - python setup.py install From 887615b659ac061f62d8a7a69e49e05f2b45e19f Mon Sep 17 00:00:00 2001 From: Sharoon Thomas Date: Mon, 2 Jun 2014 10:47:31 +0530 Subject: [PATCH 3/3] Add trove classifiers to indicate python3 support --- setup.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 3441c24..523aacf 100644 --- a/setup.py +++ b/setup.py @@ -51,13 +51,15 @@ def read(fname): url="http://openlabs.co.in/", include_package_data=True, classifiers=[ - "Development Status :: 4 - Beta", + "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Natural Language :: English", "Operating System :: OS Independent", "Topic :: Software Development :: Internationalization", - "Topic :: Utilities" + "Topic :: Utilities", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 2", ], test_suite="microsofttranslator.test.test_all", install_requires=[