o
    þÞh·$  ã                   @   sÈ   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ d	gZzddlZW n eyF   d
ZY n	w dZeddg7 Zh d£ZG dd„ deƒZG dd„ dƒZdS )aU  
This module houses the GeoIP2 object, a wrapper for the MaxMind GeoIP2(R)
Python API (https://geoip2.readthedocs.io/). This is an alternative to the
Python GeoIP2 interface provided by MaxMind.

GeoIP(R) is a registered trademark of MaxMind, Inc.

For IP-based geolocation, this module requires the GeoLite2 Country and City
datasets, in binary format (CSV will not work!). The datasets may be
downloaded from MaxMind at https://dev.maxmind.com/geoip/geoip2/geolite2/.
Grab GeoLite2-Country.mmdb.gz and GeoLite2-City.mmdb.gz, and unzip them in the
directory corresponding to settings.GEOIP_PATH.
é    N)Úsettings)ÚValidationError)Úvalidate_ipv46_address)Úto_path)ÚRemovedInDjango60Warning)Úcached_propertyÚ
HAS_GEOIP2FTÚGeoIP2ÚGeoIP2Exception>   úGeoIP2-CityúGeoLite2-CityúDBIP-City-LiteúGeoIP2-CountryúGeoLite2-CountryúDBIP-Country-Litec                   @   s   e Zd ZdS )r
   N)Ú__name__Ú
__module__Ú__qualname__© r   r   úY/var/www/html/premium_crap/venv/lib/python3.10/site-packages/django/contrib/gis/geoip2.pyr
   1   s    c                   @   sÔ   e Zd ZdZdZdZdZdZeeeeeefƒZ	dZ
dZd*dd„Zd	d
„ Zdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZddœdd„Zdd„ Zdd„ Zdd„ Zdd„ Zd+d d!„Zd"d#„ Zd$d%„ Zd&d'„ Zed(d)„ ƒZdS ),r	   r   é   é   é   é   Nc                 C   sÀ   || j vrtd| ƒ‚|pttddƒ}|pttddƒ}|p"ttddƒ}|s)tdƒ‚t|ƒ}||| || fD ]}| ¡ rJ|| _tjj	||d	�| _
 nq6td
ƒ‚| jj}|tvr^td|› �ƒ‚dS )aI  
        Initialize the GeoIP object. No parameters are required to use default
        settings. Keyword arguments may be passed in to customize the locations
        of the GeoIP datasets.

        * path: Base directory to where GeoIP data is located or the full path
            to where the city or country data files (*.mmdb) are located.
            Assumes that both the city and country data sets are located in
            this directory; overrides the GEOIP_PATH setting.

        * cache: The cache settings when opening up the GeoIP datasets. May be
            an integer in (0, 1, 2, 4, 8) corresponding to the MODE_AUTO,
            MODE_MMAP_EXT, MODE_MMAP, MODE_FILE, and MODE_MEMORY,
            `GeoIPOptions` C API settings,  respectively. Defaults to 0,
            meaning MODE_AUTO.

        * country: The name of the GeoIP country data file. Defaults to
            'GeoLite2-Country.mmdb'; overrides the GEOIP_COUNTRY setting.

        * city: The name of the GeoIP city data file. Defaults to
            'GeoLite2-City.mmdb'; overrides the GEOIP_CITY setting.
        z Invalid GeoIP caching option: %sÚ
GEOIP_PATHNÚ
GEOIP_CITYzGeoLite2-City.mmdbÚGEOIP_COUNTRYzGeoLite2-Country.mmdbzDGeoIP path must be provided via parameter or the GEOIP_PATH setting.)Úmodez@Path must be a valid database or directory containing databases.z#Unable to handle database edition: )Úcache_optionsr
   Úgetattrr   r   Úis_fileÚ_pathÚgeoip2ÚdatabaseÚReaderÚ_readerÚ	_metadataÚdatabase_typeÚSUPPORTED_DATABASE_TYPES)ÚselfÚpathÚcacheÚcountryÚcityr'   r   r   r   Ú__init__H   s.   
ÿýÿÿzGeoIP2.__init__c                 C   s   | j r
| j  ¡  d S d S ©N)r%   Úclose©r)   r   r   r   Ú__del__|   s   ÿzGeoIP2.__del__c                 C   s8   | j }d|j› d|j› �}d| jj› d|› d| j› d�S )NÚvÚ.ú<z [z	] _path='z'>)r&   Úbinary_format_major_versionÚbinary_format_minor_versionÚ	__class__r   r!   )r)   ÚmÚversionr   r   r   Ú__repr__�   s   zGeoIP2.__repr__c                 C   s
   | j  ¡ S r/   )r%   Úmetadatar1   r   r   r   r&   †   s   
