o
    ej&                     @   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   zGeocoder 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__ J/var/www/html/venv/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    