mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-21 15:31:00 +00:00
Fixed some ruff warnings: requests without timeout and naming not complying with PEP
This commit is contained in:
parent
bf18dcbcf9
commit
1c0fb597fb
7 changed files with 25 additions and 31 deletions
|
@ -87,7 +87,7 @@ class RemoteDatabase:
|
||||||
req_limit = self.cache.get(api_key)
|
req_limit = self.cache.get(api_key)
|
||||||
if req_limit is None:
|
if req_limit is None:
|
||||||
try:
|
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()
|
res = r.json()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Cannot authenticate API key: " + str(e))
|
print("Cannot authenticate API key: " + str(e))
|
||||||
|
|
|
@ -129,8 +129,8 @@ def create_app(args):
|
||||||
|
|
||||||
from libretranslate.language import load_languages
|
from libretranslate.language import load_languages
|
||||||
|
|
||||||
SWAGGER_URL = args.url_prefix + "/docs" # Swagger UI (w/o trailing '/')
|
swagger_url = args.url_prefix + "/docs" # Swagger UI (w/o trailing '/')
|
||||||
API_URL = args.url_prefix + "/spec"
|
api_url = args.url_prefix + "/spec"
|
||||||
|
|
||||||
bp = Blueprint('Main app', __name__)
|
bp = Blueprint('Main app', __name__)
|
||||||
|
|
||||||
|
@ -339,7 +339,7 @@ def create_app(args):
|
||||||
get_api_key_link=args.get_api_key_link,
|
get_api_key_link=args.get_api_key_link,
|
||||||
web_version=os.environ.get("LT_WEB") is not None,
|
web_version=os.environ.get("LT_WEB") is not None,
|
||||||
version=get_version(),
|
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)],
|
available_locales=[{'code': l['code'], 'name': _lazy(l['name'])} for l in get_available_locales(not args.debug)],
|
||||||
current_locale=get_locale(),
|
current_locale=get_locale(),
|
||||||
alternate_locales=get_alternate_locale_links()
|
alternate_locales=get_alternate_locale_links()
|
||||||
|
@ -792,7 +792,7 @@ def create_app(args):
|
||||||
checked_filepath = security.path_traversal_check(filepath, get_upload_dir())
|
checked_filepath = security.path_traversal_check(filepath, get_upload_dir())
|
||||||
if os.path.isfile(checked_filepath):
|
if os.path.isfile(checked_filepath):
|
||||||
filepath = checked_filepath
|
filepath = checked_filepath
|
||||||
except security.SuspiciousFileOperation:
|
except security.SuspiciousFileOperationError:
|
||||||
abort(400, description=_("Invalid filename"))
|
abort(400, description=_("Invalid filename"))
|
||||||
|
|
||||||
return_data = io.BytesIO()
|
return_data = io.BytesIO()
|
||||||
|
@ -1075,7 +1075,7 @@ def create_app(args):
|
||||||
swag["info"]["version"] = get_version()
|
swag["info"]["version"] = get_version()
|
||||||
swag["info"]["title"] = "LibreTranslate"
|
swag["info"]["title"] = "LibreTranslate"
|
||||||
|
|
||||||
@app.route(API_URL)
|
@app.route(api_url)
|
||||||
@limiter.exempt
|
@limiter.exempt
|
||||||
def spec():
|
def spec():
|
||||||
return jsonify(lazy_swag(swag))
|
return jsonify(lazy_swag(swag))
|
||||||
|
@ -1093,9 +1093,9 @@ def create_app(args):
|
||||||
app.jinja_env.globals.update(_e=gettext_escaped, _h=gettext_html)
|
app.jinja_env.globals.update(_e=gettext_escaped, _h=gettext_html)
|
||||||
|
|
||||||
# Call factory function to create our blueprint
|
# 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:
|
if args.url_prefix:
|
||||||
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)
|
app.register_blueprint(swaggerui_blueprint, url_prefix=swagger_url)
|
||||||
else:
|
else:
|
||||||
app.register_blueprint(swaggerui_blueprint)
|
app.register_blueprint(swaggerui_blueprint)
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
import pycld2 as cld2
|
import pycld2 as cld2
|
||||||
|
|
||||||
|
|
||||||
class UnknownLanguage(Exception):
|
class UnknownLanguageError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Language:
|
class Language:
|
||||||
|
@ -59,7 +59,7 @@ class Detector:
|
||||||
reliable, index, top_3_choices = cld2.detect(text, bestEffort=True)
|
reliable, index, top_3_choices = cld2.detect(text, bestEffort=True)
|
||||||
|
|
||||||
if not self.quiet and not reliable:
|
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.languages = [Language(x) for x in top_3_choices]
|
||||||
self.language = self.languages[0]
|
self.language = self.languages[0]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
from argostranslate import translate
|
from argostranslate import translate
|
||||||
|
|
||||||
from libretranslate.detect import Detector, UnknownLanguage
|
from libretranslate.detect import Detector, UnknownLanguageError
|
||||||
|
|
||||||
__languages = None
|
__languages = None
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ def detect_languages(text):
|
||||||
for i in range(len(d)):
|
for i in range(len(d)):
|
||||||
d[i].text_length = len(t)
|
d[i].text_length = len(t)
|
||||||
candidates.extend(d)
|
candidates.extend(d)
|
||||||
except UnknownLanguage:
|
except UnknownLanguageError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# total read bytes of the provided text
|
# total read bytes of the provided text
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
class SuspiciousFileOperation(Exception):
|
class SuspiciousFileOperationError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ def path_traversal_check(unsafe_path, known_safe_path):
|
||||||
unsafe_path = os.path.abspath(unsafe_path)
|
unsafe_path = os.path.abspath(unsafe_path)
|
||||||
|
|
||||||
if (os.path.commonprefix([known_safe_path, unsafe_path]) != known_safe_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
|
# Passes the check
|
||||||
return unsafe_path
|
return unsafe_path
|
|
@ -20,6 +20,7 @@ keywords = [
|
||||||
"Python",
|
"Python",
|
||||||
"Translate",
|
"Translate",
|
||||||
"Translation",
|
"Translation",
|
||||||
|
"API",
|
||||||
]
|
]
|
||||||
classifiers = [
|
classifiers = [
|
||||||
"Operating System :: OS Independent",
|
"Operating System :: OS Independent",
|
||||||
|
@ -64,9 +65,8 @@ ltmanage = "libretranslate.manage:manage"
|
||||||
test = [
|
test = [
|
||||||
"pytest >=7.2.0",
|
"pytest >=7.2.0",
|
||||||
"pytest-cov",
|
"pytest-cov",
|
||||||
"flake8",
|
"ruff ==0.0.277",
|
||||||
"types-requests",
|
"types-requests",
|
||||||
# "mypy >=1.4.1",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -86,16 +86,11 @@ features = [
|
||||||
|
|
||||||
[tool.hatch.envs.default.scripts]
|
[tool.hatch.envs.default.scripts]
|
||||||
dev = "python main.py {args}"
|
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 = [
|
fmt = [
|
||||||
"ruff libretranslate scripts --fix",
|
"ruff libretranslate scripts --fix",
|
||||||
]
|
]
|
||||||
test = [
|
test = [
|
||||||
# "fmt",
|
"fmt",
|
||||||
"pytest {args}",
|
"pytest {args}",
|
||||||
]
|
]
|
||||||
cov = [
|
cov = [
|
||||||
|
@ -124,7 +119,7 @@ addopts = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
# https://github.com/charliermarsh/ruff#supported-rules
|
# https://beta.ruff.rs/docs/rules
|
||||||
[tool.ruff]
|
[tool.ruff]
|
||||||
src = ["libretranslate", "scripts"]
|
src = ["libretranslate", "scripts"]
|
||||||
target-version = "py38"
|
target-version = "py38"
|
||||||
|
@ -155,8 +150,6 @@ select = [
|
||||||
]
|
]
|
||||||
|
|
||||||
ignore = [
|
ignore = [
|
||||||
# "E741", # From original flake8 ignore
|
|
||||||
# "B008", # do not perform function calls in argument defaults (required for FastAPI afaik)
|
|
||||||
"E501", # line too long
|
"E501", # line too long
|
||||||
"A003", # Class attribute is shadowing a python builtin
|
"A003", # Class attribute is shadowing a python builtin
|
||||||
"S101", # Use of `assert` detected
|
"S101", # Use of `assert` detected
|
||||||
|
|
|
@ -7,6 +7,7 @@ response = requests.post(
|
||||||
'q': 'Hello World!',
|
'q': 'Hello World!',
|
||||||
'source': 'en',
|
'source': 'en',
|
||||||
'target': 'en'
|
'target': 'en'
|
||||||
}
|
},
|
||||||
|
timeout=60
|
||||||
)
|
)
|
||||||
# if server unavailable then requests with raise exception and healthcheck will fail
|
# if server unavailable then requests with raise exception and healthcheck will fail
|
||||||
|
|
Loading…
Reference in a new issue