o
    4c7                     @   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   P/var/www/html/gps/gps/lib/python3.10/site-packages/geopy/geocoders/what3words.py_check_query   s   
r   c                       sn   e Zd ZdZdZdZeede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	   zWhat3Words 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timeoutproxies
user_agentssl_contextadapter_factoryc                   N   t  jd|||||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
        httpsschemer   r   r   r   r   api.what3words.com	%s://%s%sNsuper__init__api_keyr   geocode_pathgeocode_apireverse_pathreverse_apiselfr    r   r   r   r   r   domain	__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   geocodeW   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(    	3
1+0r	   c                       sl   e Zd ZdZdZdZeede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
   zWhat3Words 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   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`.
        r   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   \  rV   z0What3WordsV3._parse_json.<locals>.parse_resourcerW   )r&   rZ   r-   rp   r[   exc_coderU   r\   r   r   r   r:   K  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   s  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&    1
,,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    
 L