
    i6-                        U d Z ddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddl
mZ ddlmZ ddlmZmZ dd	lmZmZmZmZ dd
lmZ ddlmZ ej6                  rddlZddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$m%Z%  ejL                  d      Zi Z'e(e)e*z  df   e+d<    G d dejX                        Z- G d d      Z. G d de      Z/y)z0Abstract base classes for all engine processors.    N)abstractmethodABC)default_timer)get_setting)logger)engines)get_time_for_threadget_network)histogram_observecounter_inccount_exceptioncount_error) SearxEngineAccessDeniedException)get_engine_from_settings)Engine)SearchQuery)ResultContainer)ResultLegacyResultzsearx.search.processorSuspendedStatusSUSPENDED_STATUSc                       e Zd ZU dZeed<   	 eed<   	 eed<   	 ej                  d   ed<   	 ej                  d   dz  ed	<   	 e	eef   ed
<   	 eed<   y)RequestParamsz=Basic quantity of the Request parameters of all engine types.querycategorypageno)r         
safesearch)dayweekmonthyearN
time_rangeengine_datasearxng_locale)
__name__
__module____qualname____doc__str__annotations__inttLiteraldict     1/root/searxng/searx/search/processors/abstract.pyr   r      sk    GJ;M
 K=		'"">		89D@@c3h	7  r2   r   c                   B    e Zd ZdZd Zed        Zdedz  defdZ	d Z
