a
    }c                      @   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__ R/var/www/html/django/DPS/env/lib/python3.9/site-packages/geopy/geocoders/tomtom.pyr      s    'zTomTom.__init__TF)exactly_oner   limit	typeaheadlanguagec                C   s   |  |}| ||d< |r,tt||d< |r8d|d< |rD||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#   paramsZ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)   )Z_coerce_point_to_string_reverse_paramsr   r.   r/   r   r0   r   r   r1   r   r2   r   _parse_reverse_jsonr4   )
r   r'   r    r   r#   r9   r5   Zquoted_positionr6   r7   r   r   r   reverse   s    !

zTomTom.reversec                 C   s   |rdS dS )Ntruefalser   )r   Z
bool_valuer   r   r   r+      s    zTomTom._boolean_valuec                 C   s
   d| j iS Nkeyr   )r   Zformatted_queryr   r   r   r*      s    zTomTom._geocode_paramsc                 C   s
   d| j iS r?   rA   )r   r9   r   r   r   r:      s    zTomTom._reverse_paramsc                    s@   |r|d sd S |r&  |d d S  fdd|d D S d S )Nresultsr   c                    s   g | ]}  |qS r   _parse_search_result.0resultr   r   r   
<listcomp>   s   z&TomTom._parse_json.<locals>.<listcomp>rC   r   	resourcesr    r   rH   r   r3      s    
zTomTom._parse_jsonc                 C   s0   |d d }|d d }t |d d ||f|S )Nr9   ZlatZlonaddressfreeformAddressr	   r   rG   ZlatitudeZ	longituder   r   r   rD      s
    zTomTom._parse_search_resultc                    s@   |r|d sd S |r&  |d d S  fdd|d D S d S )N	addressesr   c                    s   g | ]}  |qS r   _parse_reverse_resultrE   rH   r   r   rI      s   z.TomTom._parse_reverse_json.<locals>.<listcomp>rP   rJ   r   rH   r   r;      s    
zTomTom._parse_reverse_jsonc                 C   s*   |d  d\}}t|d d ||f|S )Nr9   ,rL   rM   )splitr
   rN   r   r   r   rQ      s    zTomTom._parse_reverse_resultc                 C   sH   t |tsd S |jd u s"|jd u r&d S |jdkrDd|jv rDtd|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   r8   r<   r+   r*   r:   r3   rD   r;   rQ   rX   __classcell__r   r   r   r   r      s:   7=.

r   N)	functoolsr   urllib.parser   r   Zgeopy.adaptersr   Z	geopy.excr   Zgeopy.geocoders.baser   r   Zgeopy.locationr
   Z
geopy.utilr   __all__r   r   r   r   r   <module>   s   