zGeoIP2._metadatac                 C   ó   d| j jv S )NÚCity©r&   r'   r1   r   r   r   Úis_cityŠ   ó   zGeoIP2.is_cityc                 C   r=   )NÚCountryr?   r1   r   r   r   Ú
is_countryŽ   rA   zGeoIP2.is_countryF©Úrequire_cityc                C   s’   t |ttjtjfƒstdt|ƒj ƒ‚|r | js t	d| j
› �ƒ‚t |tƒr:zt|ƒ W n ty9   t |¡}Y nw | jrA| jjn| jj}||ƒS )NzSGeoIP query must be a string or instance of IPv4Address or IPv6Address, not type %szInvalid GeoIP city data file: )Ú
isinstanceÚstrÚ	ipaddressÚIPv4AddressÚIPv6AddressÚ	TypeErrorÚtyper   r@   r
   r!   r   r   ÚsocketÚgethostbynamer%   r-   r,   )r)   ÚqueryrE   Úfunctionr   r   r   Ú_query’   s    ÿÿ

þzGeoIP2._queryc                 C   sÚ   | j |dd�}|jr|jd nd}i d|jj“d|jj“d|jj“d|jj“d	|jj	“d
|jj“d|jj
“d|jj“d|jj“d|jj“d|jj“d|rO|j	nd“d|rW|jnd“d|jj“d|jj“d|rj|j	“S d“S )zÅ
        Return a dictionary of city information for the given IP address or
        Fully Qualified Domain Name (FQDN). Some information in the dictionary
        may be undefined (None).
        TrD   r   NÚaccuracy_radiusr-   Úcontinent_codeÚcontinent_nameÚcountry_codeÚcountry_nameÚis_in_european_unionÚlatitudeÚ	longitudeÚ
metro_codeÚpostal_codeÚregion_codeÚregion_nameÚ	time_zoneÚdma_codeÚregion)rQ   ÚsubdivisionsÚlocationrR   r-   ÚnameÚ	continentÚcoder,   Úiso_coderW   rX   rY   rZ   Úpostalr^   )r)   rO   Úresponser`   r   r   r   r-   ¦   sJ   ÿþýüûúùø	÷
öõôóòð
ïïzGeoIP2.cityc                 C   ó   |   |¡d S )z9Return the country code for the given IP Address or FQDN.rU   ©r,   ©r)   rO   r   r   r   rU   Â   ó   zGeoIP2.country_codec                 C   ri   )z9Return the country name for the given IP Address or FQDN.rV   rj   rk   r   r   r   rV   Æ   rl   zGeoIP2.country_namec                 C   s2   | j |dd�}|jj|jj|jj|jj|jjdœS )zä
        Return a dictionary with the country code and name when given an
        IP address or a Fully Qualified Domain Name (FQDN). For example, both
        '24.124.1.80' and 'djangoproject.com' are valid parameters.
        FrD   )rS   rT   rU   rV   rW   )rQ   rd   re   rc   r,   rf   rW   )r)   rO   rh   r   r   r   r,   Ê   s   ûzGeoIP2.country©rY   rX   c                    s0   t jdtdd� |  |¡‰ t‡ fdd„|D ƒƒS )Nz<GeoIP2.coords() is deprecated. Use GeoIP2.lon_lat() instead.r   ©Ú
stacklevelc                 3   s   � | ]}ˆ | V  qd S r/   r   )Ú.0Úo©Údatar   r   Ú	<genexpr>à   s   € z GeoIP2.coords.<locals>.<genexpr>)ÚwarningsÚwarnr   r-   Útuple)r)   rO   Úorderingr   rr   r   ÚcoordsÙ   s   ý
zGeoIP2.coordsc                 C   ó   |   |¡}|d |d fS )z@Return a tuple of the (longitude, latitude) for the given query.rY   rX   ©r-   ©r)   rO   rs   r   r   r   Úlon_latâ   ó   
zGeoIP2.lon_latc                 C   rz   )z@Return a tuple of the (latitude, longitude) for the given query.rX   rY   r{   r|   r   r   r   Úlat_lonç   r~   zGeoIP2.lat_lonc                 C   s   ddl m} ||  |¡dd�S )z/Return a GEOS Point object for the given query.r   )ÚPointiæ  )Úsrid)Údjango.contrib.gis.geosr€   r}   )r)   rO   r€   r   r   r   Úgeosì   s   zGeoIP2.geosc                 C   s   t jdtdd� t||ƒS )Nz2GeoIP2.open() is deprecated. Use GeoIP2() instead.r   rn   )ru   rv   r   r	   )ÚclsÚ	full_pathr+   r   r   r   Úopenó   s   ý
zGeoIP2.open)Nr   NN)rm   )r   r   r   Ú	MODE_AUTOÚMODE_MMAP_EXTÚ	MODE_MMAPÚ	MODE_FILEÚMODE_MEMORYÚ	frozensetr   r!   r%   r.   r2   r;   r   r&   r@   rC   rQ   r-   rU   rV   r,   ry   r}   r   rƒ   Úclassmethodr†   r   r   r   r   r	   5   s>    ÿ
4



	)Ú__doc__rH   rM   ru   Údjango.confr   Údjango.core.exceptionsr   Údjango.core.validatorsr   Údjango.utils._osr   Údjango.utils.deprecationr   Údjango.utils.functionalr   Ú__all__Úgeoip2.databaser"   ÚImportErrorr   r(   Ú	Exceptionr
   r	   r   r   r   r   Ú<module>   s*    ÿ
