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

Public Member Functions

 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)
 
Union[ 'EngineTraits', None] fetch_traits (cls, Engine engine)
 
 set_traits (self, Engine engine)
 

Public Attributes

 languages
 
 regions
 
 data_type
 

Static Public Attributes

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)
 

Protected Member Functions

 _set_traits_v1 (self, Engine engine)
 

Detailed Description

The class is intended to be instantiated for each engine.

Definition at line 37 of file traits.py.

Member Function Documentation

◆ _set_traits_v1()

searx.enginelib.traits.EngineTraits._set_traits_v1 ( self,
Engine engine )
protected

Definition at line 162 of file traits.py.

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

References searx.enginelib.traits.EngineTraits.copy().

Referenced by searx.enginelib.traits.EngineTraits.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 132 of file traits.py.

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

Referenced by searx.enginelib.traits.EngineTraits._set_traits_v1().

+ Here is the caller graph for this function:

◆ fetch_traits()

Union['EngineTraits', None] searx.enginelib.traits.EngineTraits.fetch_traits ( cls,
Engine 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 137 of file traits.py.

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

◆ get_language()

searx.enginelib.traits.EngineTraits.get_language ( self,
str searxng_locale,
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 87 of file traits.py.

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

References searx.enginelib.traits.EngineTraits.all_locale.

◆ get_region()

searx.enginelib.traits.EngineTraits.get_region ( self,
str searxng_locale,
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 103 of file traits.py.

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

References searx.enginelib.traits.EngineTraits.all_locale.

◆ 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 119 of file traits.py.

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

◆ set_traits()

searx.enginelib.traits.EngineTraits.set_traits ( self,
Engine 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 151 of file traits.py.

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

References searx.enginelib.traits.EngineTraits._set_traits_v1(), and searx.enginelib.traits.EngineTraits.data_type.

+ Here is the call graph for this function:

Member Data Documentation

◆ all_locale

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

◆ custom

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

Definition at line 83 of file traits.py.

◆ data_type [1/2]

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

Definition at line 79 of file traits.py.

Referenced by searx.enginelib.traits.EngineTraits.set_traits().

◆ data_type [2/2]

searx.enginelib.traits.EngineTraits.data_type

Definition at line 127 of file traits.py.

Referenced by searx.enginelib.traits.EngineTraits.set_traits().

◆ languages [1/2]

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

◆ languages [2/2]

◆ regions [1/2]

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

Definition at line 40 of file traits.py.

◆ regions [2/2]

searx.enginelib.traits.EngineTraits.regions

Definition at line 117 of file traits.py.


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