o
    ÔèFh08  ã                   @   s’   d dl Z d dl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e  d	e j¡Zd
d„ ZG dd„ de	ƒZG dd„ de	ƒZdS )é    N)Úpartial)Ú	urlencode)Úexc)ÚDEFAULT_SENTINELÚGeocoder)ÚLocation)Úlogger)Ú
What3WordsÚWhat3WordsV3z-[^\W\d\_]+\.{1,1}[^\W\d\_]+\.{1,1}[^\W\d\_]+$c                 C   s   t  | ¡sdS dS )z)
    Check query validity with regex
    FT)Ú_MULTIPLE_WORD_REÚmatch)Úquery© r   úS/var/www/html/loop/nvenv/lib/python3.10/site-packages/geopy/geocoders/what3words.pyÚ_check_query   s   
r   c                       sp   e Zd ZdZdZdZeededddœ‡ fdd„
Zd	d
edœdd„Zddd„Z	d	d
edœdd„Z
ddd„Z‡  ZS )r	   z¿What3Words geocoder using the legacy V2 API.

    Documentation at:
        https://docs.what3words.com/api/v2/

    .. attention::
        Consider using :class:`.What3WordsV3` instead.
    z/v2/forwardz/v2/reverseNúapi.what3words.com©ÚtimeoutÚproxiesÚ
user_agentÚssl_contextÚadapter_factoryÚdomainc                   óJ   t ƒ jd|||||d� || _d| j|| jf | _d| j|| jf | _dS )a  

        :param str api_key: Key provided by What3Words
            (https://accounts.what3words.com/register).

        :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: base api domain

            .. versionadded:: 2.4
        Úhttps©Úschemer   r   r   r   r   ú	%s://%s%sN©ÚsuperÚ__init__Úapi_keyr   Úgeocode_pathÚgeocode_apiÚreverse_pathÚreverse_api©Úselfr!   r   r   r   r   r   r   ©Ú	__class__r   r   r    (   s   &ú	zWhat3Words.__init__ÚenT©ÚlangÚexactly_oner   c                C   sh   t |ƒs	t d¡‚|| ¡ | jdœ}d | jt|ƒf¡}t 	d| j
j|¡ t| j|d�}| j|||d�S )a(  
        Return a location point for a `3 words` query. If the `3 words` address
        doesn't exist, a :class:`geopy.exc.GeocoderQueryError` exception will be
        thrown.

        :param str query: The 3-word address you wish to geocode.

        :param str lang: two character language code as supported by
            the API (https://docs.what3words.com/api/v2/#lang).

        :param bool exactly_one: Return one result or a list of results, if
            available. Due to the address scheme there is always exactly one
            result for each `3 words` address, so this parameter is rather
            useless for this geocoder.

        :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.

        :rtype: :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        ú&Search string must be 'word.word.word')Úaddrr,   Úkeyú?ú%s.geocode: %s©r-   ©r   )r   r   ÚGeocoderQueryErrorÚlowerr!   Újoinr#   r   r   Údebugr)   Ú__name__r   Ú_parse_jsonÚ_call_geocoder©r'   r   r,   r-   r   ÚparamsÚurlÚcallbackr   r   r   Úgeocode[   s   !ÿýzWhat3Words.geocodec                 C   s\   |d   d¡}|rd|d d  }|dkrt |¡‚t |¡‚dd„ }||ƒ}|r+|S |gS )úg
        Parse type, words, latitude, and longitude and language from a
        JSON response.
        ÚstatusÚcodeú Error returned by What3Words: %sÚmessagei‘  c                 S   ó\   d| v r)| d }| d }|d |d }}|r!|r!t |ƒ}t |ƒ}t|||f| ƒS t d¡‚)ú'
            Parse record.
            ÚgeometryÚwordsÚlatÚlngúError parsing result.©Úfloatr   r   ÚGeocoderParseError©ÚresourcerI   ÚpositionÚlatitudeÚ	longituder   r   r   Úparse_resourceœ   ó   
z.What3Words._parse_json.<locals>.parse_resource©Úgetr   ÚGeocoderAuthenticationFailurer5   )r'   Ú	resourcesr-   rC   Úexc_msgrU   Úlocationr   r   r   r:   Œ   s   

