.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, str fqn)
 
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
str id = ""
 
bool default_on = False
 
list keywords = []
 
logging log .Logger
 
str fqn = ""
 

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 169 of file _core.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 185 of file _core.py.

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

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 220 of file _core.py.

220 def init(self, app: flask.Flask) -> bool:
221 if not hasattr(self.module, "init"):
222 return True
223 return self.module.init(app)
224

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 230 of file _core.py.

230 def on_result(self, request: SXNG_Request, search: "SearchWithPlugins", result: Result) -> bool:
231 if not hasattr(self.module, "on_result"):
232 return True
233 return self.module.on_result(request, search, result)
234

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 235 of file _core.py.

235 def post_search(self, request: SXNG_Request, search: "SearchWithPlugins") -> None | list[Result]:
236 if not hasattr(self.module, "post_search"):
237 return None
238 return self.module.post_search(request, search)
239
240

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 225 of file _core.py.

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

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 183 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 206 of file _core.py.

◆ module

searx.plugins._core.ModulePlugin.module = mod

Definition at line 190 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: