mirror of
https://github.com/searxng/searxng.git
synced 2025-12-22 19:50:00 +00:00
[mod] addition of various type hints / tbc
- pyright configuration [1]_ - stub files: types-lxml [2]_ - addition of various type hints - enable use of new type system features on older Python versions [3]_ - ``.tool-versions`` - set python to lowest version we support (3.10.18) [4]_: Older versions typically lack some typing features found in newer Python versions. Therefore, for local type checking (before commit), it is necessary to use the older Python interpreter. .. [1] https://docs.basedpyright.com/v1.20.0/configuration/config-files/ .. [2] https://pypi.org/project/types-lxml/ .. [3] https://typing-extensions.readthedocs.io/en/latest/# .. [4] https://mise.jdx.dev/configuration.html#tool-versions Signed-off-by: Markus Heiser <markus.heiser@darmarit.de> Format: reST
This commit is contained in:
committed by
Markus Heiser
parent
09500459fe
commit
57b9673efb
@@ -1,28 +1,29 @@
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
# pylint: disable=missing-module-docstring, cyclic-import
|
||||
from __future__ import annotations
|
||||
|
||||
import typing as t
|
||||
import sys
|
||||
import os
|
||||
from os.path import dirname, abspath
|
||||
|
||||
import logging
|
||||
|
||||
import searx.unixthreadname
|
||||
import searx.settings_loader
|
||||
from searx.settings_defaults import SCHEMA, apply_schema
|
||||
import searx.unixthreadname # pylint: disable=unused-import
|
||||
|
||||
# Debug
|
||||
LOG_FORMAT_DEBUG = '%(levelname)-7s %(name)-30.30s: %(message)s'
|
||||
LOG_FORMAT_DEBUG: str = '%(levelname)-7s %(name)-30.30s: %(message)s'
|
||||
|
||||
# Production
|
||||
LOG_FORMAT_PROD = '%(asctime)-15s %(levelname)s:%(name)s: %(message)s'
|
||||
LOG_FORMAT_PROD: str = '%(asctime)-15s %(levelname)s:%(name)s: %(message)s'
|
||||
LOG_LEVEL_PROD = logging.WARNING
|
||||
|
||||
searx_dir = abspath(dirname(__file__))
|
||||
searx_parent_dir = abspath(dirname(dirname(__file__)))
|
||||
searx_dir: str = abspath(dirname(__file__))
|
||||
searx_parent_dir: str = abspath(dirname(dirname(__file__)))
|
||||
|
||||
settings = {}
|
||||
sxng_debug = False
|
||||
settings: dict[str, t.Any] = {}
|
||||
|
||||
sxng_debug: bool = False
|
||||
logger = logging.getLogger('searx')
|
||||
|
||||
_unset = object()
|
||||
@@ -33,9 +34,13 @@ def init_settings():
|
||||
``logger`` from ``SEARXNG_SETTINGS_PATH``.
|
||||
"""
|
||||
|
||||
# pylint: disable=import-outside-toplevel
|
||||
from searx import settings_loader
|
||||
from searx.settings_defaults import SCHEMA, apply_schema
|
||||
|
||||
global settings, sxng_debug # pylint: disable=global-variable-not-assigned
|
||||
|
||||
cfg, msg = searx.settings_loader.load_settings(load_user_settings=True)
|
||||
cfg, msg = settings_loader.load_settings(load_user_settings=True)
|
||||
cfg = cfg or {}
|
||||
apply_schema(cfg, SCHEMA, [])
|
||||
|
||||
@@ -52,7 +57,7 @@ def init_settings():
|
||||
logger.info(msg)
|
||||
|
||||
# log max_request_timeout
|
||||
max_request_timeout = settings['outgoing']['max_request_timeout']
|
||||
max_request_timeout: int | None = settings['outgoing']['max_request_timeout']
|
||||
if max_request_timeout is None:
|
||||
logger.info('max_request_timeout=%s', repr(max_request_timeout))
|
||||
else:
|
||||
@@ -66,22 +71,22 @@ def init_settings():
|
||||
)
|
||||
|
||||
|
||||
def get_setting(name, default=_unset):
|
||||
def get_setting(name: str, default: t.Any = _unset) -> t.Any:
|
||||
"""Returns the value to which ``name`` point. If there is no such name in the
|
||||
settings and the ``default`` is unset, a :py:obj:`KeyError` is raised.
|
||||
|
||||
"""
|
||||
value = settings
|
||||
value: dict[str, t.Any] = settings
|
||||
for a in name.split('.'):
|
||||
if isinstance(value, dict):
|
||||
value = value.get(a, _unset)
|
||||
else:
|
||||
value = _unset
|
||||
value = _unset # type: ignore
|
||||
|
||||
if value is _unset:
|
||||
if default is _unset:
|
||||
raise KeyError(name)
|
||||
value = default
|
||||
value = default # type: ignore
|
||||
break
|
||||
|
||||
return value
|
||||
@@ -119,9 +124,14 @@ def _logging_config_debug():
|
||||
'programname': {'color': 'cyan'},
|
||||
'username': {'color': 'yellow'},
|
||||
}
|
||||
coloredlogs.install(level=log_level, level_styles=level_styles, field_styles=field_styles, fmt=LOG_FORMAT_DEBUG)
|
||||
coloredlogs.install( # type: ignore
|
||||
level=log_level,
|
||||
level_styles=level_styles,
|
||||
field_styles=field_styles,
|
||||
fmt=LOG_FORMAT_DEBUG,
|
||||
)
|
||||
else:
|
||||
logging.basicConfig(level=logging.getLevelName(log_level), format=LOG_FORMAT_DEBUG)
|
||||
logging.basicConfig(level=getattr(logging, log_level, "ERROR"), format=LOG_FORMAT_DEBUG)
|
||||
|
||||
|
||||
init_settings()
|
||||
|
||||
Reference in New Issue
Block a user