Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3f5cbf87 authored by Nivesh Krishna's avatar Nivesh Krishna
Browse files

Merge branch 'add-calculator' into 'master'

add calculator plugin

Closes backlog#683

See merge request !112
parents 4ae8725f cb8ab5c1
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -18,4 +18,5 @@ sphinxcontrib-programoutput==0.17
sphinx-autobuild==2021.3.14
linuxdoc==20211220
aiounittest==1.4.1
numexpr==2.8.1
werkzeug==2.0.3
+1 −1
Original line number Diff line number Diff line
@@ -16,5 +16,5 @@ langdetect==1.0.9
setproctitle==1.2.2
redis==3.4.1
ring==0.7.3
numexpr==2.8.1
werkzeug==2.0.3
+2 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ logger = logger.getChild('plugins')
from searx.plugins import (oa_doi_rewrite,
                           ahmia_filter,
                           hash_plugin,
                           calculator,
                           https_rewrite,
                           infinite_scroll,
                           self_info,
@@ -175,6 +176,7 @@ plugins.register(search_on_category_select)
plugins.register(tracker_url_remover)
plugins.register(vim_hotkeys)
plugins.register(rest_api)
plugins.register(calculator)
# load external plugins
if 'plugins' in settings:
    plugins.register(*settings['plugins'], external=True)
+64 −0
Original line number Diff line number Diff line
from searx import logger
from numexpr import evaluate
from flask_babel import gettext


name = gettext('Calculator')
description = gettext('This plugin extends results when the query is a mathematical expression')
default_on = True
logger = logger.getChild("calculator")


def check_if_loaded():
    logger.debug("initializing calculator plugin")


def is_really_big(query):
    # For cases like 2**99999**9999
    if len(query.split("**")) >= 3:
        return True
    # Add more cases if needed
    return False


def post_search(request, search):
    if search.search_query.pageno > 1:
        return
    try:
        query = search.search_query.query.lower()
        query = query.replace("x", "*")

        # Not going to compute if only one number is present
        try:
            x = int(query) or float(query)
            return
        except ValueError:
            pass

        # Not going to compute if no numbers are present
        if not any(i.isdigit() for i in query):
            return

        # Not going to compute the result if the query is too big
        if len(query) > 30:
            return

        # Not going to compute the result if the query is not within permissible range
        if is_really_big(query):
            raise OverflowError

        value = evaluate(query).item()
        if type(value) in (int, float):
            search.result_container.answers.clear()
            answer = "{} = {}".format(query, value)
            search.result_container.answers[answer] = {'answer': answer, 'calculator': True}
    except (ZeroDivisionError, ValueError, FloatingPointError, MemoryError, OverflowError) as e:
        answer = gettext('Error')
        search.result_container.answers[answer] = {'answer': answer, 'calculator': True}
    except Exception as e:
        logger.debug(e)

    return


check_if_loaded()
+2 −2
Original line number Diff line number Diff line
@@ -108,8 +108,8 @@ outgoing: # communication with search engines

# uncomment below section if you want to configure which plugin is enabled by default
#
# enabled_plugins:
#   - "HTTPS rewrite"
enabled_plugins:
  - "Calculator"
#   - ...

# Example to rewrite hostnames in external links
Loading