calculator.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """Calculate mathematical expressions using :py:obj:`ast.parse` (mode="eval")."""
  3. from __future__ import annotations
  4. import typing
  5. import ast
  6. import math
  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 timeout_func(self, timeout, func, *args, **kwargs):
  33. que = mp_fork.Queue()
  34. p = mp_fork.Process(target=handler, args=(que, func, args), kwargs=kwargs)
  35. p.start()
  36. p.join(timeout=timeout)
  37. ret_val = None
  38. # pylint: disable=used-before-assignment,undefined-variable
  39. if not p.is_alive():
  40. ret_val = que.get()
  41. else:
  42. self.log.debug("terminate function (%s: %s // %s) after timeout is exceeded", func.__name__, args, kwargs)
  43. p.terminate()
  44. p.join()
  45. p.close()
  46. return ret_val
  47. def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults:
  48. results = EngineResults()
  49. # only show the result of the expression on the first page
  50. if search.search_query.pageno > 1:
  51. return results
  52. query = search.search_query.query
  53. # in order to avoid DoS attacks with long expressions, ignore long expressions
  54. if len(query) > 100:
  55. return results
  56. # replace commonly used math operators with their proper Python operator
  57. query = query.replace("x", "*").replace(":", "/")
  58. # use UI language
  59. ui_locale = babel.Locale.parse(request.preferences.get_value("locale"), sep="-")
  60. # parse the number system in a localized way
  61. def _decimal(match: re.Match) -> str:
  62. val = match.string[match.start() : match.end()]
  63. val = babel.numbers.parse_decimal(val, ui_locale, numbering_system="latn")
  64. return str(val)
  65. decimal = ui_locale.number_symbols["latn"]["decimal"]
  66. group = ui_locale.number_symbols["latn"]["group"]
  67. query = re.sub(f"[0-9]+[{decimal}|{group}][0-9]+[{decimal}|{group}]?[0-9]?", _decimal, query)
  68. # in python, powers are calculated via **
  69. query_py_formatted = query.replace("^", "**")
  70. # Prevent the runtime from being longer than 50 ms
  71. res = self.timeout_func(0.05, _eval_expr, query_py_formatted)
  72. if res is None or res[0] == "":
  73. return results
  74. res, is_boolean = res
  75. if is_boolean:
  76. res = "True" if res != 0 else "False"
  77. else:
  78. res = babel.numbers.format_decimal(res, locale=ui_locale)
  79. results.add(results.types.Answer(answer=f"{search.search_query.query} = {res}"))
  80. return results
  81. def _compare(ops: list[ast.cmpop], values: list[int | float]) -> int:
  82. """
  83. 2 < 3 becomes ops=[ast.Lt] and values=[2,3]
  84. 2 < 3 <= 4 becomes ops=[ast.Lt, ast.LtE] and values=[2,3, 4]
  85. """
  86. for op, a, b in zip(ops, values, values[1:]): # pylint: disable=invalid-name
  87. if isinstance(op, ast.Eq) and a == b:
  88. continue
  89. if isinstance(op, ast.NotEq) and a != b:
  90. continue
  91. if isinstance(op, ast.Lt) and a < b:
  92. continue
  93. if isinstance(op, ast.LtE) and a <= b:
  94. continue
  95. if isinstance(op, ast.Gt) and a > b:
  96. continue
  97. if isinstance(op, ast.GtE) and a >= b:
  98. continue
  99. # Ignore impossible ops:
  100. # * ast.Is
  101. # * ast.IsNot
  102. # * ast.In
  103. # * ast.NotIn
  104. # the result is False for a and b and operation op
  105. return 0
  106. # the results for all the ops are True
  107. return 1
  108. operators: dict[type, typing.Callable] = {
  109. ast.Add: operator.add,
  110. ast.Sub: operator.sub,
  111. ast.Mult: operator.mul,
  112. ast.Div: operator.truediv,
  113. ast.Pow: operator.pow,
  114. ast.BitXor: operator.xor,
  115. ast.BitOr: operator.or_,
  116. ast.BitAnd: operator.and_,
  117. ast.USub: operator.neg,
  118. ast.RShift: operator.rshift,
  119. ast.LShift: operator.lshift,
  120. ast.Mod: operator.mod,
  121. ast.Compare: _compare,
  122. }
  123. math_constants = {
  124. 'e': math.e,
  125. 'pi': math.pi,
  126. }
  127. # with multiprocessing.get_context("fork") we are ready for Py3.14 (by emulating
  128. # the old behavior "fork") but it will not solve the core problem of fork, nor
  129. # will it remove the deprecation warnings in py3.12 & py3.13. Issue is
  130. # ddiscussed here: https://github.com/searxng/searxng/issues/4159
  131. mp_fork = multiprocessing.get_context("fork")
  132. def _eval_expr(expr):
  133. """
  134. Evaluates the given textual expression.
  135. Returns a tuple of (numericResult, isBooleanResult).
  136. >>> _eval_expr('2^6')
  137. 64, False
  138. >>> _eval_expr('2**6')
  139. 64, False
  140. >>> _eval_expr('1 + 2*3**(4^5) / (6 + -7)')
  141. -5.0, False
  142. >>> _eval_expr('1 < 3')
  143. 1, True
  144. >>> _eval_expr('5 < 3')
  145. 0, True
  146. >>> _eval_expr('17 == 11+1+5 == 7+5+5')
  147. 1, True
  148. """
  149. try:
  150. root_expr = ast.parse(expr, mode='eval').body
  151. return _eval(root_expr), isinstance(root_expr, ast.Compare)
  152. except (SyntaxError, TypeError, ZeroDivisionError):
  153. # Expression that can't be evaluated (i.e. not a math expression)
  154. return "", False
  155. def _eval(node):
  156. if isinstance(node, ast.Constant) and isinstance(node.value, (int, float)):
  157. return node.value
  158. if isinstance(node, ast.BinOp):
  159. return operators[type(node.op)](_eval(node.left), _eval(node.right))
  160. if isinstance(node, ast.UnaryOp):
  161. return operators[type(node.op)](_eval(node.operand))
  162. if isinstance(node, ast.Compare):
  163. return _compare(node.ops, [_eval(node.left)] + [_eval(c) for c in node.comparators])
  164. if isinstance(node, ast.Name) and node.id in math_constants:
  165. return math_constants[node.id]
  166. raise TypeError(node)
  167. def handler(q: multiprocessing.Queue, func, args, **kwargs): # pylint:disable=invalid-name
  168. try:
  169. q.put(func(*args, **kwargs))
  170. except:
  171. q.put(None)
  172. raise