22from collections import defaultdict
33from operator import itemgetter
44from threading import RLock
5- from typing import List , NamedTuple , Set
5+ from typing import List , NamedTuple , Set , Optional
66from urllib .parse import urlparse , unquote
77
88from searx import logger
99from searx .engines import engines
1010from searx .metrics import histogram_observe , counter_add , count_error
11+ from . import models
1112
1213
1314CONTENT_LEN_IGNORED_CHARS_REGEX = re .compile (r'[,;:!?\./\\\\ ()-_]' , re .M | re .U )
@@ -55,7 +56,7 @@ def compare_urls(url_a, url_b):
5556 return unquote (path_a ) == unquote (path_b )
5657
5758
58- def merge_two_infoboxes (infobox1 , infobox2 ):
59+ def merge_two_infoboxes (infobox1 : models . Infobox , infobox2 : models . Infobox ):
5960 # get engines weights
6061 if hasattr (engines [infobox1 ['engine' ]], 'weight' ):
6162 weight1 = engines [infobox1 ['engine' ]].weight
@@ -91,7 +92,7 @@ def merge_two_infoboxes(infobox1, infobox2):
9192
9293 infobox1 ['urls' ] = urls1
9394
94- if 'img_src' in infobox2 :
95+ if infobox2 . get ( 'img_src' ) is not None :
9596 img1 = infobox1 .get ('img_src' , None )
9697 img2 = infobox2 .get ('img_src' )
9798 if img1 is None :
@@ -127,7 +128,7 @@ def merge_two_infoboxes(infobox1, infobox2):
127128 infobox1 ['content' ] = content2
128129
129130
130- def result_score (result ):
131+ def result_score (result : models . Result ):
131132 weight = 1.0
132133
133134 for result_engine in result ['engines' ]:
@@ -173,18 +174,18 @@ class ResultContainer:
173174
174175 def __init__ (self ):
175176 super ().__init__ ()
176- self ._merged_results = []
177- self .infoboxes = []
178- self .suggestions = set ()
179- self .answers = {}
180- self .corrections = set ()
177+ self ._merged_results : List [ models . MainResult ] = []
178+ self .infoboxes : List [ models . Infobox ] = []
179+ self .suggestions : Set [ models . Suggestion ] = set ()
180+ self .answers : Set [ models . Answer ] = {}
181+ self .corrections : Set [ models . Correction ] = set ()
181182 self ._number_of_results = []
182183 self .engine_data = defaultdict (dict )
183- self ._closed = False
184- self .paging = False
184+ self ._closed : bool = False
185+ self .paging : bool = False
185186 self .unresponsive_engines : Set [UnresponsiveEngine ] = set ()
186187 self .timings : List [Timing ] = []
187- self .redirect_url = None
188+ self .redirect_url : Optional [ str ] = None
188189 self .on_result = lambda _ : True
189190 self ._lock = RLock ()
190191
@@ -234,7 +235,7 @@ def extend(self, engine_name, results):
234235 if not self .paging and standard_result_count > 0 and engine_name in engines and engines [engine_name ].paging :
235236 self .paging = True
236237
237- def _merge_infobox (self , infobox ):
238+ def _merge_infobox (self , infobox : models . Infobox ):
238239 add_infobox = True
239240 infobox_id = infobox .get ('id' , None )
240241 infobox ['engines' ] = set ([infobox ['engine' ]])
@@ -249,7 +250,7 @@ def _merge_infobox(self, infobox):
249250 if add_infobox :
250251 self .infoboxes .append (infobox )
251252
252- def _is_valid_url_result (self , result , error_msgs ):
253+ def _is_valid_url_result (self , result : models . UrlResult , error_msgs ):
253254 if 'url' in result :
254255 if not isinstance (result ['url' ], str ):
255256 logger .debug ('result: invalid URL: %s' , str (result ))
@@ -269,7 +270,7 @@ def _is_valid_url_result(self, result, error_msgs):
269270
270271 return True
271272
272- def _normalize_url_result (self , result ):
273+ def _normalize_url_result (self , result : models . UrlResult ):
273274 """Return True if the result is valid"""
274275 result ['parsed_url' ] = urlparse (result ['url' ])
275276
@@ -290,7 +291,7 @@ def _normalize_url_result(self, result):
290291 if result .get ('content' ):
291292 result ['content' ] = WHITESPACE_REGEX .sub (' ' , result ['content' ])
292293
293- def __merge_url_result (self , result , position ):
294+ def __merge_url_result (self , result : models . UrlResult , position : int ):
294295 result ['engines' ] = set ([result ['engine' ]])
295296 with self ._lock :
296297 duplicated = self .__find_duplicated_http_result (result )
@@ -302,7 +303,7 @@ def __merge_url_result(self, result, position):
302303 result ['positions' ] = [position ]
303304 self ._merged_results .append (result )
304305
305- def __find_duplicated_http_result (self , result ):
306+ def __find_duplicated_http_result (self , result : models . UrlResult ):
306307 result_template = result .get ('template' )
307308 for merged_result in self ._merged_results :
308309 if 'parsed_url' not in merged_result :
@@ -320,7 +321,7 @@ def __find_duplicated_http_result(self, result):
320321 return merged_result
321322 return None
322323
323- def __merge_duplicated_http_result (self , duplicated , result , position ):
324+ def __merge_duplicated_http_result (self , duplicated : models . UrlResult , result : models . UrlResult , position : int ):
324325 # using content with more text
325326 if result_content_len (result .get ('content' , '' )) > result_content_len (duplicated .get ('content' , '' )):
326327 duplicated ['content' ] = result ['content' ]
@@ -341,7 +342,7 @@ def __merge_duplicated_http_result(self, duplicated, result, position):
341342 duplicated ['url' ] = result ['parsed_url' ].geturl ()
342343 duplicated ['parsed_url' ] = result ['parsed_url' ]
343344
344- def __merge_result_no_url (self , result , position ):
345+ def __merge_result_no_url (self , result : models . KeyValueResult , position : int ):
345346 result ['engines' ] = set ([result ['engine' ]])
346347 result ['positions' ] = [position ]
347348 with self ._lock :
0 commit comments