
    i&                        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Zddl	Z	ddl
mZmZ ddlmZ ej                  rddlmZ  ej$                  d      Z e ee            Zi dd	d
dddddddgddddded   d   dddddddi dddddg ddZeeeez  eej4                     z  eeej4                  f   z  ez  f   ed<   d Zdg iZd!ed<   i Zd"ed<   	 i Z 	 d#ejB                  fd$Z"d%eeej4                  f   d&d'fd(Z#d)d*d+efd,Z$d)d-d%eeej4                  f   fd.Z%d7d/Z&d7d0Z'd7d1Z(d7d2Z)d)d-d%eeej4                  f   d&efd3Z*d7d4Z+d5eeeej4                  f      fd6Z,y)8zLoad and initialize the ``engines``, see :py:func:`load_engines` and register
:py:obj:`engine_shortcuts`.

usage::

    load_engines( settings['engines'] )

    N)realpathdirname)loggersettings)load_module)Engineenginesengine_typeonlinepagingFtime_range_support
safesearch
categoriesgeneralenable_httpshortcut-timeoutoutgoingrequest_timeoutdisplay_error_messagesTdisabledinactiveaboutusing_tor_proxysend_accept_language_headertokensmax_pageENGINE_DEFAULT_ARGSotherz(dict[str, list[Engine|types.ModuleType]]z$dict[str, Engine | types.ModuleType]modulec                     t        | dd       }|r>t        j                  |      r(d| j                   d|j                   d}t	        |      y y )Nnetworkztype of z.network is a module (z), expected a string)getattrinspectismodule__name__	TypeError)r!   objmsgs      '/root/searxng/searx/engines/__init__.pycheck_engine_moduler,   E   sR    
 &)T
*C
w$))?~Mabn %s    engine_datareturnz Engine | types.ModuleType | Nonec                 f   | j                  d      }|t        j                  d       yd|v r%t        j                  dj                  |             y|j	                         |k7  r9t        j
                  dj                  |             |j	                         }|| d<   | j                  d      }|%t        j                  dj                  |             y	 t        |d	z   t              }t%               t'        ||        t)        |       ddlm} |j/                         }|j1                  |       t3        |      syt5        |      ryt7        ||       t9        ||       syt;        d |j<                  D              s|j<                  j?                  t@               |S # t        t        t        t        t        t        f$ r= t        j                  d
j                  |             t        j                   d       Y t"        $ r' t        j                  dj                  |             Y yw xY w)a  Load engine from ``engine_data``.

    :param dict engine_data:  Attributes from YAML ``settings:engines/<engine>``
    :return: initialized namespace of the ``<engine>``.

    1. create a namespace and load module of the ``<engine>``
    2. update namespace with the defaults from :py:obj:`ENGINE_DEFAULT_ARGS`
    3. update namespace with values from ``engine_data``

    If engine *is active*, return namespace of the engine, otherwise return
    ``None``.

    This function also returns ``None`` if initialization of the namespace fails
    for one of the following reasons:

    - engine name contains underscore
    - engine name is not lowercase
    - required attribute is not set :py:func:`is_missing_required_attributes`

    nameNz&An engine does not have a "name" field_z%Engine name contains underscore: "{}"z;Engine name is not lowercase: "{}", converting to lowercaseenginez7The "engine" field is missing for the engine named "{}"z.pyzFatal exception in engine "{}"   zCannot load engine "{}"r   )EngineTraitsMapc              3   2   K   | ]  }|t         d    v   yw)categories_as_tabsN)r   ).0cats     r+   	<genexpr>zload_engine.<locals>.<genexpr>   s     Rsh344Rs   )!getr   errorformatlowerwarningr   
ENGINE_DIRSyntaxErrorKeyboardInterrupt
SystemExitSystemErrorImportErrorRuntimeError	exceptionsysexitBaseExceptionr,   update_engine_attributesupdate_attributes_for_torsearx.enginelib.traitsr5   	from_data
set_traitsis_engine_activeis_missing_required_attributesset_loggerscall_engine_setupanyr   appendDEFAULT_CATEGORY)r.   engine_namemodule_namer3   r5   	trait_maps         r+   load_enginerZ   Q   s   . //&)K=>
k<CCKPQk)T[[\ghi!''))F //(+KNUUVabc[50*= V[1f% 7))+I F#%f-$V[1R@Q@QRR  !12MA *J[R^_ 9@@MN 299+FGs   F" "AH0,H0/H0r3   zEngine|types.ModuleTyperW   c                 J   t        j                  |      | _         t        j                  j	                         }|j                         D ]X  \  }}|j                  d      s|dk7  st        |d      r+|j                  d      d   }t        j                  |      |_         Z y )Nzsearx.engineszsearx.engines.__init__r   .)	r   getChildrH   modulescopyitems
startswithhasattrsplit)r3   rW   r_   rX   r!   module_engine_names         r+   rR   rR      s    OOK0FM kk G&}} @V""?377FH-!,!2!23!7!;"OO,>?FM@r-   Engine | types.ModuleTypec           	         |j                         D ]  \  }}|dk(  rJt        |t              r2t        t	        t        j
                  |j                  d                  }|| _        Ut        | d      r|dk(  ri | j                  |d   | _	        t        | ||        t        j                         D ]2  \  }}t        | |      rt        | |t        j                  |             4 y )Nr   ,r   )ra   
