
    in                         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mZ ddlmZ ej                  rddlmZ ddlmZ dd	l
mZ g d
Z G d de      ZdZdedz  fdZy)a  A plugin for converting measured values from one unit to another unit (a
unit converter).

The plugin looks up the symbols (given in the query term) in a list of
converters, each converter is one item in the list (compare
:py:obj:`ADDITIONAL_UNITS`).  If the symbols are ambiguous, the matching units
of measurement are evaluated.  The weighting in the evaluation results from the
sorting of the :py:obj:`list of unit converters<symbol_to_si>`.
    N)gettext
get_locale)symbol_to_si)Plugin
PluginInfo)EngineResults)SearchWithPlugins)SXNG_Request)	PluginCfg)intoasc                   <     e Zd ZdZdZd
 fdZdddddefd	Z xZS )
SXNGPluginzSConvert between units.  The result is displayed in area for the
    "answers".
    unit_converterreturnc                     t         |   |       t        | j                  t	        d      t	        d      d      | _        y )NzUnit converter pluginzConvert between unitsgeneral)idnamedescriptionpreference_section)super__init__r   r   r   info)selfplg_cfg	__class__s     -/root/searxng/searx/plugins/unit_converter.pyr   zSXNGPlugin.__init__%   s8    !ww01 78(	
	    requestr
   searchr	   c                    t               }|j                  j                  dkD  r|S |j                  j                  }|j	                  d      }t        |      dk  r|S |D ]~  }t        D ]s  }||k(  s	|j	                  |d      \  }}	t        |j                         |	j                               }
|
sI|j                  |j                  j                  |
             u  |S )N       )answer)r   search_querypagenoquerysplitlenCONVERT_KEYWORDS_parse_text_and_convertstripaddtypesAnswer)r   r!   r"   resultsr*   query_parts
query_partkeyword
from_queryto_query
target_vals              r   post_searchzSXNGPlugin.post_search/   s    / %%)N##))kk#&{aN% 	MJ+ M(+0;;w+B(J!89I9I9KX^^M]!^J!GMM$8$8
$8$KLM	M r    )r   r   r   N)	__name__
__module____qualname____doc__r   r   r   r:   __classcell__)r   s   @r   r   r      s0     
B
> ;N S` r    r   aY  
(?P<sign>[-+]?)         # +/- or nothing for positive
(\s*)                   # separator: white space or nothing
(?P<number>[\d\.,]*)    # number: 1,000.00 (en) or 1.000,00 (de)
(?P<E>[eE][-+]?\d+)?    # scientific notation: e(+/-)2 (*10^2)
(\s*)                   # separator: white space or nothing
(?P<unit>\S+)           # unit of measure
r   c                 2   | r|sy t        j                  t        | t         j                        }|xr |j	                  d      |j	                  d      fsy g g }}t               D ]I  \  }}}}}	||j	                  d      k(  r|j                  ||f       ||k(  s6|j                  |||	f       K |r|sy d x}
x}}|D ]$  }|D ]  }|d   |d   k(  s|d   }
|d   }|d   } & |
r|sy t               xs d}|j	                  d      |j	                  d      z   |j	                  d      xs d	z   }t        j                  j                  ||
      }t        |
t        t        f      rt        |      |
z  }n |
t        |            }t        |t        t        f      rt        |      |z  }n |t        |            }|j	                  d      r"t        j                  j                  ||
      }n"t        j                  j                  ||d      }| d| S )Nnumberunitr   r$      en_USsignE )localez#,##0.##########;-#)rH   formatr%   )rematch
RE_MEASUREVERBOSEgroupr   appendr   babelnumbersparse_decimal
isinstancefloatintformat_scientificformat_decimal)r7   r8   measuredsource_listtarget_listsymbolsi_namefrom_sito_siorig_symbolsource_to_sitarget_from_sitarget_symbolsourcetarget_localevalueresults                     r   r.   r.   R   s    8xx
J

;H118>>&3IJ  "2K8D @4%X^^F++/0X+>?@ K488L8>M  *! 	*FayF1I%%ay!' &q			** ^l%gGNN6"X^^H%==PSATAZXZ[EMM''g'>E ,-e|+U5\* .5#,/e~-uU|,~~c00w0G--eGLa-bXQ}o&&r    )r>   typingrJ   babel.numbersrP   flask_babelr   r   searx.wikidata_unitsr   searx.pluginsr   r   searx.result_typesr   TYPE_CHECKINGsearx.searchr	   searx.extended_typesr
   r   r-   r   rL   strr.    r    r   <module>rs      sa     	  + - , ,	.1' & & &T
D'S4Z D'r    