o
    ÔèFh   ã                   @   st   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZ d dlmZ d dlmZ dZG d	d
„ d
eƒZdS )é    )Úpartial)ÚquoteÚ	urlencode)ÚAdapterHTTPError)ÚGeocoderQuotaExceeded)ÚDEFAULT_SENTINELÚGeocoder©ÚLocation)Úlogger)ÚTomTomc                       s¢   e Zd ZdZdZdZdeededddœ‡ fdd„
Zd	edd
ddœdd„Zd	eddœdd„Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Z‡  ZS )!r   zuTomTom geocoder.

    Documentation at:
        https://developer.tomtom.com/search-api/search-api-documentation
    z /search/2/geocode/%(query)s.jsonz*/search/2/reverseGeocode/%(position)s.jsonNzapi.tomtom.com)ÚschemeÚtimeoutÚproxiesÚ
user_agentÚssl_contextÚadapter_factoryÚdomainc          	         sJ   t ƒ j||||||d� || _d| j|| jf | _d| j|| jf | _dS )a7  
        :param str api_key: TomTom API key.

        :param str scheme:
            See :attr:`geopy.geocoders.options.default_scheme`.

        :param int timeout:
            See :attr:`geopy.geocoders.options.default_timeout`.

        :param dict proxies:
            See :attr:`geopy.geocoders.options.default_proxies`.

        :param str user_agent:
            See :attr:`geopy.geocoders.options.default_user_agent`.

        :type ssl_context: :class:`ssl.SSLContext`
        :param ssl_context:
            See :attr:`geopy.geocoders.options.default_ssl_context`.

        :param callable adapter_factory:
            See :attr:`geopy.geocoders.options.default_adapter_factory`.

            .. versionadded:: 2.0

        :param str domain: Domain where the target TomTom service
            is hosted.
        )r   r   r   r   r   r   z	%s://%s%sN)ÚsuperÚ__init__Úapi_keyr   Úgeocode_pathÚapiÚreverse_pathÚapi_reverse)	Úselfr   r   r   r   r   r   r   r   ©Ú	__class__© úO/var/www/html/loop/nvenv/lib/python3.10/site-packages/geopy/geocoders/tomtom.pyr      s   'úzTomTom.__init__TF)Úexactly_oner   ÚlimitÚ	typeaheadÚlanguagec                C   s    |   |¡}|  |¡|d< |rtt|ƒƒ|d< |rd|d< |r"||d< t| d¡ƒ}d | jt|d� t	|ƒf¡}	t
 d| jj|	¡ t| j|d	�}
| j|	|
|d
�S )a=  
        Return a location point by address.

        :param str query: The address or query you wish to geocode.

        :param bool exactly_one: Return one result or a list of results, if
            available.

        :param int timeout: Time, in seconds, to wait for the geocoding service
            to respond before raising a :class:`geopy.exc.GeocoderTimedOut`
            exception. Set this only if you wish to override, on this call
            only, the value set during the geocoder's initialization.

        :param int limit: Maximum amount of results to return from the service.
            Unless exactly_one is set to False, limit will always be 1.

        :param bool typeahead: If the "typeahead" flag is set, the query
            will be interpreted as a partial input and the search will
            enter predictive mode.

        :param str language: Language in which search results should be
            returned. When data in specified language is not
            available for a specific field, default language is used.
            List of supported languages (case-insensitive):
            https://developer.tomtom.com/online-search/online-search-documentation/supported-languages

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        r"   r!   Ú1r#   úutf-8ú?)Úqueryz%s.geocode: %s©r    ©r   )Ú_geocode_paramsÚ_boolean_valueÚstrÚintr   ÚencodeÚjoinr   Údictr   r   Údebugr   Ú__name__r   Ú_parse_jsonÚ_call_geocoder)r   r'   r    r   r!   r"   r#   ÚparamsÚquoted_queryÚurlÚcallbackr   r   r   ÚgeocodeJ   s   
'ÿzTomTom.geocode)r    r   r#   c          
      C   s|   |   |¡}|  |¡}|r||d< t| d¡ƒ}d | jt|d� t|ƒf¡}t 	d| j