isinstancestrlistmapstriprd   r   rc   r   setattrr   r`   deepcopy)r3   r.   
param_nameparam_valuearg_name	arg_values         r+   rK   rK      s    #.#4#4#6 5
K%+s+"3syy+2C2CC2H#IJ +FVW%**?CfllCk'.BCFLFJ45  388: @)vx(FHdmmI&>?@r-   c                     t        |       rYt        | d      rL| j                  t        | dd      z   | _        | xj
                  t        d   j                  dd      z  c_        y y y )N	onion_urlsearch_path r   extra_proxy_timeoutr   )r   rc   ru   r$   
search_urlr   r   r;   r3   s    r+   rL   rL      sY    v76;#?",,wv}b/QQ(:.223H!LL $@r-   c                     d}t        |       D ]R  }|j                  d      rt        | |      "t        j                  dj                  | j                  |             d}T |S )zAn attribute is required when its name doesn't start with ``_`` (underline).
    Required attributes must not be ``None``.

    Fr2   z*Missing engine config attribute: "{0}.{1}"T)dirrb   r$   r   r<   r=   r1   )r3   missingengine_attrs      r+   rQ   rQ      sa    
 G6{ %%c*wv{/K/SLLELLV[[ZefgG Nr-   c                 P    t         d   j                  d      xs t        | dd      S )z<Return True if the engine configuration declares to use Tor.r   r   F)r   r;   r$   rz   s    r+   r   r      s)    J##$56c'&J[]b:ccr-   c                 V    | j                   du ryd| j                  v rt        |       syy)NTFonions)r   r   r   rz   s    r+   rP   rP      s.    $ 6$$$_V-Dr-   c                 `   d}t        | dd       }|d}n=t        |      s t        j                  dt	        |             n	 | j                  |      }|s t        j                  d| j                         |S # t        $ r.}t        j                  dj                  |             Y d }~Vd }~ww xY w)NFsetupTz7engine's setup method isn't a callable (is of type: %s)zexception : {0}z?%s: Engine setup was not successful, engine is set to inactive.)
r$   callabler   r<   typer   	ExceptionrG   r=   r1   )r3   r.   setup_ok
setup_funces        r+   rS   rS      s    H$/Jj!NPTU_P`a	:||K0H VX^XcXcdO  	:.55a899	:s   A6 6	B-?$B((B-c                 $   | j                   t        v rCt        j                  dj	                  | j                                t        j                  d       | t        | j                   <   | j                  t        v rCt        j                  dj	                  | j                               t        j                  d       | j                   t        | j                  <   | j                  D ]'  }t        j                  |g       j                  |        ) y )Nz(Engine config error: ambiguous name: {0}r4   z,Engine config error: ambiguous shortcut: {0})r1   r	   r   r<   r=   rH   rI   r   engine_shortcutsr   
setdefaultrU   )r3   category_names     r+   register_enginer      s    {{g?FFv{{ST!GFKK**CJJ6??[\(.V__%** @mR077?@r-   engine_listc                 V   t         j                          t        j                          t        j                          g t        d<   | D ]Z  }|j	                  d      du rt        |      }|rt        |       0t        j                  d|j	                  dd             d|d<   \ t         S )z,usage: ``engine_list = settings['engines']``r   r   Tz1loading engine %s failed: set engine to inactive!r1   z???)	r	   clearr   r   r;   rZ   r   r   r<   )r   r.   r3   s      r+   load_enginesr   
  s    MMOJy" 
+??:&$.[)F# LLLkoo^dfkNlm&*K
#
+ Nr-   )r3   rf   )-__doc__typingtrH   r`   os.pathr   r   typesr%   searxr   r   searx.utilsr   TYPE_CHECKINGsearx.enginelibr   r^   __file__r@   r   dictrj   intrk   Anybool__annotations__rV   r   r	   r   
ModuleTyper,   rZ   rR   rK   rL   rQ   r   rP   rS   r   r    r-   r+   <module>r      sw    
  %   " #??&		#Xh'(
U8U eU %	U
 %U 9+U 5U U x
#$56U dU U U RU uU  "4!U" b#U$ %U T#sSy4;6c155j9IIDPPQ *  :CR
6 H24	/ 4 ) 	 0 0 	KT#quu*- K2T K\@1 @ @&@%@ @tTWYZY^Y^T^O_ @$M
d
	9 SRSRWRWZHX ]a &@d4QUU
#34 r-   