o
    ÔèFhj&  ã                   @   st   d dl Zd dlmZ d dlmZ d dlmZmZ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 )é    N)Úpartial)Ú	urlencode)ÚGeocoderQueryErrorÚGeocoderServiceErrorÚGeocoderUnavailable)ÚDEFAULT_SENTINELÚGeocoder)ÚLocation)Úlogger)ÚWoosmapc                       s€   e Zd ZdZdZddeededdœ‡ fdd„
Zdd	„ Zdd
edddddœdd„Zdd
edddœdd„Z	ddd„Z
dd„ Z‡  ZS )r   z£Geocoder using the Woosmap Address API.

    Documentation at:
        https://developers.woosmap.com/products/address-api/geocode/

    .. versionadded:: 2.4
    z/address/geocode/jsonzapi.woosmap.comN)ÚdomainÚschemeÚtimeoutÚproxiesÚ
user_agentÚssl_contextÚadapter_factoryc          	         sD   t ƒ j||||||d� || _| d¡| _d| j| j| jf | _dS )a/  

        :param str api_key: The Private API key required by Woosmap to perform
            geocoding requests.
            API keys are managed through
            the Woosmap Console (https://console.woosmap.com/).
            Make sure to have ``Address API`` service enabled
            for your project Private API key.

        :param str domain: Domain where the target Woosmap service
            is hosted.

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

        )r   r   r   r   r   r   ú/z	%s://%s%sN)ÚsuperÚ__init__Úapi_keyÚstripr   r   Úapi_pathÚapi)	Úselfr   r   r   r   r   r   r   r   ©Ú	__class__© úP/var/www/html/loop/nvenv/lib/python3.10/site-packages/geopy/geocoders/woosmap.pyr      s   ,úzWoosmap.__init__c                 C   sZ   g }t |tjjƒr| ¡ }nt |tjjƒrt |ttfƒs|}ntdƒ‚d 	dd„ |D ƒ¡S )Nz7`components` parameter must be of type `dict` or `list`ú|c                 s   s   � | ]}d   |¡V  qdS )ú:N)Újoin)Ú.0Úitemr   r   r   Ú	<genexpr>^   s   € 

ÿz3Woosmap._format_components_param.<locals>.<genexpr>)
Ú
isinstanceÚcollectionsÚabcÚMappingÚitemsÚSequenceÚstrÚbytesÚ
ValueErrorr!   )r   Ú
componentsÚcomponent_itemsr   r   r   Ú_format_components_paramP   s   
ÿþÿ
ÿz Woosmap._format_components_paramT)ÚlimitÚexactly_oner   Úlocationr.   ÚlanguageÚcountry_code_formatc                C   s¬   || j dœ}	|r| j|dd�}
|
|	d< |r|  |¡|	d< |r"||	d< |r(||	d< |r.||	d< |r4d	|	d< d
 | jt|	ƒf¡}t d| jj	|¡ t
| j|d�}| j|||d�S )ay  
        Return a location point by address.

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

        :param int limit: Maximum number of results to be returned.
            This will be reset to one if ``exactly_one`` is True.

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

        :type location: :class:`geopy.point.Point`, list or tuple of ``(latitude,
            longitude)``, or string as ``"%(latitude)s, %(longitude)s"``.
        :param location: The center latlng to bias the search context.

        :type components: dict or list
        :param components: Geographic places to which you would like to restrict
            your results. Currently, you can use components to filter over countries.
            Countries are identified by a two character, ISO 3166-1 Alpha-2
            or a three character, ISO 3166-1 Alpha-3 compatible country code.

            Pass a list of tuples if you want to specify multiple components of
            the same type, e.g.:

                >>> [('country', 'FRA'), ('country', 'DE')]

        :param str language: The language in which to return results.
            Must be a ISO 639-1 language code.

        :param str country_code_format: Default country code format
            in responses is Alpha3.
            However, format in responses can be changed
            by specifying components in alpha2.
            Available formats: ``alpha2``, ``alpha3``.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        )ÚaddressÚprivate_keyú%(lat)s,%(lon)s©Úoutput_formatr3   r.   r4   Ú	cc_formatr1   é   ú?z%s.geocode: %s©r2   ©r   )r   Ú_coerce_point_to_stringr0   r!   r   r   r
   Údebugr   Ú__name__r   Ú_parse_jsonÚ_call_geocoder)r   Úqueryr1   r2   r   r3   r.   r4   r5   ÚparamsÚpointÚurlÚcallbackr   r   r   Úgeocodeb   s,   8þÿzWoosmap.geocode)r1   r2   r   r4   r5   c                C   sŽ   | j |dd�}|| jdœ}|r||d< |r||d< |r||d< |r%d|d< d | j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 int limit: Maximum number of results to be returned.
            This will be reset to one if ``exactly_one`` is True.

        :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: The language in which to return results.

        :param str country_code_format: Default country code format
            in responses is Alpha3.
            However, format in responses can be changed
            by specifying components in alpha2.
            Available formats: ``alpha2``, ``alpha3``.

        :rtype: ``None``, :class:`geopy.location.Location` or a list of them, if
            ``exactly_one=False``.
        r8   r9   )Úlatlngr7   r4   r;   r1   r<   r=   z%s.reverse: %sr>   r?   )r@   r   r!   r   r   r
   rA   r   rB   r   rC   rD   )r   rE   r1   r2   r   r4   r5   rK   rF   rH   rI   r   r   r   Úreverse²   s    )þzWoosmap.reversec                    sH   |  dg ¡}|  |¡ |sd S dd„ ‰ |rˆ |d ƒS ‡ fdd„|D ƒS )NÚresultsc                 S   s:   |   d¡}| d d d }| d d d }t|||f| ƒS )z6Get the location, lat, lng from a single json address.Úformatted_addressÚgeometryr3   ÚlatÚlng)Úgetr	   )r6   r3   ÚlatitudeÚ	longituder   r   r   Úparse_addressõ   s   
z*Woosmap._parse_json.<locals>.parse_addressr   c                    s   g | ]}ˆ |ƒ‘qS r   r   )r"   r6   ©rU   r   r   Ú
<listcomp>ÿ   s    z'Woosmap._parse_json.<locals>.<listcomp>)rR   Ú_check_status)r   Úresponser2   Ú	addressesr   rV   r   rC   î   s   
zWoosmap._parse_jsonc                 C   st   |  d¡}|dkrd S |dkrd S |  d¡}|dkr t|pdƒ‚|dkr*t|p(dƒ‚|d	kr4t|p2d
ƒ‚t|p8dƒ‚)NÚstatusÚOKÚZERO_RESULTSÚerror_messageÚINVALID_REQUESTz,Invalid request or missing address or latlngÚREQUEST_DENIEDz2Your request was denied. Please check your API KeyÚUNKNOWN_ERRORzServer errorzUnknown error)rR   r   r   r   )r   rY   r[   r^   r   r   r   rX     s"   

ÿÿzWoosmap._check_status)T)rB   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r0   rJ   rL   rC   rX   Ú__classcell__r   r   r   r   r      s8    ö8öTø
<r   )Úcollections.abcr&   Ú	functoolsr   Úurllib.parser   Ú	geopy.excr   r   r   Úgeopy.geocoders.baser   r   Úgeopy.locationr	   Ú
geopy.utilr
   Ú__all__r   r   r   r   r   Ú<module>   s    