Skip to content

Commit

Permalink
Merge pull request #17 from sharoonthomas/flake8ify
Browse files Browse the repository at this point in the history
Flake8ify
  • Loading branch information
Sharoon Thomas committed Dec 16, 2014
2 parents 493405a + 0d8dcd0 commit 40a78e8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
27 changes: 15 additions & 12 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class Translator(object):
:param app_id: A string containing the Bing AppID. (Deprecated)
"""

def __init__(self, client_id, client_secret,
base_url = "http://api.microsofttranslator.com/V2/Ajax.svc"

def __init__(
self, client_id, client_secret,
scope="http://api.microsofttranslator.com",
grant_type="client_credentials", app_id=None, debug=False):
"""
Expand Down Expand Up @@ -113,14 +116,17 @@ def get_access_token(self):
)
return response['access_token']

def call(self, url, params):
"""Calls the given url with the params urlencoded
def call(self, path, params):
"""Calls the given path with the params urlencoded
:param path: The path of the API call being made
:param params: The parameters dictionary
"""
if not self.access_token:
self.access_token = self.get_access_token()

resp = requests.get(
"%s" % url,
"/".join([self.base_url, path]),
params=params,
headers={'Authorization': 'Bearer %s' % self.access_token}
)
Expand All @@ -139,10 +145,11 @@ def call(self, url, params):
rv.startswith(("ArgumentException: "
"The incoming token has expired")):
self.access_token = None
return self.call(url, params)
return self.call(path, params)
return rv

def translate(self, text, to_lang, from_lang=None,
def translate(
self, text, to_lang, from_lang=None,
content_type='text/plain', category='general'):
"""Translates a text string from one language to another.
Expand All @@ -166,9 +173,7 @@ def translate(self, text, to_lang, from_lang=None,
}
if from_lang is not None:
params['from'] = from_lang
return self.call(
"http://api.microsofttranslator.com/V2/Ajax.svc/Translate",
params)
return self.call("Translate", params)

def translate_array(self, texts, to_lang, from_lang=None, **options):
"""Translates an array of text strings from one language to another.
Expand Down Expand Up @@ -208,6 +213,4 @@ def translate_array(self, texts, to_lang, from_lang=None, **options):
if from_lang is not None:
params['from'] = from_lang

return self.call(
"http://api.microsofttranslator.com/V2/Ajax.svc/TranslateArray",
params)
return self.call("TranslateArray", params)
5 changes: 1 addition & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
[flake8]
exclude=.svn,CVS,.bzr,.hg,.git,__pycache__,build,dist,upload.py,doc,scripts,selenium*,proteus*,Flask*,pycountry*

ignore=E126,E131,E128

exclude=.svn,CVS,.bzr,.hg,.git,__pycache__,build,dist,upload.py,doc,scripts,*.egg
max-complexity=15
max-line-length=80

0 comments on commit 40a78e8

Please sign in to comment.