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

Public Member Functions

 get_language (self, str searxng_locale, t.Any default=None)
t.Any get_region (self, str searxng_locale, t.Any default=None)
bool is_locale_supported (self, str searxng_locale)
 copy (self)
"EngineTraits | None" fetch_traits (cls, "Engine | types.ModuleType" engine)
 set_traits (self, "Engine | types.ModuleType" engine)

Static Public Attributes

dict regions = dataclasses.field(default_factory=dict)
dict languages = dataclasses.field(default_factory=dict)
str all_locale = None
data_type = 'traits_v1'
dict custom = dataclasses.field(default_factory=dict)

Protected Member Functions

 _set_traits_v1 (self, "Engine | types.ModuleType" engine)

Detailed Description

The class is intended to be instantiated for each engine.

Definition at line 38 of file traits.py.

Member Function Documentation

◆ _set_traits_v1()

searx.enginelib.traits.EngineTraits._set_traits_v1 ( self,
"Engine | types.ModuleType" engine )
protected

Definition at line 163 of file traits.py.

163 def _set_traits_v1(self, engine: "Engine | types.ModuleType"):
164 # For an engine, when there is `language: ...` in the YAML settings the engine
165 # does support only this one language (region)::
166 #
167 # - name: google italian
168 # engine: google
169 # language: it
170 # region: it-IT
171
172 traits = self.copy()
173
174 _msg = "settings.yml - engine: '%s' / %s: '%s' not supported"
175
176 languages = traits.languages
177 if hasattr(engine, 'language'):
178 if engine.language not in languages:
179 raise ValueError(_msg % (engine.name, 'language', engine.language))
180 traits.languages = {engine.language: languages[engine.language]}
181
182 regions = traits.regions
183 if hasattr(engine, 'region'):
184 if engine.region not in regions:
185 raise ValueError(_msg % (engine.name, 'region', engine.region))
186 traits.regions = {engine.region: regions[engine.region]}
187
188 engine.language_support = bool(traits.languages or traits.regions) # type: ignore
189
190 # set the copied & modified traits in engine's namespace
191 engine.traits = traits # pyright: ignore[reportAttributeAccessIssue]
192
193

References copy().

Referenced by set_traits().

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

◆ copy()

searx.enginelib.traits.EngineTraits.copy ( self)
Create a copy of the dataclass object.

Definition at line 133 of file traits.py.

133 def copy(self):
134 """Create a copy of the dataclass object."""
135 return EngineTraits(**dataclasses.asdict(self)) # type: ignore
136

Referenced by _set_traits_v1().

Here is the caller graph for this function:

◆ fetch_traits()

