.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.enginelib.traits.EngineTraitsMap Class Reference
+ Inheritance diagram for searx.enginelib.traits.EngineTraitsMap:
+ Collaboration diagram for searx.enginelib.traits.EngineTraitsMap:

Public Member Functions

 save_data (self)
 
'EngineTraitsMapfrom_data (cls)
 
'EngineTraitsMapfetch_traits (cls, Callable log)
 
 set_traits (self, Engine|types.ModuleType engine)
 
- Public Member Functions inherited from searx.enginelib.traits.EngineTraits
 get_language (self, str searxng_locale, default=None)
 
 get_region (self, str searxng_locale, default=None)
 
bool is_locale_supported (self, str searxng_locale)
 
 copy (self)
 

Static Public Attributes

tuple ENGINE_TRAITS_FILE = (data_dir / 'engine_traits.json').resolve()
 
- Static Public Attributes inherited from searx.enginelib.traits.EngineTraits
Dict regions = dataclasses.field(default_factory=dict)
 
Dict languages = dataclasses.field(default_factory=dict)
 
Optional all_locale = None
 
Literal data_type = 'traits_v1'
 
Dict custom = dataclasses.field(default_factory=dict)
 

Additional Inherited Members

- Protected Member Functions inherited from searx.enginelib.traits.EngineTraits
 _set_traits_v1 (self, Engine engine)
 

Detailed Description

A python dictionary to map :class:`EngineTraits` by engine name.

Definition at line 195 of file traits.py.

Member Function Documentation

◆ fetch_traits()

'EngineTraitsMap' searx.enginelib.traits.EngineTraitsMap.fetch_traits ( cls,
Callable engine )
Call a function ``fetch_traits(engine_traits)`` from engines namespace to fetch
and set properties from the origin engine in the object ``engine_traits``.  If
function does not exists, ``None`` is returned.

Reimplemented from searx.enginelib.traits.EngineTraits.

Definition at line 215 of file traits.py.

215 def fetch_traits(cls, log: Callable) -> 'EngineTraitsMap':
216 from searx import engines # pylint: disable=cyclic-import, import-outside-toplevel
217
218 names = list(engines.engines)
219 names.sort()
220 obj = cls()
221
222 for engine_name in names:
223 engine = engines.engines[engine_name]
224 traits = None
225
226 # pylint: disable=broad-exception-caught
227 try:
228 traits = EngineTraits.fetch_traits(engine)
229 except Exception as exc:
230 log("FATAL: while fetch_traits %s: %s" % (engine_name, exc))
231 if os.environ.get('FORCE', '').lower() not in ['on', 'true', '1']:
232 raise
233 v = ENGINE_TRAITS.get(engine_name)
234 if v:
235 log("FORCE: re-use old values from fetch_traits - ENGINE_TRAITS[%s]" % engine_name)
236 traits = EngineTraits(**v)
237
238 if traits is not None:
239 log("%-20s: SearXNG languages --> %s " % (engine_name, len(traits.languages)))
240 log("%-20s: SearXNG regions --> %s" % (engine_name, len(traits.regions)))
241 obj[engine_name] = traits
242
243 return obj
244

◆ from_data()

'EngineTraitsMap' searx.enginelib.traits.EngineTraitsMap.from_data ( cls)
Instantiate :class:`EngineTraitsMap` object from :py:obj:`ENGINE_TRAITS`

Definition at line 207 of file traits.py.

207 def from_data(cls) -> 'EngineTraitsMap':
208 """Instantiate :class:`EngineTraitsMap` object from :py:obj:`ENGINE_TRAITS`"""
209 obj = cls()
210 for k, v in ENGINE_TRAITS.items():
211 obj[k] = EngineTraits(**v)
212 return obj
213

◆ save_data()

searx.enginelib.traits.EngineTraitsMap.save_data ( self)
Store EngineTraitsMap in in file :py:obj:`self.ENGINE_TRAITS_FILE`

Definition at line 201 of file traits.py.

201 def save_data(self):
202 """Store EngineTraitsMap in in file :py:obj:`self.ENGINE_TRAITS_FILE`"""
203 with open(self.ENGINE_TRAITS_FILE, 'w', encoding='utf-8') as f:
204 json.dump(self, f, indent=2, sort_keys=True, cls=EngineTraitsEncoder)
205

References ENGINE_TRAITS_FILE.

◆ set_traits()

searx.enginelib.traits.EngineTraitsMap.set_traits ( self,
Engine | types.ModuleType engine )
Set traits in a :py:obj:`Engine` namespace.

:param engine: engine instance build by :py:func:`searx.engines.load_engine`

Reimplemented from searx.enginelib.traits.EngineTraits.

Definition at line 245 of file traits.py.

245 def set_traits(self, engine: Engine | types.ModuleType):
246 """Set traits in a :py:obj:`Engine` namespace.
247
248 :param engine: engine instance build by :py:func:`searx.engines.load_engine`
249 """
250
251 engine_traits = EngineTraits(data_type='traits_v1')
252 if engine.name in self.keys():
253 engine_traits = self[engine.name]
254
255 elif engine.engine in self.keys():
256 # The key of the dictionary traits_map is the *engine name*
257 # configured in settings.xml. When multiple engines are configured
258 # in settings.yml to use the same origin engine (python module)
259 # these additional engines can use the languages from the origin
260 # engine. For this use the configured ``engine: ...`` from
261 # settings.yml
262 engine_traits = self[engine.engine]
263
264 engine_traits.set_traits(engine)

Member Data Documentation

◆ ENGINE_TRAITS_FILE

searx.enginelib.traits.EngineTraitsMap.ENGINE_TRAITS_FILE = (data_dir / 'engine_traits.json').resolve()
static

Definition at line 198 of file traits.py.

Referenced by save_data().


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