.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.plugins._core.ModulePlugin Class Reference
+ Inheritance diagram for searx.plugins._core.ModulePlugin:
+ Collaboration diagram for searx.plugins._core.ModulePlugin:

Public Member Functions

 __init__ (self, types.ModuleType mod)
 
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)
 
None|list[Resultpost_search (self, SXNG_Request request, "SearchWithPlugins" search)
 
- Public Member Functions inherited from searx.plugins._core.Plugin
None __init__ (self)
 
int __hash__ (self)
 
 __eq__ (self, other)
 

Public Attributes

 module = mod
 
 info
 

Static Protected Attributes

tuple _required_attrs = (("name", str), ("description", str), ("default_on", bool))
 

Additional Inherited Members

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

Detailed Description

A wrapper class for legacy *plugins*.

.. note::

   For internal use only!

In a module plugin, the follwing names are mapped:

- `module.query_keywords` --> :py:obj:`Plugin.keywords`
- `module.plugin_id` --> :py:obj:`Plugin.id`
- `module.logger` --> :py:obj:`Plugin.log`

Definition at line 165 of file _core.py.

Constructor & Destructor Documentation

◆ __init__()

searx.plugins._core.ModulePlugin.__init__ ( self,
types.ModuleType mod )
In case of missing attributes in the module or wrong types are given,
a :py:obj:`TypeError` exception is raised.

Definition at line 181 of file _core.py.

181 def __init__(self, mod: types.ModuleType):
182 """In case of missing attributes in the module or wrong types are given,
183 a :py:obj:`TypeError` exception is raised."""
184
185 self.module = mod
186 self.id = getattr(self.module, "plugin_id", self.module.__name__)
187 self.log = logging.getLogger(self.module.__name__)
188 self.keywords = getattr(self.module, "query_keywords", [])
189
190 for attr, attr_type in self._required_attrs:
191 if not hasattr(self.module, attr):
192 msg = f"missing attribute {attr}, cannot load plugin"
193 self.log.critical(msg)
194 raise TypeError(msg)
195 if not isinstance(getattr(self.module, attr), attr_type):
196 msg = f"attribute {attr} is not of type {attr_type}"
197 self.log.critical(msg)
198 raise TypeError(msg)
199
200 self.default_on = mod.default_on
201 self.info = PluginInfo(
202 id=self.id,
203 name=self.module.name,
204 description=self.module.description,
205 preference_section=getattr(self.module, "preference_section", None),
206 examples=getattr(self.module, "query_examples", []),
207 keywords=self.keywords,
208 )
209
210 # monkeypatch module
211 self.module.logger = self.log # type: ignore
212
213 super().__init__()
214

Member Function Documentation

◆ init()

bool searx.plugins._core.ModulePlugin.init ( self,
flask.Flask app )
Initialization of the plugin, the return value decides whether this
plugin is active or not.  Initialization only takes place once, at the
time the WEB application is set up.  The base methode always returns
``True``, the methode can be overwritten in the inheritances,

- ``True`` plugin is active
- ``False`` plugin is inactive

Reimplemented from searx.plugins._core.Plugin.

Definition at line 215 of file _core.py.

215 def init(self, app: flask.Flask) -> bool:
216 if not hasattr(self.module, "init"):
217 return True
218 return self.module.init(app)
219

References init(), searx.answerers._core.ModuleAnswerer.module, and module.

Referenced by searx.sqlitedb.SQLiteAppl.connect(), and init().

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

◆ on_result()

bool searx.plugins._core.ModulePlugin.on_result ( self,
SXNG_Request request,
"SearchWithPlugins" search,
Result result )
Runs for each result of each engine and returns a boolean:

- ``True`` to keep the result
- ``False`` to remove the result from the result list

The ``result`` can be modified to the needs.

.. hint::

   If :py:obj:`Result.url` is modified, :py:obj:`Result.parsed_url` must
   be changed accordingly:

   .. code:: python

      result["parsed_url"] = urlparse(result["url"])

Reimplemented from searx.plugins._core.Plugin.

Definition at line 225 of file _core.py.

225 def on_result(self, request: SXNG_Request, search: "SearchWithPlugins", result: Result) -> bool:
226 if not hasattr(self.module, "on_result"):
227 return True
228 return self.module.on_result(request, search, result)
229

References searx.answerers._core.ModuleAnswerer.module, module, and on_result().

Referenced by on_result().

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

◆ post_search()

None | list[Result] searx.plugins._core.ModulePlugin.post_search ( self,
SXNG_Request request,
"SearchWithPlugins" search )
Runs AFTER the search request.  Can return a list of :py:obj:`Result`
objects to be added to the final result list.

Reimplemented from searx.plugins._core.Plugin.

Definition at line 230 of file _core.py.

230 def post_search(self, request: SXNG_Request, search: "SearchWithPlugins") -> None | list[Result]:
231 if not hasattr(self.module, "post_search"):
232 return None
233 return self.module.post_search(request, search)
234
235

References searx.answerers._core.ModuleAnswerer.module, module, and post_search().

Referenced by post_search().

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

◆ pre_search()

bool searx.plugins._core.ModulePlugin.pre_search ( self,
SXNG_Request request,
"SearchWithPlugins" search )
Runs BEFORE the search request and returns a boolean:

- ``True`` to continue the search
- ``False`` to stop the search

Reimplemented from searx.plugins._core.Plugin.

Definition at line 220 of file _core.py.

220 def pre_search(self, request: SXNG_Request, search: "SearchWithPlugins") -> bool:
221 if not hasattr(self.module, "pre_search"):
222 return True
223 return self.module.pre_search(request, search)
224

References searx.answerers._core.ModuleAnswerer.module, module, and pre_search().

Referenced by pre_search().

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

Member Data Documentation

◆ _required_attrs

tuple searx.plugins._core.ModulePlugin._required_attrs = (("name", str), ("description", str), ("default_on", bool))
staticprotected

Definition at line 179 of file _core.py.

◆ info

searx.plugins._core.ModulePlugin.info
Initial value:
= PluginInfo(
id=self.id,
name=self.module.name,
description=self.module.description,
preference_section=getattr(self.module, "preference_section", None),
examples=getattr(self.module, "query_examples", []),
keywords=self.keywords,
)

Definition at line 201 of file _core.py.

◆ module

searx.plugins._core.ModulePlugin.module = mod

Definition at line 185 of file _core.py.

Referenced by init(), on_result(), post_search(), and pre_search().


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