.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
__init__.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2# pylint: disable=missing-module-docstring
3
4import sys
5from os import listdir
6from os.path import realpath, dirname, join, isdir
7from collections import defaultdict
8
9from searx.utils import load_module
10
11answerers_dir = dirname(realpath(__file__))
12
13
15 answerers = [] # pylint: disable=redefined-outer-name
16
17 for filename in listdir(answerers_dir):
18 if not isdir(join(answerers_dir, filename)) or filename.startswith('_'):
19 continue
20 module = load_module('answerer.py', join(answerers_dir, filename))
21 if not hasattr(module, 'keywords') or not isinstance(module.keywords, tuple) or not module.keywords:
22 sys.exit(2)
23 answerers.append(module)
24 return answerers
25
26
27def get_answerers_by_keywords(answerers): # pylint:disable=redefined-outer-name
28 by_keyword = defaultdict(list)
29 for answerer in answerers:
30 for keyword in answerer.keywords:
31 for keyword in answerer.keywords:
32 by_keyword[keyword].append(answerer.answer)
33 return by_keyword
34
35
36def ask(query):
37 results = []
38 query_parts = list(filter(None, query.query.split()))
39
40 if not query_parts or query_parts[0] not in answerers_by_keywords:
41 return results
42
43 for answerer in answerers_by_keywords[query_parts[0]]:
44 result = answerer(query)
45 if result:
46 results.append(result)
47 return results
48
49
50answerers = load_answerers()
51answerers_by_keywords = get_answerers_by_keywords(answerers)
get_answerers_by_keywords(answerers)
Definition __init__.py:27