.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.plugins._core.PluginStorage Class Reference

Public Member Functions

 __init__ (self)
 
 __iter__ (self)
 
 __len__ (self)
 
list[PluginInfoinfo (self)
 
 load_settings (self, dict[str, dict] cfg)
 
 register (self, Plugin plugin)
 
None 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 post_search (self, SXNG_Request request, "SearchWithPlugins" search)
 

Static Public Attributes

set plugin_list [Plugin]
 

Detailed Description

A storage for managing the *plugins* of SearXNG.

Definition at line 190 of file _core.py.

Constructor & Destructor Documentation

◆ __init__()

searx.plugins._core.PluginStorage.__init__ ( self)

Definition at line 196 of file _core.py.

196 def __init__(self):
197 self.plugin_list = set()
198

References plugin_list.

Member Function Documentation

◆ __iter__()

searx.plugins._core.PluginStorage.__iter__ ( self)

Definition at line 199 of file _core.py.

199 def __iter__(self):
200 yield from self.plugin_list
201

References plugin_list.

◆ __len__()

searx.plugins._core.PluginStorage.__len__ ( self)

Definition at line 202 of file _core.py.

202 def __len__(self):
203 return len(self.plugin_list)
204

References plugin_list.

◆ info()

list[PluginInfo] searx.plugins._core.PluginStorage.info ( self)

Definition at line 206 of file _core.py.

206 def info(self) -> list[PluginInfo]:
207
208 return [p.info for p in self.plugin_list]
209

References plugin_list.

◆ init()

None searx.plugins._core.PluginStorage.init ( self,
"flask.Flask" app )
Calls the method :py:obj:`Plugin.init` of each plugin in this
storage.  Depending on its return value, the plugin is removed from
*this* storage or not.

Definition at line 242 of file _core.py.

242 def init(self, app: "flask.Flask") -> None:
243 """Calls the method :py:obj:`Plugin.init` of each plugin in this
244 storage. Depending on its return value, the plugin is removed from
245 *this* storage or not."""
246
247 for plg in self.plugin_list.copy():
248 if not plg.init(app):
249 self.plugin_list.remove(plg)
250

References plugin_list.

Referenced by searx.sqlitedb.SQLiteAppl.connect().

+ Here is the caller graph for this function:

◆ load_settings()

searx.plugins._core.PluginStorage.load_settings ( self,
dict[str, dict] cfg )
Load plugins configured in SearXNG's settings :ref:`settings
plugins`.

Definition at line 210 of file _core.py.

210 def load_settings(self, cfg: dict[str, dict]):
211 """Load plugins configured in SearXNG's settings :ref:`settings
212 plugins`."""
213
214 for fqn, plg_settings in cfg.items():
215 cls = None
216 mod_name, cls_name = fqn.rsplit('.', 1)
217 try:
218 mod = importlib.import_module(mod_name)
219 cls = getattr(mod, cls_name, None)
220 except Exception as exc: # pylint: disable=broad-exception-caught
221 log.exception(exc)
222
223 if cls is None:
224 msg = f"plugin {fqn} is not implemented"
225 raise ValueError(msg)
226 plg = cls(PluginCfg(**plg_settings))
227 self.register(plg)
228

References searx.answerers._core.AnswerStorage.register(), and register().

+ Here is the call graph for this function:

◆ on_result()

bool searx.plugins._core.PluginStorage.on_result ( self,
SXNG_Request request,
"SearchWithPlugins" search,
Result result )

Definition at line 265 of file _core.py.

265 def on_result(self, request: SXNG_Request, search: "SearchWithPlugins", result: Result) -> bool:
266
267 ret = True
268 for plugin in [p for p in self.plugin_list if p.id in search.user_plugins]:
269 try:
270 ret = bool(plugin.on_result(request=request, search=search, result=result))
271 except Exception: # pylint: disable=broad-except
272 plugin.log.exception("Exception while calling on_result")
273 continue
274 if not ret:
275 # ignore this result item on the first False from a plugin
276 break
277
278 return ret
279

References plugin_list.

◆ post_search()

None searx.plugins._core.PluginStorage.post_search ( self,
SXNG_Request request,
"SearchWithPlugins" search )
Extend :py:obj:`search.result_container
<searx.results.ResultContainer`> with result items from plugins listed
in :py:obj:`search.user_plugins <SearchWithPlugins.user_plugins>`.

Definition at line 280 of file _core.py.

280 def post_search(self, request: SXNG_Request, search: "SearchWithPlugins") -> None:
281 """Extend :py:obj:`search.result_container
282 <searx.results.ResultContainer`> with result items from plugins listed
283 in :py:obj:`search.user_plugins <SearchWithPlugins.user_plugins>`.
284 """
285
286 keyword = None
287 for keyword in search.search_query.query.split():
288 if keyword:
289 break
290
291 for plugin in [p for p in self.plugin_list if p.id in search.user_plugins]:
292
293 if plugin.keywords:
294 # plugin with keywords: skip plugin if no keyword match
295 if keyword and keyword not in plugin.keywords:
296 continue
297 try:
298 results = plugin.post_search(request=request, search=search) or []
299 except Exception: # pylint: disable=broad-except
300 plugin.log.exception("Exception while calling post_search")
301 continue
302
303 # In case of *plugins* prefix ``plugin:`` is set, see searx.result_types.Result
304 search.result_container.extend(f"plugin: {plugin.id}", results)

References plugin_list.

◆ pre_search()

bool searx.plugins._core.PluginStorage.pre_search ( self,
SXNG_Request request,
"SearchWithPlugins" search )

Definition at line 251 of file _core.py.

251 def pre_search(self, request: SXNG_Request, search: "SearchWithPlugins") -> bool:
252
253 ret = True
254 for plugin in [p for p in self.plugin_list if p.id in search.user_plugins]:
255 try:
256 ret = bool(plugin.pre_search(request=request, search=search))
257 except Exception: # pylint: disable=broad-except
258 plugin.log.exception("Exception while calling pre_search")
259 continue
260 if not ret:
261 # skip this search on the first False from a plugin
262 break
263 return ret
264

References plugin_list.

◆ register()

searx.plugins._core.PluginStorage.register ( self,
Plugin plugin )
Register a :py:obj:`Plugin`.  In case of name collision (if two
plugins have same ID) a :py:obj:`KeyError` exception is raised.

Definition at line 229 of file _core.py.

229 def register(self, plugin: Plugin):
230 """Register a :py:obj:`Plugin`. In case of name collision (if two
231 plugins have same ID) a :py:obj:`KeyError` exception is raised.
232 """
233
234 if plugin in [p.id for p in self.plugin_list]:
235 msg = f"name collision '{plugin.id}'"
236 plugin.log.critical(msg)
237 raise KeyError(msg)
238
239 self.plugin_list.add(plugin)
240 plugin.log.debug("plugin has been loaded")
241

References plugin_list.

Referenced by load_settings().

+ Here is the caller graph for this function:

Member Data Documentation

◆ plugin_list

searx.plugins._core.PluginStorage.plugin_list [Plugin]
static

Definition at line 193 of file _core.py.

Referenced by __init__(), __iter__(), __len__(), info(), init(), on_result(), post_search(), pre_search(), and register().


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