chore: Remove python 2.7 support and various cleanup (#179)

Co-authored-by: Bastien Vallet <b.vallet@criteo.com>
This commit is contained in:
Bastien Vallet 2020-06-08 12:09:24 +02:00 committed by GitHub
parent afb17c76bf
commit 778b00758e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 38 additions and 69 deletions

1
.gitignore vendored
View File

@ -8,6 +8,7 @@ __pycache__/
# Distribution / packaging
.Python
env/
.venv/
build/
develop-eggs/
dist/

View File

@ -1,8 +1,10 @@
language: python
python:
- 2.7
- 3.5
- 3.6
- pypy
- 3.7
- 3.8
- pypy3
install:
- pip install tox-travis
- pip install pytest-cov coveralls

View File

@ -9,8 +9,7 @@ implemented Google Translate API. This uses the `Google Translate Ajax
API <https://translate.google.com>`__ to make calls to such methods as
detect and translate.
Compatible with Python 2.7+ and 3.4+. (Note: Python 2 support will be dropped in the
next major release.)
Compatible with Python 3.5+.
For details refer to the `API
Documentation <https://py-googletrans.readthedocs.io/en/latest>`__.

View File

@ -4,4 +4,4 @@ __version__ = '2.4.1'
from googletrans.client import Translator
from googletrans.constants import LANGCODES, LANGUAGES
from googletrans.constants import LANGCODES, LANGUAGES # noqa

View File

@ -9,7 +9,6 @@ import random
from googletrans import urls, utils
from googletrans.adapters import TimeoutAdapter
from googletrans.compat import PY3
from googletrans.gtoken import TokenAcquirer
from googletrans.constants import DEFAULT_USER_AGENT, LANGCODES, LANGUAGES, SPECIAL_CASES
from googletrans.models import Translated, Detected
@ -18,7 +17,7 @@ from googletrans.models import Translated, Detected
EXCLUDES = ('en', 'ca', 'fr')
class Translator(object):
class Translator:
"""Google Translate ajax API implementation class
You have to create an instance of Translator to use this API
@ -69,9 +68,6 @@ class Translator(object):
return random.choice(self.service_urls)
def _translate(self, text, dest, src, override):
if not PY3 and isinstance(text, str): # pragma: nocover
text = text.decode('utf-8')
token = self.token_acquirer.do(text)
params = utils.build_params(query=text, src=src, dest=dest,
token=token, override=override)
@ -189,20 +185,10 @@ class Translator(object):
pron = data[0][1][-2]
except Exception: # pragma: nocover
pass
if not PY3 and isinstance(pron, unicode) and isinstance(origin, str): # pragma: nocover
origin = origin.decode('utf-8')
if dest in EXCLUDES and pron == origin:
pron = translated
# for python 2.x compatbillity
if not PY3: # pragma: nocover
if isinstance(src, str):
src = src.decode('utf-8')
if isinstance(dest, str):
dest = dest.decode('utf-8')
if isinstance(translated, str):
translated = translated.decode('utf-8')
# put final values into a new Translated object
result = Translated(src=src, dest=dest, origin=origin,
text=translated, pronunciation=pron, extra_data=extra_data)

View File

@ -1,11 +0,0 @@
# -*- coding: utf-8 -*-
import sys
try: # pragma: nocover
from urllib.parse import quote
except: # pragma: nocover
from urllib import quote
PY3 = sys.version_info > (3, )
unicode = str if PY3 else unicode

View File

@ -42,6 +42,7 @@ LANGUAGES = {
'ha': 'hausa',
'haw': 'hawaiian',
'iw': 'hebrew',
'he': 'hebrew',
'hi': 'hindi',
'hmn': 'hmong',
'hu': 'hungarian',
@ -109,8 +110,6 @@ LANGUAGES = {
'yi': 'yiddish',
'yo': 'yoruba',
'zu': 'zulu',
'fil': 'Filipino',
'he': 'Hebrew'
}
LANGCODES = dict(map(reversed, LANGUAGES.items()))

View File

@ -6,13 +6,10 @@ import time
import requests
from googletrans.compat import PY3
from googletrans.compat import unicode
from googletrans.utils import rshift
class TokenAcquirer(object):
class TokenAcquirer:
"""Google Translate API token generator
translate.google.com uses a token to authorize the requests. If you are
@ -62,12 +59,9 @@ class TokenAcquirer(object):
return
# this will be the same as python code after stripping out a reserved word 'var'
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '')
code = self.RE_TKK.search(r.text).group(1).replace('var ', '')
# unescape special ascii characters such like a \x3d(=)
if PY3: # pragma: no cover
code = code.encode().decode('unicode-escape')
else: # pragma: no cover
code = code.decode('string_escape')
if code:
tree = ast.parse(code)

View File

@ -1,4 +1,4 @@
class Translated(object):
class Translated:
"""Translate result object
:param src: source langauge (default: auto)
@ -19,13 +19,17 @@ class Translated(object):
return self.__unicode__()
def __unicode__(self): # pragma: nocover
return u'Translated(src={src}, dest={dest}, text={text}, pronunciation={pronunciation}, ' \
return (
u'Translated(src={src}, dest={dest}, text={text}, pronunciation={pronunciation}, '
u'extra_data={extra_data})'.format(
src=self.src, dest=self.dest, text=self.text, pronunciation=self.pronunciation,
extra_data='"' + repr(self.extra_data)[:10] + '..."')
src=self.src, dest=self.dest, text=self.text,
pronunciation=self.pronunciation,
extra_data='"' + repr(self.extra_data)[:10] + '..."'
)
)
class Detected(object):
class Detected:
"""Language detection result object
:param lang: detected language

View File

@ -1,5 +1,4 @@
"""A conversion module for googletrans"""
from __future__ import print_function
import re
import json

View File

@ -1 +0,0 @@
requests==2.13.0

View File

@ -52,11 +52,10 @@ def install():
'Operating System :: MacOS :: MacOS X',
'Topic :: Education',
'Programming Language :: Python',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'],
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8'],
packages=find_packages(exclude=['docs', 'tests']),
keywords='google translate translator',
install_requires=[

View File

@ -1,11 +1,9 @@
[tox]
envlist = py27,py36,pypy
envlist = py35,py36,py37,py38,pypy3
[testenv]
deps=
-r{toxinidir}/requirements.txt
pytest
pytest-capturelog
pytest-cov
commands=
py.test --cov-report= --cov={envsitepackagesdir}/googletrans {posargs:}