
    id#                     (   U d Z g dZddlZddlmZ ddlmZ  G d d      Zdd	d
 d dddd d dddej                  ej                  dgZ
	 dddddZ	 g ai aeed<   dededeez  defdZdededeez  defdZd ZdZdZd Zd!Zd"Zd# Zd$Zd% Zy)&zUnit conversion on the basis of `SPARQL/WIKIDATA Precision, Units and
Coordinates`_

.. _SPARQL/WIKIDATA Precision, Units and Coordinates:
   https://en.wikibooks.org/wiki/SPARQL/WIKIDATA_Precision,_Units_and_Coordinates#Quantities
)convert_from_siconvert_to_sisymbol_to_si    N)data)wikidatac                   V    e Zd ZU dZg dZee   ed<   edefd       Z	edefd       Z
y)Beauforta|  The mapping of the Beaufort_ contains values from 0 to 16 (55.6 m/s),
    wind speeds greater than 200km/h (55.6 m/s) are given as 17 Bft. Thats why
    a value of 17 Bft cannot be converted to SI.

    .. hint::

       Negative values or values greater 16 Bft (55.6 m/s) will throw a
       :py:obj:`ValueError`.

    _Beaufort: https://en.wikipedia.org/wiki/Beaufort_scale
    )g?g      ?gffffff
@g@g@gffffff%@g+@g1@g333334@gffffff8@gffffff<@gL@@gY@@ǧD@gfffffF@gffffffI@K@scalereturnc                     |dk  s|dkD  rt        d| d      d}t        | j                        D ]  \  }}||k\  s |S  |S )Nr   r
   invalid value . / the Beaufort scales from 0 to 16 (55.6 m/s))
ValueError	enumerater   )clsvaluebftmpss       %/root/searxng/searx/wikidata_units.pyfrom_sizBeaufort.from_si'   s[    19~eW4bcdd!#)), 	HCe|
	 
    c                 h    t        |      }|dk  s|dkD  rt        d| d      | j                  |   S )Nr      r   r   )roundr   r   )r   r   idxs      r   to_sizBeaufort.to_si1   s;    El7cBh~eW4bcddyy~r   N)__name__
__module____qualname____doc__r   listfloat__annotations__classmethodr   r    r   r   r	   r	      sO    
E4;  u   U  r   r	   Q11579   °Cc                     | dz   S Ngfffffq@r&   vals    r   <lambda>r-   =   s
    S6\ r   c                     | dz
  S r*   r&   r+   s    r   r-   r-   >   s
    sV| r   )si_namesymbolr   r      °Fc                     | dz   dz  dz  S )NQ|@   	   r&   r+   s    r   r-   r-   C   s    cFla/!3 r   c                     | dz  dz  dz
  S )Nr5   r4   r3   r&   r+   s    r   r-   r-   D   s    a!v5 r   Q182429Bft)C)F)L)r   )r(   r1   mir8   UNITS_BY_SI_NAMEr/   r0   r   r   c                     t        |       |   t           }t        |t        t        f      rt        |      |z  }|S  |t        |            }|S N)units_by_si_namepos_from_si
isinstancer#   int)r/   r0   r   r   s       r   r   r   w   sN    w'/<G'E3<(ew& L e%Lr   c                     t        |       |   t           }t        |t        t        f      rt        |      |z  }|S  |t        |            }|S r?   )r@   	pos_to_sirB   r#   rC   )r/   r0   r   r   s       r   r   r      sN    W%f-i8E%%&eu$ L eEl#Lr   c                     t         r	t         |    S t               D ];  }|t           }|t           }t         j	                  |      }|i }|t         |<   |||<   = t         |    S r?   )r=   r   pos_si_name
pos_symbolget)r/   itemitem_si_nameitem_symbol	by_symbols        r   r@   r@      sv     ((  	&K(:&$((6	I-6\*!%	+	& G$$r               c            
         t         rt         S t        j                  j                         D ]:  } | d   s	| d   st         j	                  | d   | d   d| d   z  | d   | d   f       < t
        D ]+  } t         j	                  | d   | d   | d   | d   | d   f       - g }t         D ]B  } t        j                  | d   d      D ]$  }|j	                  || d   | d	   | d
   | d   f       & D t         |z   a t         S )a  Generates a list of tuples, each tuple is a measure unit and the fields
    in the tuple are:

    0. Symbol of the measure unit (e.g. 'mi' for measure unit 'miles' Q253276)

    1. SI name of the measure unit (e.g. Q11573 for SI unit 'metre')

    2. Factor to get SI value from measure unit (e.g. 1mi is equal to SI 1m
       multiplied by 1609.344)

    3. Factor to get measure value from from SI value (e.g. SI 100m is equal to
       100mi divided by 1609.344)

    The returned list is sorted, the first items are created from
    ``WIKIDATA_UNITS``, the second group of items is build from
    :py:obj:`ADDITIONAL_UNITS` and items created from :py:obj:`ALIAS_SYMBOLS`.

    If you search this list for a symbol, then a match with a symbol from
    Wikidata has the highest weighting (first hit in the list), followed by the
    symbols from the :py:obj:`ADDITIONAL_UNITS` and the lowest weighting is
    given to the symbols resulting from the aliases :py:obj:`ALIAS_SYMBOLS`.

    to_si_factorr0   r/   rN   r   r   r   r&   rO   rP   )SYMBOL_TO_SIr   WIKIDATA_UNITSvaluesappendADDITIONAL_UNITSALIAS_SYMBOLSrI   )rJ   alias_itemsaliass      r   r   r      s9   4  ##**, 
DNNO^,,(N
 ! 	
XYYWX	
	
 K 
"&&tAw3 		EGGGG		
  +-Lr   a  
SELECT DISTINCT ?item ?symbol ?tosi ?tosiUnit
WHERE
{
  ?item wdt:P31/wdt:P279 wd:Q47574 .
  ?item p:P5061 ?symbolP .
  ?symbolP ps:P5061 ?symbol ;
           wikibase:rank ?rank .
  OPTIONAL {
    ?item p:P2370 ?tosistmt .
    ?tosistmt psv:P2370 ?tosinode .
    ?tosinode wikibase:quantityAmount ?tosi .
    ?tosinode wikibase:quantityUnit ?tosiUnit .
  }
  FILTER(LANG(?symbol) = "en").
}
ORDER BY ?item DESC(?rank) ?symbol
c                     t        j                         } t        j                  t              }|d   d   D ]  }|d   d   }|d   d   j                  dd      d   }|j                  di       j                  dd	      }|r|j                  dd      d   }|j                  d
i       j                  dd	      }|| vs||r|nd|rt        |      ndd| |<    | S )znFetch units from Wikidata.  Function is used to update persistence of
    :py:obj:`searx.data.WIKIDATA_UNITS`.resultsbindingsr0   r   rJ   /rN   tosiUnit tosiN)r0   r/   rS   )collectionsOrderedDictr   send_wikidata_querySARQL_REQUESTrsplitrI   r#   )r]   responseunitr0   namer/   rS   s          r   fetch_unitsrk     s     %%'G++M:H#J/ h(F|G$++C3A6((:r*..w;nnS!,Q/Gxx+//<w !&-747Cl 3GDM  Nr   )r!   __all__rc   searxr   searx.enginesr   r	   r   r   rX   rY   rT   r=   dictr$   strr#   rC   r   r   r@   rH   rG   rA   rE   r   rf   rk   r&   r   r   <module>rq      s   ?   "% %T )+	 35	 ##	 (2 
	  $ S # eck e 3  ECK E %* 
	
Eh(r   