calculator.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Calculate mathematical expressions using :py:obj:`ast.parse` (mode="eval").
  3. """
  4. from __future__ import annotations
  5. import typing
  6. import ast
  7. import re
  8. import operator
  9. import multiprocessing
  10. import babel
  11. import babel.numbers
  12. from flask_babel import gettext
  13. from searx.result_types import EngineResults
  14. from searx.plugins import Plugin, PluginInfo
  15. if typing.TYPE_CHECKING:
  16. from searx.search import SearchWithPlugins
  17. from searx.extended_types import SXNG_Request
  18. from searx.plugins import PluginCfg
  19. class SXNGPlugin(Plugin):
  20. """Plugin converts strings to different hash digests. The results are
  21. displayed in area for the "answers".
  22. """
  23. id = "calculator"
  24. def __init__(self, plg_cfg: "PluginCfg") -> None:
  25. super().__init__(plg_cfg)
  26. self.info = PluginInfo(
  27. id=self.id,
  28. name=gettext("Basic Calculator"),
  29. description=gettext("Calculate mathematical expressions via the search bar"),
  30. preference_section="general",
  31. )
  32. def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults:
  33. results = EngineResults()
  34. # only show the result of the expression on the first page
  35. if search.search_query.pageno > 1:
  36. return results
  37. query = search.search_query.query
  38. # in order to avoid DoS attacks with long expressions, ignore long expressions
  39. if len(query) > 100:
  40. return results
  41. # replace commonly used math operators with their proper Python operator
  42. query = query.replace("x", "*").replace(":", "/")
  43. # use UI language
  44. ui_locale = babel.Locale.parse(request.preferences.get_value("locale"), sep="-")
  45. # parse the number system in a localized way
  46. def _decimal(match: re.Match) -> str:
  47. val = match.string[match.start() : match.end()]
  48. val = babel.numbers.parse_decimal(val, ui_locale, numbering_system="latn")
  49. return str(val)
  50. decimal = ui_locale.number_symbols["latn"]["decimal"]
  51. group = ui_locale.number_symbols["latn"]["group"]
  52. query = re.sub(f"[0-9]+[{decimal}|{group}][0-9]+[{decimal}|{group}]?[0-9]?", _decimal, query)
  53. # only numbers and math operators are accepted
  54. if any(str.isalpha(c) for c in query):
  55. return results
  56. # in python, powers are calculated via **
  57. query_py_formatted = query.replace("^", "**")
  58. # Prevent the runtime from being longer than 50 ms
  59. res = timeout_func(0.05, _eval_expr, query_py_formatted)
  60. if res is None or res == "":
  61. return results
  62. res = babel.numbers.format_decimal(res, locale=ui_locale)
  63. results.add(results.types.Answer(answer=f"{search.search_query.query} = {res}"))
  64. return results
  65. operators: dict[type, typing.Callable] = {
  66. ast.Add: operator.add,
  67. ast.Sub: operator.sub,
  68. ast.Mult: operator.mul,
  69. ast.Div: operator.truediv,
  70. ast.Pow: operator.pow,
  71. ast.BitXor: operator.xor,
  72. ast.USub: operator.neg,
  73. }
  74. # with multiprocessing.get_context("fork") we are ready for Py3.14 (by emulating
  75. # the old behavior "fork") but it will not solve the core problem of fork, nor
  76. # will it remove the deprecation warnings in py3.12 & py3.13. Issue is
  77. # ddiscussed here: https://github.com/searxng/searxng/issues/4159
  78. mp_fork = multiprocessing.get_context("fork")
  79. def _eval_expr(expr):
  80. """
  81. >>> _eval_expr('2^6')
  82. 64
  83. >>> _eval_expr('2**6')
  84. 64
  85. >>> _eval_expr('1 + 2*3**(4^5) / (6 + -7)')
  86. -5.0
  87. """
  88. try:
  89. return _eval(ast.parse(expr, mode='eval').body)
  90. except ZeroDivisionError:
  91. # This is undefined
  92. return ""
  93. def _eval(node):
  94. if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)):
  95. return node.value
  96. if isinstance(node, ast.BinOp):
  97. return operators[type(node.op)](_eval(node.left), _eval(node.right))
  98. if isinstance(node, ast.UnaryOp):
  99. return operators[type(node.op)](_eval(node.operand))
  100. raise TypeError(node)
  101. def handler(q: multiprocessing.Queue, func, args, **kwargs): # pylint:disable=invalid-name
  102. try:
  103. q.put(func(*args, **kwargs))
  104. except:
  105. q.put(None)
  106. raise
  107. def timeout_func(timeout, func, *args, **kwargs):
  108. que = mp_fork.Queue()
  109. p = mp_fork.Process(target=handler, args=(que, func, args), kwargs=kwargs)
  110. p.start()
  111. p.join(timeout=timeout)
  112. ret_val = None
  113. # pylint: disable=used-before-assignment,undefined-variable
  114. if not p.is_alive():
  115. ret_val = que.get()
  116. else:
  117. logger.debug("terminate function after timeout is exceeded") # type: ignore
  118. p.terminate()
  119. p.join()
  120. p.close()
  121. return ret_val