.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.plugins.calculator.SXNGPlugin Class Reference
Inheritance diagram for searx.plugins.calculator.SXNGPlugin:
Collaboration diagram for searx.plugins.calculator.SXNGPlugin:

Public Member Functions

None __init__ (self, "PluginCfg" plg_cfg)
 timeout_func (self, timeout, func, *args, **kwargs)
EngineResults post_search (self, "SXNG_Request" request, "SearchWithPlugins" search)
Public Member Functions inherited from searx.plugins._core.Plugin
None __init__ (self, PluginCfg plg_cfg)
int __hash__ (self)
 __eq__ (self, other)
bool init (self, "flask.Flask" app)
bool pre_search (self, SXNG_Request request, "SearchWithPlugins" search)
bool on_result (self, SXNG_Request request, "SearchWithPlugins" search, Result result)

Public Attributes

 info

Additional Inherited Members

Static Public Attributes inherited from searx.plugins._core.Plugin
str id = ""
typing active .ClassVar[bool]
list keywords = []
logging log .Logger
str fqn = ""

Detailed Description

Plugin converts strings to different hash digests.  The results are
displayed in area for the "answers".

Definition at line 26 of file calculator.py.

Constructor & Destructor Documentation

◆ __init__()

None searx.plugins.calculator.SXNGPlugin.__init__ ( self,
"PluginCfg" plg_cfg )

Definition at line 33 of file calculator.py.

33 def __init__(self, plg_cfg: "PluginCfg") -> None:
34 super().__init__(plg_cfg)
35
36 self.info = PluginInfo(
37 id=self.id,
38 name=gettext("Basic Calculator"),
39 description=gettext("Calculate mathematical expressions via the search bar"),
40 preference_section="general",
41 )
42

References __init__().

Referenced by __init__().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Function Documentation

◆ post_search()

EngineResults searx.plugins.calculator.SXNGPlugin.post_search ( self,
"SXNG_Request" request,
"SearchWithPlugins" search )
Runs AFTER the search request.  Can return a list of
:py:obj:`Result <searx.result_types._base.Result>` objects to be added to the
final result list.

Reimplemented from searx.plugins._core.Plugin.

Definition at line 59 of file calculator.py.

59 def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults:
60 results = EngineResults()
61
62 # only show the result of the expression on the first page
63 if search.search_query.pageno > 1:
64 return results
65
66 query = search.search_query.query
67 # in order to avoid DoS attacks with long expressions, ignore long expressions
68 if len(query) > 100:
69 return results
70
71 # replace commonly used math operators with their proper Python operator
72 query = query.replace("x", "*").replace(":", "/")
73
74 # Is this a term that can be calculated?
75 word, constants = "", set()
76 for x in query:
77 # Alphabetic characters are defined as "Letters" in the Unicode
78 # character database and are the constants in an equation.
79 if x.isalpha():
80 word += x.strip()
81 elif word:
82 constants.add(word)
83 word = ""
84
85 # In the term of an arithmetic operation there should be no other
86 # alphabetic characters besides the constants
87 if constants - set(math_constants):
88 return results
89
90 # use UI language
91 ui_locale = babel.Locale.parse(request.preferences.get_value("locale"), sep="-")
92
93 # parse the number system in a localized way
94 def _decimal(match: re.Match) -> str:
95 val = match.string[match.start() : match.end()]
96 val = babel.numbers.parse_decimal(val, ui_locale, numbering_system="latn")
97 return str(val)
98
99 decimal = ui_locale.number_symbols["latn"]["decimal"]
100 group = ui_locale.number_symbols["latn"]["group"]
101 query = re.sub(f"[0-9]+[{decimal}|{group}][0-9]+[{decimal}|{group}]?[0-9]?", _decimal, query)
102
103 # in python, powers are calculated via **
104 query_py_formatted = query.replace("^", "**")
105
106 # Prevent the runtime from being longer than 50 ms
107 res = self.timeout_func(0.05, _eval_expr, query_py_formatted)
108 if res is None or res[0] == "":
109 return results
110
111 res, is_boolean = res
112 if is_boolean:
113 res = "True" if res != 0 else "False"
114 else:
115 res = babel.numbers.format_decimal(res, locale=ui_locale)
116 results.add(results.types.Answer(answer=f"{search.search_query.query} = {res}"))
117
118 return results
119
120

References timeout_func().

Here is the call graph for this function:

◆ timeout_func()

searx.plugins.calculator.SXNGPlugin.timeout_func ( self,
timeout,
func,
* args,
** kwargs )

Definition at line 43 of file calculator.py.

43 def timeout_func(self, timeout, func, *args, **kwargs):
44 que = mp_fork.Queue()
45 p = mp_fork.Process(target=handler, args=(que, func, args), kwargs=kwargs)
46 p.start()
47 p.join(timeout=timeout)
48 ret_val = None
49 # pylint: disable=used-before-assignment,undefined-variable
50 if not p.is_alive():
51 ret_val = que.get()
52 else:
53 self.log.debug("terminate function (%s: %s // %s) after timeout is exceeded", func.__name__, args, kwargs)
54 p.terminate()
55 p.join()
56 p.close()
57 return ret_val
58

References searx.plugins._core.Plugin.log.

Referenced by post_search().

Here is the caller graph for this function:

Member Data Documentation

◆ info

searx.plugins.calculator.SXNGPlugin.info
Initial value:
= PluginInfo(
id=self.id,
name=gettext("Basic Calculator"),
description=gettext("Calculate mathematical expressions via the search bar"),
preference_section="general",
)

Definition at line 36 of file calculator.py.


The documentation for this class was generated from the following file:
  • /home/andrew/Documents/code/public/searxng/searx/plugins/calculator.py