.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
babel_extract.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""This module implements the :origin:`searxng_msg <babel.cfg>` extractor to
3extract messages from:
4
5- :origin:`searx/searxng.msg`
6
7The ``searxng.msg`` files are selected by Babel_, see Babel's configuration in
8:origin:`babel.cfg`::
9
10 searxng_msg = searx.babel_extract.extract
11 ...
12 [searxng_msg: **/searxng.msg]
13
14A ``searxng.msg`` file is a python file that is *executed* by the
15:py:obj:`extract` function. Additional ``searxng.msg`` files can be added by:
16
171. Adding a ``searxng.msg`` file in one of the SearXNG python packages and
182. implement a method in :py:obj:`extract` that yields messages from this file.
19
20.. _Babel: https://babel.pocoo.org/en/latest/index.html
21
22"""
23
24from os import path
25
26SEARXNG_MSG_FILE = "searxng.msg"
27_MSG_FILES = [path.join(path.dirname(__file__), SEARXNG_MSG_FILE)]
28
29
31 # pylint: disable=unused-argument
32 fileobj,
33 keywords,
34 comment_tags,
35 options,
36):
37 """Extract messages from ``searxng.msg`` files by a custom extractor_.
38
39 .. _extractor:
40 https://babel.pocoo.org/en/latest/messages.html#writing-extraction-methods
41 """
42 if fileobj.name not in _MSG_FILES:
43 raise RuntimeError("don't know how to extract messages from %s" % fileobj.name)
44
45 namespace = {}
46 exec(fileobj.read(), {}, namespace) # pylint: disable=exec-used
47
48 for name in namespace['__all__']:
49 for k, v in namespace[name].items():
50 yield 0, '_', v, ["%s['%s']" % (name, k)]
extract(fileobj, keywords, comment_tags, options)