"EngineTraits | None" searx.enginelib.traits.EngineTraits.fetch_traits ( cls,
"Engine | types.ModuleType" 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 in searx.enginelib.traits.EngineTraitsMap.

Definition at line 138 of file traits.py.

138 def fetch_traits(cls, engine: "Engine | types.ModuleType") -> "EngineTraits | None":
139 """Call a function ``fetch_traits(engine_traits)`` from engines namespace to fetch
140 and set properties from the origin engine in the object ``engine_traits``. If
141 function does not exists, ``None`` is returned.
142 """
143
144 fetch_traits = getattr(engine, 'fetch_traits', None)
145 engine_traits = None
146
147 if fetch_traits:
148 engine_traits = cls()
149 fetch_traits(engine_traits)
150 return engine_traits
151

References fetch_traits().

Referenced by fetch_traits().

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

◆ get_language()

searx.enginelib.traits.EngineTraits.get_language ( self,
str searxng_locale,
t.Any default = None )
Return engine's language string that *best fits* to SearXNG's locale.

:param searxng_locale: SearXNG's internal representation of locale
  selected by the user.

:param default: engine's default language

The *best fits* rules are implemented in
:py:obj:`searx.locales.get_engine_locale`.  Except for the special value ``all``
which is determined from :py:obj:`EngineTraits.all_locale`.

Definition at line 88 of file traits.py.

88 def get_language(self, searxng_locale: str, default: t.Any = None):
89 """Return engine's language string that *best fits* to SearXNG's locale.
90
91 :param searxng_locale: SearXNG's internal representation of locale
92 selected by the user.
93
94 :param default: engine's default language
95
96 The *best fits* rules are implemented in
97 :py:obj:`searx.locales.get_engine_locale`. Except for the special value ``all``
98 which is determined from :py:obj:`EngineTraits.all_locale`.
99 """
100 if searxng_locale == 'all' and self.all_locale is not None:
101 return self.all_locale
102 return locales.get_engine_locale(searxng_locale, self.languages, default=default)
103

References all_locale, and languages.

Referenced by is_locale_supported().

Here is the caller graph for this function:

◆ get_region()

t.Any searx.enginelib.traits.EngineTraits.get_region ( self,
str searxng_locale,
t.Any default = None )
Return engine's region string that best fits to SearXNG's locale.

:param searxng_locale: SearXNG's internal representation of locale
  selected by the user.

:param default: engine's default region

The *best fits* rules are implemented in
:py:obj:`searx.locales.get_engine_locale`.  Except for the special value ``all``
which is determined from :py:obj:`EngineTraits.all_locale`.

Definition at line 104 of file traits.py.

104 def get_region(self, searxng_locale: str, default: t.Any = None) -> t.Any:
105 """Return engine's region string that best fits to SearXNG's locale.
106
107 :param searxng_locale: SearXNG's internal representation of locale
108 selected by the user.
109
110 :param default: engine's default region
111
112 The *best fits* rules are implemented in
113 :py:obj:`searx.locales.get_engine_locale`. Except for the special value ``all``
114 which is determined from :py:obj:`EngineTraits.all_locale`.
115 """
116 if searxng_locale == 'all' and self.all_locale is not None:
117 return self.all_locale
118 return locales.get_engine_locale(searxng_locale, self.regions, default=default)
119

References all_locale, and regions.

Referenced by is_locale_supported().

Here is the caller graph for this function:

◆ is_locale_supported()

bool searx.enginelib.traits.EngineTraits.is_locale_supported ( self,
str searxng_locale )
A *locale* (SearXNG's internal representation) is considered to be
supported by the engine if the *region* or the *language* is supported
by the engine.

For verification the functions :py:func:`EngineTraits.get_region` and
:py:func:`EngineTraits.get_language` are used.

Definition at line 120 of file traits.py.

120 def is_locale_supported(self, searxng_locale: str) -> bool:
121 """A *locale* (SearXNG's internal representation) is considered to be
122 supported by the engine if the *region* or the *language* is supported
123 by the engine.
124
125 For verification the functions :py:func:`EngineTraits.get_region` and
126 :py:func:`EngineTraits.get_language` are used.
127 """
128 if self.data_type == 'traits_v1':
129 return bool(self.get_region(searxng_locale) or self.get_language(searxng_locale))
130
131 raise TypeError('engine traits of type %s is unknown' % self.data_type)
132

References data_type, get_language(), and get_region().

Here is the call graph for this function:

◆ set_traits()

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

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

Reimplemented in searx.enginelib.traits.EngineTraitsMap.

Definition at line 152 of file traits.py.

152 def set_traits(self, engine: "Engine | types.ModuleType"):
153 """Set traits from self object in a :py:obj:`.Engine` namespace.
154
155 :param engine: engine instance build by :py:func:`searx.engines.load_engine`
156 """
157
158 if self.data_type == 'traits_v1':
159 self._set_traits_v1(engine)
160 else:
161 raise TypeError('engine traits of type %s is unknown' % self.data_type)
162

References _set_traits_v1(), and data_type.

Here is the call graph for this function:

Member Data Documentation

◆ all_locale

str searx.enginelib.traits.EngineTraits.all_locale = None
static

Definition at line 75 of file traits.py.

Referenced by get_language(), and get_region().

◆ custom

dict searx.enginelib.traits.EngineTraits.custom = dataclasses.field(default_factory=dict)
static

Definition at line 84 of file traits.py.

◆ data_type

searx.enginelib.traits.EngineTraits.data_type = 'traits_v1'
static

Definition at line 80 of file traits.py.

Referenced by is_locale_supported(), and set_traits().

◆ languages

searx.enginelib.traits.EngineTraits.languages = dataclasses.field(default_factory=dict)
static

◆ regions

searx.enginelib.traits.EngineTraits.regions = dataclasses.field(default_factory=dict)
static

Definition at line 41 of file traits.py.

Referenced by get_region().


The documentation for this class was generated from the following file:
  • /home/andrew/Documents/code/public/searxng/searx/enginelib/traits.py