y)	r   zClass to handle suspend state.c                 `    t        j                         | _        d| _        d| _        d| _        y Nr    )	threadingLocklockcontinuous_errorssuspend_end_timesuspend_reasonselfs    r3   __init__zSuspendedStatus.__init__P   s(    $-NN$4	&''(#%r2   c                 0    | j                   t               k\  S N)r<   r   r>   s    r3   is_suspendedzSuspendedStatus.is_suspendedV   s    $$77r2   suspended_timeNr=   c                    | j                   5  | xj                  dz  c_        |"t        d      }t        d      }t        ||      }t	               |z   | _        || _        t        j                  d|       d d d        y # 1 sw Y   y xY w)Nr   zsearch.max_ban_time_on_failzsearch.ban_time_on_failzSuspend for %i seconds)	r:   r;   r   minr   r<   r=   r   debug)r?   rD   r=   max_banban_fails        r3   suspendzSuspendedStatus.suspendZ   s    YY 
	C""a'"%*+HI +,E F!$Wh!7$1On$DD!"0DLL1>B
	C 
	C 
	Cs   A)A??Bc                 p    | j                   5  d| _        d| _        d| _        d d d        y # 1 sw Y   y xY wr6   )r:   r;   r<   r=   r>   s    r3   resumezSuspendedStatus.resumeg   s6    YY 	%%&D"$%D!"$D		% 	% 	%s   ,5)r'   r(   r)   r*   r@   propertyrC   r-   r+   rJ   rL   r1   r2   r3   r   r   M   s=    (& 8 8CcDj C# C%r2   c                       e Zd ZU dZeed<   ddZdej                  d e	ge	f   fdZ
de	fdZ	 d dd	d
eez  de	fdZdd	deddfdZdd	deddfdZdd	de	fdZdddededz  fdZedededd	dedef
d       Zd Zd Zy)!EngineProcessorz6Base classes used for all types of request processors.engine_typec                     || _         t        |j                     j                  | _        t	        | j                   j                        }|rt        |      n| j                   j                  }t        j                  |t                     | _	        y rB   )
enginer   namer   r
   idr   
setdefaultr   suspended_status)r?   rR   keys      r3   r@   zEngineProcessor.__init__t   sc     &&-fkk&:&A&A$++**+bg$++"2"21A1L1LSRaRc1dr2   callbackc                 V    t         j                  d      s
  d       yt         j                  j                        s=t	        j
                  dt         j                  j                                 d       y fd}t        j                  |d      j                          y)a  Initialization of *this* :py:obj:`EngineProcessor`.

        If processor's engine has an ``init`` method, it is called first.
        Engine's ``init`` method is executed in a thread, meaning that the
        *registration* (the ``callback``) may occur later and is not already
        established by the return from this registration method.

        Registration only takes place if the ``init`` method is not available or
        is successfully run through.
        initTNz7Engine's init method isn't a callable (is of type: %s).Fc                  8    j                         }  |        y rB   )init_engine)eng_okrX   r?   s    r3   __init_processor_threadz;EngineProcessor.initialize.<locals>.__init_processor_thread   s    %%'FT6"r2   )targetdaemon)
hasattrrR   callablerZ   r   errortyper8   Threadstart)r?   rX   '_EngineProcessor__init_processor_threads   `` r3   
initializezEngineProcessor.initialize{   s     t{{F+T4 (()LLRTXY]YdYdYiYiTjkT5!	#
 	 7EKKMr2   returnc                     t        | j                  j                        }d}	 | j                  j                  |      }|d}|S # t        $ r/ t        j                  d| j                  j                         d}Y =w xY w)NFz4Init method of engine %s failed due to an exception.T)r   rR   rS   rZ   	Exceptionr   	exception)r?   eng_settinginit_oks      r3   r\   zEngineProcessor.init_engine   sy    .t{{/?/?@$	kk&&{3G ?G  	SUYU`U`UeUefG	s   A 5A<;A<result_containerr   exception_or_messagerJ   c                 @   t        |t              r5|j                  }t        |dd      }|dk(  rdn|dz   }||j                  z   }n|}|j                  | j                  j                  |       t        d| j                  j                  ddd       t        |t              r!t        | j                  j                  |       n t        | j                  j                  |       |r;d }t        |t              r|j                  }| j                  j                  ||       y y )	Nr(   builtinsr7   .rR   searchcountrc   )
isinstanceBaseException	__class__getattrr)   add_unresponsive_enginerR   rS   r   r   r   r   rD   rV   rJ   )r?   ro   rp   rJ   exception_classmodule_nameerror_messagerD   s           r3   handle_exceptionz EngineProcessor.handle_exception   s     *M:2<<O!/<LK +z 9"{S?PK'/*F*FFM0M001A1A=QHdkk..'7K*M:DKK,,.BC((*>?!N.0PQ!5!D!D!!)).-H	 r2   
start_timesearch_resultszlist[Result | LegacyResult]c                    |j                  | j                  j                  |       t               |z
  }t	               }|j                  | j                  j                  ||       t        d| j                  j                  ddd       t        |d| j                  j                  dd       |$t        |d| j                  j                  dd       y y )NrR   rt   ru   
successfultimetotalhttp)extendrR   rS   r   r	   
add_timingr   r   )r?   ro   r   r   engine_timepage_load_times         r3   _extend_container_basicz'EngineProcessor._extend_container_basic   s     	 0 0.A#o
2,.##DKK$4$4k>RHdkk..'<P+x1A1A67S%nh8H8H&RXY &r2   z list[Result | LegacyResult]|Nonec                     t        t        j                         dd      r| j                  |dd       y || j	                  |||       | j
                  j                          y )N_timeoutFtimeout)ry   r8   current_threadr~   r   rV   rL   )r?   ro   r   r   s       r3   extend_containerz EngineProcessor.extend_container   sY     9++-z5A!!"2IuE ),,-=z>Z!!((*r2   c                     | j                   j                  r=|j                  | j                  j                  | j                   j
                  d       yy)NT)	suspendedF)rV   rC   rz   rR   rS   r=   )r?   ro   s     r3   extend_container_if_suspendedz-EngineProcessor.extend_container_if_suspended   sJ      --44  $"7"7"F"FRV 5  r2   search_queryr   engine_categoryNc           	         |j                   dkD  r| j                  j                  sy| j                  j                  xs t	        d      }|r||j                   k  ry|j
                  r| j                  j                  sy|j                  ||j                   |j                  |j
                  |j                  j                  | j                  j                  i       |j                  d}t        | j                  d      r1| j                  j                  r| j                  j                  |d<   |S |j                  |d<   |S )a  Returns a dictionary with the :ref:`request parameters <engine
        request arguments>` (:py:obj:`RequestParams`), if the search condition
        is not supported by the engine, ``None`` is returned:

        - *time range* filter in search conditions, but the engine does not have
           a corresponding filter
        - page number > 1 when engine does not support paging
        - page number > ``max_page``

        r   Nzsearch.max_page)r   r   r   r   r$   r%   r&   language)r   rR   pagingmax_pager   r$   time_range_supportr   r   r%   getrS   langra   r   )r?   r   r   r   paramss        r3   
get_paramszEngineProcessor.get_params   s    "4;;+=+= ;;''I;7H+I<#6#66 ""4;;+I+I "'''"))&11&11'33778H8H"M*//!
" 4;;
+0D0D!%!5!5F:  ".!2!2F:r2   r   r   timeout_limitc                      y rB   r1   )r?   r   r   ro   r   r   s         r3   rt   zEngineProcessor.search  s     	r2   c                     i S rB   r1   r>   s    r3   	get_testszEngineProcessor.get_tests$      	r2   c                     i S rB   r1   r>   s    r3   get_default_testsz!EngineProcessor.get_default_tests(  r   r2   )rR   zEngine|types.ModuleType)F)r'   r(   r)   r*   r+   r,   r@   r.   Callableboolrh   r\   rw   r~   floatr   r   r   r   r   r   rt   r   r   r1   r2   r3   rO   rO   o   s8   @eN1::/@$.G.M#N N8T $ 	I+I ,c1I 	I8Z+Z Z 6	Z"+++ + ;	+>O TX .} .s .}_cOc .`   ,	
   r2   rO   )0r*   typingr.   loggingr8   abcr   r   timeitr   searxr   r   searx.enginesr   searx.networkr	   r
   searx.metricsr   r   r   r   searx.exceptionsr   searx.utilsr   TYPE_CHECKINGtypessearx.enginelibr   searx.search.modelsr   searx.resultsr   searx.result_typesr   r   getChildr   r0   r-   r+   r,   	TypedDictr   r   rO   r1   r2   r3   <module>r      s    6    #     ! : V V = 0??&/-7 
1	279 $sSy"334 9+ AKK + \% %D{c {r2   