1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-12-24 10:06:43 +02:00

Fixed some ruff warnings: requests without timeout and naming not complying with PEP

This commit is contained in:
Vincent Emonet 2023-07-09 12:38:03 +02:00
parent bf18dcbcf9
commit 1c0fb597fb
7 changed files with 25 additions and 31 deletions

View File

@ -87,7 +87,7 @@ class RemoteDatabase:
req_limit = self.cache.get(api_key)
if req_limit is None:
try:
r = requests.post(self.url, data={'api_key': api_key})
r = requests.post(self.url, data={'api_key': api_key}, timeout=60)
res = r.json()
except Exception as e:
print("Cannot authenticate API key: " + str(e))

View File

@ -129,8 +129,8 @@ def create_app(args):
from libretranslate.language import load_languages
SWAGGER_URL = args.url_prefix + "/docs" # Swagger UI (w/o trailing '/')
API_URL = args.url_prefix + "/spec"
swagger_url = args.url_prefix + "/docs" # Swagger UI (w/o trailing '/')
api_url = args.url_prefix + "/spec"
bp = Blueprint('Main app', __name__)
@ -339,7 +339,7 @@ def create_app(args):
get_api_key_link=args.get_api_key_link,
web_version=os.environ.get("LT_WEB") is not None,
version=get_version(),
swagger_url=SWAGGER_URL,
swagger_url=swagger_url,
available_locales=[{'code': l['code'], 'name': _lazy(l['name'])} for l in get_available_locales(not args.debug)],
current_locale=get_locale(),
alternate_locales=get_alternate_locale_links()
@ -792,7 +792,7 @@ def create_app(args):
checked_filepath = security.path_traversal_check(filepath, get_upload_dir())
if os.path.isfile(checked_filepath):
filepath = checked_filepath
except security.SuspiciousFileOperation:
except security.SuspiciousFileOperationError:
abort(400, description=_("Invalid filename"))
return_data = io.BytesIO()
@ -1075,7 +1075,7 @@ def create_app(args):
swag["info"]["version"] = get_version()
swag["info"]["title"] = "LibreTranslate"
@app.route(API_URL)
@app.route(api_url)
@limiter.exempt
def spec():
return jsonify(lazy_swag(swag))
@ -1093,9 +1093,9 @@ def create_app(args):
app.jinja_env.globals.update(_e=gettext_escaped, _h=gettext_html)
# Call factory function to create our blueprint
swaggerui_blueprint = get_swaggerui_blueprint(SWAGGER_URL, API_URL)
swaggerui_blueprint = get_swaggerui_blueprint(swagger_url, api_url)
if args.url_prefix:
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)
app.register_blueprint(swaggerui_blueprint, url_prefix=swagger_url)
else:
app.register_blueprint(swaggerui_blueprint)

View File

@ -3,7 +3,7 @@
import pycld2 as cld2
class UnknownLanguage(Exception):
class UnknownLanguageError(Exception):
pass
class Language:
@ -57,9 +57,9 @@ class Detector:
if not reliable:
self.reliable = False
reliable, index, top_3_choices = cld2.detect(text, bestEffort=True)
if not self.quiet and not reliable:
raise UnknownLanguage("Try passing a longer snippet of text")
raise UnknownLanguageError("Try passing a longer snippet of text")
self.languages = [Language(x) for x in top_3_choices]
self.language = self.languages[0]
@ -69,4 +69,4 @@ class Detector:
text = f"Prediction is reliable: {self.reliable}\n"
text += "\n".join([f"Language {i+1}: {str(l)}"
for i,l in enumerate(self.languages)])
return text
return text

View File

@ -1,7 +1,7 @@
from argostranslate import translate
from libretranslate.detect import Detector, UnknownLanguage
from libretranslate.detect import Detector, UnknownLanguageError
__languages = None
@ -29,7 +29,7 @@ def detect_languages(text):
for i in range(len(d)):
d[i].text_length = len(t)
candidates.extend(d)
except UnknownLanguage:
except UnknownLanguageError:
pass
# total read bytes of the provided text
@ -83,10 +83,10 @@ def improve_translation_formatting(source, translation, improve_punctuation=True
if not len(source):
return ""
if not len(translation):
return source
if improve_punctuation:
source_last_char = source[len(source) - 1]
translation_last_char = translation[len(translation) - 1]

View File

@ -1,7 +1,7 @@
import os
class SuspiciousFileOperation(Exception):
class SuspiciousFileOperationError(Exception):
pass
@ -10,7 +10,7 @@ def path_traversal_check(unsafe_path, known_safe_path):
unsafe_path = os.path.abspath(unsafe_path)
if (os.path.commonprefix([known_safe_path, unsafe_path]) != known_safe_path):
raise SuspiciousFileOperation(f"{unsafe_path} is not safe")
raise SuspiciousFileOperationError(f"{unsafe_path} is not safe")
# Passes the check
return unsafe_path
return unsafe_path

View File

@ -20,6 +20,7 @@ keywords = [
"Python",
"Translate",
"Translation",
"API",
]
classifiers = [
"Operating System :: OS Independent",
@ -64,9 +65,8 @@ ltmanage = "libretranslate.manage:manage"
test = [
"pytest >=7.2.0",
"pytest-cov",
"flake8",
"ruff ==0.0.277",
"types-requests",
# "mypy >=1.4.1",
]
@ -86,16 +86,11 @@ features = [
[tool.hatch.envs.default.scripts]
dev = "python main.py {args}"
lint = [
# "flake8 . --count --exit-zero --select=E9,F63,F7,F82 --show-source --statistics",
# "flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics",
"ruff libretranslate scripts",
]
fmt = [
"ruff libretranslate scripts --fix",
]
test = [
# "fmt",
"fmt",
"pytest {args}",
]
cov = [
@ -124,7 +119,7 @@ addopts = [
]
# https://github.com/charliermarsh/ruff#supported-rules
# https://beta.ruff.rs/docs/rules
[tool.ruff]
src = ["libretranslate", "scripts"]
target-version = "py38"
@ -155,8 +150,6 @@ select = [
]
ignore = [
# "E741", # From original flake8 ignore
# "B008", # do not perform function calls in argument defaults (required for FastAPI afaik)
"E501", # line too long
"A003", # Class attribute is shadowing a python builtin
"S101", # Use of `assert` detected

View File

@ -7,6 +7,7 @@ response = requests.post(
'q': 'Hello World!',
'source': 'en',
'target': 'en'
}
},
timeout=60
)
# if server unavailable then requests with raise exception and healthcheck will fail