
    in                     @   U d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
  ej                  e      j                  Z ej                  d      Zej"                  ed<   e G d d             Z G d	 d
e j(                        Z G d de      Z G d de      Zy)    N)	dataclass)load_module)
BaseAnswerzsearx.answererslogc                   L    e Zd ZU dZeed<   	 eed<   	 ee   ed<   	 ee   ed<   y)AnswererInfoa  Object that holds information about an answerer, these infos are shown
    to the user in the Preferences menu.

    To be able to translate the information into other languages, the text must
    be written in English and translated with :py:obj:`flask_babel.gettext`.
    namedescriptionexampleskeywordsN)__name__
__module____qualname____doc__str__annotations__list     &/root/searxng/searx/answerers/_core.pyr   r      s4     I!.3i?3i)r   r   c                       e Zd ZU dZee   ed<   	 ej                  dedee	   fd       Z
ej                  defd       Zy)Answererz!Abstract base class of answerers.r   queryreturnc                      y)z>Function that returns a list of answers to the question/query.Nr   selfr   s     r   answerzAnswerer.answer1       r   c                      y)z=Information about the *answerer*, see :py:obj:`AnswererInfo`.Nr   )r   s    r   infozAnswerer.info5   r   r   N)r   r   r   r   r   r   r   abcabstractmethodr   r   r   r!   r   r   r   r   r   +   se    +3i7MC MD$4 M M 	Ll L Lr   r   c                   8    e Zd ZdZd Zdedee   fdZde	fdZ
y)ModuleAnswererzA wrapper class for legacy *answerers* where the names (keywords, answer,
    info) are implemented on the module level (not in a class).

    .. note::

       For internal use only!
    c                     dD ]  }t        ||d       rt        d       t        |j                  t              st        d      || _        |j                  | _        y )N)r   	self_infor      )getattr
SystemExit
isinstancer   tuplemodule)r   modr	   s      r   __init__zModuleAnswerer.__init__C   sS    7 	$D3d+ m#	$ #,,.Q-r   r   r   c                 8    | j                   j                  |      S N)r-   r   r   s     r   r   zModuleAnswerer.answerN   s    {{!!%((r   c                 j    | j                   j                         }| j                  |d<   t        di |S )Nr   r   )r-   r'   r   r   )r   kwargss     r   r!   zModuleAnswerer.infoQ   s0    &&(!]]z%f%%r   N)r   r   r   r   r/   r   r   r   r   r   r!   r   r   r   r%   r%   :   s0    	%)C )D$4 )&l &r   r%   c                        e Zd ZU dZee   ed<   	  fdZd Zde	fdZ
defdZd	e	d
ee   fdZed
ee   fd       Z xZS )AnswerStorageu   A storage for managing the *answerers* of SearXNG.  With the
    :py:obj:`AnswerStorage.ask`” method, a caller can ask questions to all
    *answerers* and receives a list of the results.answerer_listc                 @    t         |           t               | _        y r1   )superr/   setr6   )r   	__class__s    r   r/   zAnswerStorage.__init___   s     Ur   c                    t         j                         D ]  }|j                  j                  d      r|j	                         r/|j
                  dk(  r | j                  d|j                   d       ^|j                         so|dz  j                         st        j                  d| dt               t        dt        |            }| j                  t!        |              y)	zLoads ``answerer.py`` modules from the python packages in
        :origin:`searx/answerers`.  The python modules are wrapped by
        :py:obj:`ModuleAnswerer`._z.pyzsearx.answerers.z.SXNGAnswererzanswerer.pyzanswerer module z4 is deprecated / migrate to searx.answerers.AnswererN)_defaultiterdirr	   
startswithis_filesuffixregister_by_fqnstemis_direxistswarningswarnDeprecationWarningr   r   registerr%   )r   fr.   s      r   load_builtinszAnswerStorage.load_builtinsc   s    
 !!# 	3Avv  %yy{qxx50$$'7x}%MN xxzq=088:&qc)]^`r "-Q8nS12	3r   fqnc                     |j                  d      \  }}}t        j                  |      }t        ||d      }|&d| d}t        j                  |       t        |      | j                   |              y)zGRegister a :py:obj:`Answerer` via its fully qualified class namen(FQN)..Nz	answerer z is not implemented)
rpartition	importlibimport_moduler)   r   critical
ValueErrorrI   )r   rL   mod_namer<   obj_namer.   code_objmsgs           r   rB   zAnswerStorage.register_by_fqny   sp     !$s 3!X%%h/3$/cU"56CLLS/!hj!r   answererc                     | j                   j                  |       |j                  D ]+  }| j                  |g       | |<   | |   j	                  |       - y)zRegister a :py:obj:`Answerer`.N)r6   addr   getappend)r   rX   _kws      r   rI   zAnswerStorage.register   sS     	x($$ 	'Cb)DIIX&	'r   r   r   c                     g }d}|j                         D ]  }|s n |r|| vr|S | |   D ]3  }|j                  |      D ]  }d| |_        |j                  |        5 |S )a
  An answerer is identified via keywords, if there is a keyword at the
        first position in the ``query`` for which there is one or more
        answerers, then these are called, whereby the entire ``query`` is passed
        as argument to the answerer function.Nz
answerer: )splitr   enginer\   )r   r   resultskeywordrX   r   s         r   askzAnswerStorage.ask   s     {{} 	G	 '-NW 	'H"//%0 '",WI 6v&'	' r   c                 \    | j                   D cg c]  }|j                          c}S c c}w r1   )r6   r!   )r   as     r   r!   zAnswerStorage.info   s"    "&"4"45Q555s   ))r   r   r   r   r9   r   r   r/   rK   r   rB   rI   r   r   rc   propertyr   r!   __classcell__)r:   s   @r   r5   r5   W   sq    7 x= 9#3,"3 "' ' j!1 . 6d<( 6 6r   r5   )r"   rP   loggingpathlibrF   dataclassesr   searx.utilsr   searx.result_types.answerr   Path__file__parentr=   	getLoggerr   Loggerr   r   ABCr   r%   dictr5   r   r   r   <module>rt      s   
      ! # 0 7<<!(('g''(9:W^^ : * * **Lsww L&X &:Q6D Q6r   