.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
__main__.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
5import io
6import os
7import argparse
8import logging
9
10import searx.search
12from searx.search import PROCESSORS
13from searx.engines import engine_shortcuts
14
15
16# configure logging
17root = logging.getLogger()
18handler = logging.StreamHandler(sys.stdout)
19for h in root.handlers:
20 root.removeHandler(h)
21root.addHandler(handler)
22
23# color only for a valid terminal
24if sys.stdout.isatty() and os.environ.get('TERM') not in ['dumb', 'unknown']:
25 RESET_SEQ = "\033[0m"
26 COLOR_SEQ = "\033[1;%dm"
27 BOLD_SEQ = "\033[1m"
28 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = map(lambda i: COLOR_SEQ % (30 + i), range(8))
29else:
30 RESET_SEQ = ""
31 COLOR_SEQ = ""
32 BOLD_SEQ = ""
33 BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = "", "", "", "", "", "", "", ""
34
35# equivalent of 'python -u' (unbuffered stdout, stderr)
36stdout = io.TextIOWrapper(
37 # pylint: disable=consider-using-with
38 open(sys.stdout.fileno(), 'wb', 0),
39 write_through=True,
40)
41stderr = io.TextIOWrapper(
42 # pylint: disable=consider-using-with
43 open(sys.stderr.fileno(), 'wb', 0),
44 write_through=True,
45)
46
47
48# iterator of processors
49def iter_processor(engine_name_list):
50 if len(engine_name_list) > 0:
51 for name in engine_name_list:
52 name = engine_shortcuts.get(name, name)
53 processor = PROCESSORS.get(name)
54 if processor is not None:
55 yield name, processor
56 else:
57 stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}{RED}Engine does not exist{RESET_SEQ}\n')
58 else:
59 for name, processor in searx.search.PROCESSORS.items():
60 yield name, processor
61
62
63# actual check & display
64def run(engine_name_list, verbose):
66 name_checker_list = []
67 for name, processor in iter_processor(engine_name_list):
68 stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}Checking\n')
69 if not sys.stdout.isatty():
70 stderr.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}Checking\n')
71 checker = searx.search.checker.Checker(processor)
72 checker.run()
73 name_checker_list.append((name, checker))
74
75 stdout.write(f'\n== {BOLD_SEQ}Results{RESET_SEQ} ' + '=' * 70 + '\n')
76 for name, checker in name_checker_list:
77 if checker.test_results.successful:
78 stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}{GREEN}OK{RESET_SEQ}\n')
79 if verbose:
80 stdout.write(f' {"found languages":15}: {" ".join(sorted(list(checker.test_results.languages)))}\n')
81 else:
82 stdout.write(f'{BOLD_SEQ}Engine {name:30}{RESET_SEQ}{RESET_SEQ}{RED}Error{RESET_SEQ}')
83 if not verbose:
84 errors = [test_name + ': ' + error for test_name, error in checker.test_results]
85 stdout.write(f'{RED}Error {str(errors)}{RESET_SEQ}\n')
86 else:
87 stdout.write('\n')
88 stdout.write(f' {"found languages":15}: {" ".join(sorted(list(checker.test_results.languages)))}\n')
89 for test_name, logs in checker.test_results.logs.items():
90 for log in logs:
91 log = map(lambda l: l if isinstance(l, str) else repr(l), log)
92 stdout.write(f' {test_name:15}: {RED}{" ".join(log)}{RESET_SEQ}\n')
93
94
95# call by setup.py
96def main():
97 parser = argparse.ArgumentParser(description='Check searx engines.')
98 parser.add_argument(
99 'engine_name_list',
100 metavar='engine name',
101 type=str,
102 nargs='*',
103 help='engines name or shortcut list. Empty for all engines.',
104 )
105 parser.add_argument(
106 '--verbose',
107 '-v',
108 action='store_true',
109 dest='verbose',
110 help='Display details about the test results',
111 default=False,
112 )
113 args = parser.parse_args()
114 run(args.engine_name_list, args.verbose)
115
116
117if __name__ == '__main__':
118 main()
::1337x
Definition 1337x.py:1
run(engine_name_list, verbose)
Definition __main__.py:64
iter_processor(engine_name_list)
Definition __main__.py:49
initialize(settings_engines=None, enable_checker=False, check_network=False, enable_metrics=True)
Definition __init__.py:30