.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)
 
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
 

Static Public Attributes

dict operators
 
- 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 43 of file calculator.py.

43 def post_search(self, request: "SXNG_Request", search: "SearchWithPlugins") -> EngineResults:
44 results = EngineResults()
45
46 # only show the result of the expression on the first page
47 if search.search_query.pageno > 1:
48 return results
49
50 query = search.search_query.query
51 # in order to avoid DoS attacks with long expressions, ignore long expressions
52 if len(query) > 100:
53 return results
54
55 # replace commonly used math operators with their proper Python operator
56 query = query.replace("x", "*").replace(":", "/")
57
58 # use UI language
59 ui_locale = babel.Locale.parse(request.preferences.get_value("locale"), sep="-")
60
61 # parse the number system in a localized way
62 def _decimal(match: re.Match) -> str:
63 val = match.string[match.start() : match.end()]
64 val = babel.numbers.parse_decimal(val, ui_locale, numbering_system="latn")
65 return str(val)
66
67 decimal = ui_locale.number_symbols["latn"]["decimal"]
68 group = ui_locale.number_symbols["latn"]["group"]
69 query = re.sub(f"[0-9]+[{decimal}|{group}][0-9]+[{decimal}|{group}]?[0-9]?", _decimal, query)
70
71 # only numbers and math operators are accepted
72 if any(str.isalpha(c) for c in query):
73 return results
74
75 # in python, powers are calculated via **
76 query_py_formatted = query.replace("^", "**")
77
78 # Prevent the runtime from being longer than 50 ms
79 res = timeout_func(0.05, _eval_expr, query_py_formatted)
80 if res is None or res == "":
81 return results
82
83 res = babel.numbers.format_decimal(res, locale=ui_locale)
84 results.add(results.types.Answer(answer=f"{search.search_query.query} = {res}"))
85
86 return results
87
88

References searx.plugins.calculator.timeout_func().

+ Here is the call 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.

◆ operators

dict searx.plugins.calculator.SXNGPlugin.operators
static
Initial value:
= {
ast.Add: operator.add,
ast.Sub: operator.sub,
ast.Mult: operator.mul,
ast.Div: operator.truediv,
ast.Pow: operator.pow,
ast.BitXor: operator.xor,
ast.USub: operator.neg,
}

Definition at line 89 of file calculator.py.


The documentation for this class was generated from the following file: