summaryrefslogtreecommitdiff
path: root/searx/result_types/answer.py
blob: 1a24f12f1c1674f45af913bf5177180dd7482c4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Typification of the *answer* results.  Results of this type are rendered in
the :origin:`answers.html <searx/templates/simple/elements/answers.html>`
template.

----

.. autoclass:: BaseAnswer
   :members:
   :show-inheritance:

.. autoclass:: Answer
   :members:
   :show-inheritance:

.. autoclass:: Translations
   :members:
   :show-inheritance:

.. autoclass:: WeatherAnswer
   :members:
   :show-inheritance:

.. autoclass:: AnswerSet
   :members:
   :show-inheritance:
"""
# pylint: disable=too-few-public-methods


__all__ = ["AnswerSet", "Answer", "Translations", "WeatherAnswer"]

from flask_babel import gettext
import msgspec

from searx import weather
from ._base import Result


class BaseAnswer(Result, kw_only=True):
    """Base class of all answer types.  It is not intended to build instances of
    this class (aka *abstract*)."""


class AnswerSet:
    """Aggregator for :py:obj:`BaseAnswer` items in a result container."""

    def __init__(self):
        self._answerlist = []

    def __len__(self):
        return len(self._answerlist)

    def __bool__(self):
        return bool(self._answerlist)

    def add(self, answer: BaseAnswer) -> None:
        a_hash = hash(answer)
        for i in self._answerlist:
            if hash(i) == a_hash:
                return
        self._answerlist.append(answer)

    def __iter__(self):
        """Sort items in this set and iterate over the items."""
        self._answerlist.sort(key=lambda answer: answer.template)
        yield from self._answerlist

    def __contains__(self, answer: BaseAnswer) -> bool:
        a_hash = hash(answer)
        for i in self._answerlist:
            if hash(i) == a_hash:
                return True
        return False


class Answer(BaseAnswer, kw_only=True):
    """Simple answer type where the *answer* is a simple string with an optional
    :py:obj:`url field <Result.url>` field to link a resource (article, map, ..)
    related to the answer."""

    template: str = "answer/legacy.html"

    answer: str
    """Text of the answer."""

    def __hash__(self):
        """The hash value of field *answer* is the hash value of the
        :py:obj:`Answer` object.  :py:obj:`Answer <Result.__eq__>` objects are
        equal, when the hash values of both objects are equal."""
        return hash(self.answer)


class Translations(BaseAnswer, kw_only=True):
    """Answer type with a list of translations.

    The items in the list of :py:obj:`Translations.translations` are of type
    :py:obj:`Translations.Item`:

    .. code:: python

       def response(resp):
           results = []
           ...
           foo_1 = Translations.Item(
               text="foobar",
               synonyms=["bar", "foo"],
               examples=["foo and bar are placeholders"],
           )
           foo_url="https://www.deepl.com/de/translator#en/de/foo"
           ...
           Translations(results=results, translations=[foo], url=foo_url)

    """

    template: str = "answer/translations.html"
    """The template in :origin:`answer/translations.html
    <searx/templates/simple/answer/translations.html>`"""

    translations: "list[Translations.Item]"
    """List of translations."""

    def __post_init__(self):
        if not self.translations:
            raise ValueError("Translation does not have an item in the list translations")

    class Item(msgspec.Struct, kw_only=True):
        """A single element of the translations / a translation.  A translation
        consists of at least a mandatory ``text`` property (the translation) ,
        optional properties such as *definitions*, *synonyms* and *examples* are
        possible."""

        text: str
        """Translated text."""

        transliteration: str = ""
        """Transliteration_ of the requested translation.

        .. _Transliteration: https://en.wikipedia.org/wiki/Transliteration
        """

        examples: list[str] = []
        """List of examples for the requested translation."""

        definitions: list[str] = []
        """List of definitions for the requested translation."""

        synonyms: list[str] = []
        """List of synonyms for the requested translation."""


class WeatherAnswer(BaseAnswer, kw_only=True):
    """Answer type for weather data."""

    template: str = "answer/weather.html"
    """The template is located at :origin:`answer/weather.html
    <searx/templates/simple/answer/weather.html>`"""

    current: "WeatherAnswer.Item"
    """Current weather at ``location``."""

    forecasts: "list[WeatherAnswer.Item]" = []
    """Weather forecasts for ``location``."""

    service: str = ""
    """Weather service from which this information was provided."""

    class Item(msgspec.Struct, kw_only=True):
        """Weather parameters valid for a specific point in time."""

        location: weather.GeoLocation
        """The geo-location the weather data is from (e.g. `Berlin, Germany`)."""

        temperature: weather.Temperature
        """Air temperature at 2m above the ground."""

        condition: weather.WeatherConditionType
        """Standardized designations that summarize the weather situation
        (e.g. ``light sleet showers and thunder``)."""

        # optional fields

        datetime: weather.DateTime | None = None
        """Time of the forecast - not needed for the current weather."""

        summary: str | None = None
        """One-liner about the weather forecast / current weather conditions.
        If unset, a summary is build up from temperature and current weather
        conditions.
        """

        feels_like: weather.Temperature | None = None
        """Apparent temperature, the temperature equivalent perceived by
        humans, caused by the combined effects of air temperature, relative
        humidity and wind speed.  The measure is most commonly applied to the
        perceived outdoor temperature.
        """

        pressure: weather.Pressure | None = None
        """Air pressure at sea level (e.g. 1030 hPa) """

        humidity: weather.RelativeHumidity | None = None
        """Amount of relative humidity in the air at 2m above the ground. The
        unit is ``%``, e.g. 60%)
        """

        wind_from: weather.Compass
        """The directon which moves towards / direction the wind is coming from."""

        wind_speed: weather.WindSpeed | None = None
        """Speed of wind / wind speed at 10m above the ground (10 min average)."""

        cloud_cover: int | None = None
        """Amount of sky covered by clouds / total cloud cover for all heights
        (cloudiness, unit: %)"""

        # attributes: dict[str, str | int] = {}
        # """Key-Value dict of additional typeless weather attributes."""

        def __post_init__(self):
            if not self.summary:
                self.summary = gettext("{location}: {temperature}, {condition}").format(
                    location=self.location,
                    temperature=self.temperature,
                    condition=gettext(self.condition.capitalize()),
                )

        @property
        def url(self) -> str | None:
            """Determines a `data URL`_ with a symbol for the weather
            conditions.  If no symbol can be assigned, ``None`` is returned.

            .. _data URL:
               https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data
            """
            return weather.symbol_url(self.condition)