babel_extract.py 1.5 KB

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