[mod] move group_engines_in_tab to searx.webutils

This commit is contained in:
Martin Fischer 2022-01-03 07:24:20 +01:00
parent 5d74bf3820
commit 1e195f5b95
4 changed files with 30 additions and 28 deletions

View file

@ -39,6 +39,7 @@ exclude_patterns = ['build-templates/*.rst']
import searx.engines import searx.engines
import searx.plugins import searx.plugins
import searx.webutils
searx.engines.load_engines(searx.settings['engines']) searx.engines.load_engines(searx.settings['engines'])
jinja_contexts = { jinja_contexts = {
@ -54,7 +55,7 @@ jinja_contexts = {
}, },
} }
jinja_filters = { jinja_filters = {
'group_engines_in_tab': searx.engines.group_engines_in_tab, 'group_engines_in_tab': searx.webutils.group_engines_in_tab,
} }
# Let the Jinja template in configured_engines.rst access documented_modules # Let the Jinja template in configured_engines.rst access documented_modules

View file

@ -13,7 +13,6 @@ usage::
import sys import sys
import copy import copy
import itertools
from os.path import realpath, dirname from os.path import realpath, dirname
from babel.localedata import locale_identifiers from babel.localedata import locale_identifiers
@ -267,26 +266,3 @@ def load_engines(engine_list):
if engine: if engine:
register_engine(engine) register_engine(engine)
return engines return engines
DEFAULT_GROUP_NAME = 'others'
def group_engines_in_tab(engines): # pylint: disable=redefined-outer-name
def engine_sort_key(engine):
return (engine.about.get('language', ''), engine.name)
def group_sort_key(group):
return (group[0] == DEFAULT_GROUP_NAME, group[0].lower())
def get_group(eng):
non_tab_engines = [c for c in eng.categories if c not in settings['categories_as_tabs'] + [OTHER_CATEGORY]]
return non_tab_engines[0] if len(non_tab_engines) > 0 else DEFAULT_GROUP_NAME
return [
(groupname, sorted(engines, key=engine_sort_key))
for groupname, engines in sorted(
((name, list(engines)) for name, engines in itertools.groupby(sorted(engines, key=get_group), get_group)),
key=group_sort_key,
)
]

View file

@ -60,11 +60,9 @@ from searx.settings_loader import get_default_settings_path
from searx.exceptions import SearxParameterException from searx.exceptions import SearxParameterException
from searx.engines import ( from searx.engines import (
OTHER_CATEGORY, OTHER_CATEGORY,
DEFAULT_GROUP_NAME,
categories, categories,
engines, engines,
engine_shortcuts, engine_shortcuts,
group_engines_in_tab,
) )
from searx.webutils import ( from searx.webutils import (
UnicodeWriter, UnicodeWriter,
@ -76,6 +74,8 @@ from searx.webutils import (
new_hmac, new_hmac,
is_hmac_of, is_hmac_of,
is_flask_run_cmdline, is_flask_run_cmdline,
DEFAULT_GROUP_NAME,
group_engines_in_tab,
) )
from searx.webadapter import ( from searx.webadapter import (
get_search_query_from_webapp, get_search_query_from_webapp,

View file

@ -5,11 +5,13 @@ import hashlib
import hmac import hmac
import re import re
import inspect import inspect
import itertools
from io import StringIO from io import StringIO
from codecs import getincrementalencoder from codecs import getincrementalencoder
from searx import logger from searx import logger, settings
from searx.engines import OTHER_CATEGORY
VALID_LANGUAGE_CODE = re.compile(r'^[a-z]{2,3}(-[a-zA-Z]{2})?$') VALID_LANGUAGE_CODE = re.compile(r'^[a-z]{2,3}(-[a-zA-Z]{2})?$')
@ -134,3 +136,26 @@ def is_flask_run_cmdline():
if len(frames) < 2: if len(frames) < 2:
return False return False
return frames[-2].filename.endswith('flask/cli.py') return frames[-2].filename.endswith('flask/cli.py')
DEFAULT_GROUP_NAME = 'others'
def group_engines_in_tab(engines):
def engine_sort_key(engine):
return (engine.about.get('language', ''), engine.name)
def group_sort_key(group):
return (group[0] == DEFAULT_GROUP_NAME, group[0].lower())
def get_group(eng):
non_tab_engines = [c for c in eng.categories if c not in settings['categories_as_tabs'] + [OTHER_CATEGORY]]
return non_tab_engines[0] if len(non_tab_engines) > 0 else DEFAULT_GROUP_NAME
return [
(groupname, sorted(engines, key=engine_sort_key))
for groupname, engines in sorted(
((name, list(engines)) for name, engines in itertools.groupby(sorted(engines, key=get_group), get_group)),
key=group_sort_key,
)
]