From 1c0fb597fb3cf8e1e249b7fc13047e9c1951a368 Mon Sep 17 00:00:00 2001 From: Vincent Emonet Date: Sun, 9 Jul 2023 12:38:03 +0200 Subject: [PATCH] Fixed some ruff warnings: requests without timeout and naming not complying with PEP --- libretranslate/api_keys.py | 2 +- libretranslate/app.py | 14 +++++++------- libretranslate/detect.py | 8 ++++---- libretranslate/language.py | 8 ++++---- libretranslate/security.py | 6 +++--- pyproject.toml | 15 ++++----------- scripts/healthcheck.py | 3 ++- 7 files changed, 25 insertions(+), 31 deletions(-) diff --git a/libretranslate/api_keys.py b/libretranslate/api_keys.py index 0583531..ea9e603 100644 --- a/libretranslate/api_keys.py +++ b/libretranslate/api_keys.py @@ -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)) diff --git a/libretranslate/app.py b/libretranslate/app.py index 77197f2..39ad1ec 100644 --- a/libretranslate/app.py +++ b/libretranslate/app.py @@ -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) diff --git a/libretranslate/detect.py b/libretranslate/detect.py index 8717658..b9f6f2e 100644 --- a/libretranslate/detect.py +++ b/libretranslate/detect.py @@ -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 \ No newline at end of file + return text diff --git a/libretranslate/language.py b/libretranslate/language.py index 68e3e48..75fd0a7 100644 --- a/libretranslate/language.py +++ b/libretranslate/language.py @@ -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] diff --git a/libretranslate/security.py b/libretranslate/security.py index 3c4023a..7163a83 100644 --- a/libretranslate/security.py +++ b/libretranslate/security.py @@ -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 \ No newline at end of file + return unsafe_path diff --git a/pyproject.toml b/pyproject.toml index e9eaed8..17b9fb1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 diff --git a/scripts/healthcheck.py b/scripts/healthcheck.py index 20eba19..07bcaa4 100644 --- a/scripts/healthcheck.py +++ b/scripts/healthcheck.py @@ -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