summaryrefslogtreecommitdiff
path: root/searx/webutils.py
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2022-01-04 13:39:06 +0100
committerMartin Fischer <martin@push-f.com>2022-01-05 11:03:44 +0100
commit3dd534e5c0017670a1e8ed12acfdfead4f2bcd0f (patch)
tree824bc78f90dd226c9a0cdaed2fdd02300d8fef7a /searx/webutils.py
parentd01e8aa8ccee9b3f1a8705cd4b0c67c012d0b06c (diff)
downloadsearxng-3dd534e5c0017670a1e8ed12acfdfead4f2bcd0f.tar.gz
searxng-3dd534e5c0017670a1e8ed12acfdfead4f2bcd0f.zip
[refactor] make group_engines_in_tab more readable
Diffstat (limited to 'searx/webutils.py')
-rw-r--r--searx/webutils.py29
1 files changed, 15 insertions, 14 deletions
diff --git a/searx/webutils.py b/searx/webutils.py
index b99307600..4c3073aac 100644
--- a/searx/webutils.py
+++ b/searx/webutils.py
@@ -6,12 +6,13 @@ import hmac
import re
import inspect
import itertools
+from typing import Iterable, List, Tuple
from io import StringIO
from codecs import getincrementalencoder
from searx import logger, settings
-from searx.engines import OTHER_CATEGORY
+from searx.engines import Engine, OTHER_CATEGORY
VALID_LANGUAGE_CODE = re.compile(r'^[a-z]{2,3}(-[a-zA-Z]{2})?$')
@@ -141,21 +142,21 @@ def is_flask_run_cmdline():
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 group_engines_in_tab(engines: Iterable[Engine]) -> List[Tuple[str, Iterable[Engine]]]:
+ """Groups an Iterable of engines by their first non tab category"""
def get_group(eng):
non_tab_categories = [c for c in eng.categories if c not in settings['categories_as_tabs'] + [OTHER_CATEGORY]]
return non_tab_categories[0] if len(non_tab_categories) > 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,
- )
- ]
+ groups = itertools.groupby(sorted(engines, key=get_group), get_group)
+
+ def group_sort_key(group):
+ return (group[0] == DEFAULT_GROUP_NAME, group[0].lower())
+
+ sorted_groups = sorted(((name, list(engines)) for name, engines in groups), key=group_sort_key)
+
+ def engine_sort_key(engine):
+ return (engine.about.get('language', ''), engine.name)
+
+ return [(groupname, sorted(engines, key=engine_sort_key)) for groupname, engines in sorted_groups]