zWhat3Words._parse_jsonc                C   ód   |  ¡ }|  |¡|  ¡ | jdœ}d | jt|ƒf¡}t d| jj	|¡ t
| j|d�}| j|||d�S )aÇ  
        Return a `3 words` address by location point. Each point on surface has
        a `3 words` address, so there's always a non-empty response.

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

        :param str lang: two character language code as supported by the
            API (https://docs.what3words.com/api/v2/#lang).

        :param bool exactly_one: Return one result or a list of results, if
            available. Due to the address scheme there is always exactly one
            result for each `3 words` address, so this parameter is rather
            useless for this geocoder.

        :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.

        :rtype: :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.

        )Úcoordsr,   r0   r1   ú%s.reverse: %sr3   r4   ©r6   Ú_coerce_point_to_stringr!   r7   r%   r   r   r8   r)   r9   r   Ú_parse_reverse_jsonr;   r<   r   r   r   Úreverse³   ó   "ýzWhat3Words.reversec                 C   ó   |   ||¡S ©zJ
        Parses a location from a single-result reverse API call.
        ©r:   ©r'   rZ   r-   r   r   r   rb   ã   ó   zWhat3Words._parse_reverse_json©T©r9   Ú
__module__Ú__qualname__Ú__doc__r"   r$   r   r    r@   r:   rc   rb   Ú__classcell__r   r   r(   r   r	      s*    	÷7ú
1+ú0r	   c                       sn   e Zd ZdZdZdZeededddœ‡ fdd„
Zd	ed
œdd„Zddd„Z	dd	edœdd„Z
ddd„Z‡  ZS )r
   z•What3Words geocoder using the V3 API.

    Documentation at:
        https://developer.what3words.com/public-api/docs

    .. versionadded:: 2.2
    z/v3/convert-to-coordinatesz/v3/convert-to-3waNr   r   c                   r   )añ  

        :param str api_key: Key provided by What3Words
            (https://accounts.what3words.com/register).

        :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`.

        :param str domain: base api domain

            .. versionadded:: 2.4
        r   r   r   Nr   r&   r(   r   r   r    ö   s   $ú	zWhat3WordsV3.__init__T)r-   r   c                C   sb   t |ƒs	t d¡‚|| jdœ}d | jt|ƒf¡}t d| j	j
|¡ t| j|d�}| j|||d�S )a¢  
        Return a location point for a `3 words` query. If the `3 words` address
        doesn't exist, a :class:`geopy.exc.GeocoderQueryError` exception will be
        thrown.

        :param str query: The 3-word address you wish to geocode.

        :param bool exactly_one: Return one result or a list of results, if
            available. Due to the address scheme there is always exactly one
            result for each `3 words` address, so this parameter is rather
            useless for this geocoder.

        :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.

        :rtype: :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        r.   )rI   r0   r1   r2   r3   r4   )r   r   r5   r!   r7   r#   r   r   r8   r)   r9   r   r:   r;   )r'   r   r-   r   r=   r>   r?   r   r   r   r@   '  s   ÿþzWhat3WordsV3.geocodec                 C   sf   |  d¡}|dur$d|d d  }|  d¡}|dv rt |¡‚t |¡‚dd„ }||ƒ}|r0|S |gS )	rA   ÚerrorNrD   rE   rC   )Ú
MissingKeyÚ
InvalidKeyc                 S   rF   )rG   ÚcoordinatesrI   rJ   rK   rL   rM   rP   r   r   r   rU   d  rV   z0What3WordsV3._parse_json.<locals>.parse_resourcerW   )r'   rZ   r-   rp   r[   Úexc_coderU   r\   r   r   r   r:   S  s   



zWhat3WordsV3._parse_jsonr*   r+   c                C   r]   )aã  
        Return a `3 words` address by location point. Each point on surface has
        a `3 words` address, so there's always a non-empty response.

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

        :param str lang: two character language code as supported by the
            API (https://developer.what3words.com/public-api/docs#available-languages).

        :param bool exactly_one: Return one result or a list of results, if
            available. Due to the address scheme there is always exactly one
            result for each `3 words` address, so this parameter is rather
            useless for this geocoder.

        :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.

        :rtype: :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.

        )rs   Úlanguager0   r1   r_   r3   r4   r`   r<   r   r   r   rc   {  rd   zWhat3WordsV3.reversec                 C   re   rf   rg   rh   r   r   r   rb   «  ri   z What3WordsV3._parse_reverse_jsonrj   rk   r   r   r(   r   r
   ê   s(    ÷5û
,,ú0r
   )ÚreÚ	functoolsr   Úurllib.parser   Úgeopyr   Úgeopy.geocoders.baser   r   Úgeopy.locationr   Ú
geopy.utilr   Ú__all__ÚcompileÚUr   r   r	   r
   r   r   r   r   Ú<module>   s    ÿ
 P