j|¡ t| j|d�}	| j||	|d�S )a»  
        Return an address by location point.

        :param query: The coordinates for which you wish to obtain the
            closest human-readable addresses.
        :type query: :class:`geopy.point.Point`, list or tuple of ``(latitude,
            longitude)``, or string as ``"%(latitude)s, %(longitude)s"``.

        :param bool exactly_one: Return one result or a list of results, if
            available.

        :param int timeout: Time, in seconds, to wait for the geocoding service
            to respond before raising a :class:`geopy.exc.GeocoderTimedOut`
            exception. Set this only if you wish to override, on this call
            only, the value set during the geocoder's initialization.

        :param str language: Language in which search results should be
            returned. When data in specified language is not
            available for a specific field, default language is used.
            List of supported languages (case-insensitive):
            https://developer.tomtom.com/online-search/online-search-documentation/supported-languages

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        r#   r%   r&   )Úpositionz%s.reverse: %sr(   r)   )Ú_coerce_point_to_stringÚ_reverse_paramsr   r.   r/   r   r0   r   r   r1   r   r2   r   Ú_parse_reverse_jsonr4   )
r   r'   r    r   r#   r:   r5   Úquoted_positionr7   r8   r   r   r   Úreverseƒ   s   
!
ÿzTomTom.reversec                 C   s   |rdS dS )NÚtrueÚfalser   )r   Ú
bool_valuer   r   r   r+   ±   s   zTomTom._boolean_valuec                 C   ó
   d| j iS ©NÚkey©r   )r   Úformatted_queryr   r   r   r*   ´   ó   ÿzTomTom._geocode_paramsc                 C   rC   rD   rF   )r   r:   r   r   r   r<   ¹   rH   zTomTom._reverse_paramsc                    ó<   |r|d sd S |rˆ   |d d ¡S ‡ fdd„|d D ƒS )NÚresultsr   c                    ó   g | ]}ˆ   |¡‘qS r   ©Ú_parse_search_result©Ú.0Úresult©r   r   r   Ú
<listcomp>Å   ó    ÿz&TomTom._parse_json.<locals>.<listcomp>rL   ©r   Ú	resourcesr    r   rQ   r   r3   ¾   ó   
ÿzTomTom._parse_jsonc                 C   s0   |d d }|d d }t |d d ||f|ƒS )Nr:   ÚlatÚlonÚaddressÚfreeformAddressr	   ©r   rP   ÚlatitudeÚ	longituder   r   r   rM   È   s
   ÿzTomTom._parse_search_resultc                    rI   )NÚ	addressesr   c                    rK   r   ©Ú_parse_reverse_resultrN   rQ   r   r   rR   Õ   rS   z.TomTom._parse_reverse_json.<locals>.<listcomp>r_   rT   r   rQ   r   r=   Î   rV   zTomTom._parse_reverse_jsonc                 C   s*   |d   d¡\}}t|d d ||f|ƒS )Nr:   ú,rY   rZ   )Úsplitr
   r[   r   r   r   r`   Ø   s   ÿzTomTom._parse_reverse_resultc                 C   sL   t |tƒsd S |jd u s|jd u rd S |jdkr"d|jv r$tdƒ|‚d S d S )Ni�  zDeveloper Over Qps)Ú
isinstancer   Ústatus_codeÚtextr   )r   Úerrorr   r   r   Ú_geocoder_exception_handlerÝ   s   

ÿz"TomTom._geocoder_exception_handler)r2   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r9   r?   r+   r*   r<   r3   rM   r=   r`   rg   Ú__classcell__r   r   r   r   r      s<    ö7ø=ú.

r   N)Ú	functoolsr   Úurllib.parser   r   Úgeopy.adaptersr   Ú	geopy.excr   Úgeopy.geocoders.baser   r   Úgeopy.locationr
   Ú
geopy.utilr   Ú__all__r   r   r   r   r   Ú<module>   s    