o
    ˜¨Êh¡"  ã                   @   s¢   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mZ eed	dƒeed
dƒeeddƒdœZG dd„ deƒZG dd„ dƒZdS )é    N)Úsettings)ÚPoint)ÚValidationError)Úvalidate_ipv46_address)Úto_pathé   )ÚCityÚCountryÚ
GEOIP_PATHÚ
GEOIP_CITYzGeoLite2-City.mmdbÚGEOIP_COUNTRYzGeoLite2-Country.mmdb)r
   r   r   c                   @   s   e Zd ZdS )ÚGeoIP2ExceptionN)Ú__name__Ú
__module__Ú__qualname__© r   r   úP/var/www/html/env/lib/python3.10/site-packages/django/contrib/gis/geoip2/base.pyr      s    r   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ZdZd*dd	„Zed
d„ ƒZedd„ ƒZdd„ Zdd„ Z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ed(d)„ ƒZdS )-ÚGeoIP2r   r   é   é   é   Ú Nc           	      C   s*  || j vrtd| ƒ‚|ptd }|stdƒ‚t|ƒ}| ¡ r^||p%td  }| ¡ r9tjjt	|ƒ|d�| _
|| _||p?td  }| ¡ rStjjt	|ƒ|d�| _|| _| js\td| ƒ‚dS | ¡ r‘tjjt	|ƒ|d�}| ¡ j}| d¡r~|| _|| _dS | d	¡r‹|| _
|| _dS td
| ƒ‚tdƒ‚)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: %sr
   zDGeoIP path must be provided via parameter or the GEOIP_PATH setting.r   )Úmoder   z"Could not load a database from %s.r   r	   z(Unable to recognize database edition: %sz-GeoIP path must be a valid file or directory.N)Úcache_optionsr   ÚGEOIP_SETTINGSr   Úis_dirÚis_fileÚgeoip2ÚdatabaseÚReaderÚstrÚ_countryÚ_country_fileÚ_cityÚ
_city_fileÚ_readerÚmetadataÚdatabase_typeÚendswith)	ÚselfÚpathÚcacheÚcountryÚcityÚ
country_dbÚcity_dbÚreaderÚdb_typer   r   r   Ú__init__1   sB   
ÿÿ




ÿzGeoIP2.__init__c                 C   s   | j p| jS ©N)r!   r#   ©r)   r   r   r   r%   y   ó   zGeoIP2._readerc                 C   s   | j r| j jS | jjS r3   )r!   r,   r#   r-   r4   r   r   r   Ú_country_or_city}   s   zGeoIP2._country_or_cityc                 C   s   | j r
| j  ¡  d S d S r3   )r%   Úcloser4   r   r   r   Ú__del__„   s   ÿzGeoIP2.__del__c                 C   s4   | j  ¡ }d|j|jf }d| jj|| j| jdœ S )Nz[v%s.%s]zH<%(cls)s %(version)s _country_file="%(country)s", _city_file="%(city)s">)ÚclsÚversionr,   r-   )r%   r&   Úbinary_format_major_versionÚbinary_format_minor_versionÚ	__class__r   r"   r$   )r)   Úmetar:   r   r   r   Ú__repr__‰   s   
þüÿÿzGeoIP2.__repr__Fc                 C   sz   t |tƒstdt|ƒj ƒ‚|r| js| jstdƒ‚|r&| js&td| j ƒ‚zt	|ƒ W |S  t
y<   t |¡}Y |S w )z*Check the query and database availability.z)GeoIP query must be a string, not type %sz*Invalid GeoIP country and city data files.z Invalid GeoIP city data file: %s)Ú
isinstancer    Ú	TypeErrorÚtyper   r!   r#   r   r$   r   r   ÚsocketÚgethostbyname)r)   Úqueryr-   Úcity_or_countryr   r   r   Ú_check_query™   s   
ÿ

ýýzGeoIP2._check_queryc                 C   s   | j |dd�}t| j |¡ƒ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).
        T)r-   )rG   r   r#   r-   ©r)   rE   Ú	enc_queryr   r   r   r-   ¯   s   zGeoIP2.cityc                 C   ó   |   |¡d S )z9Return the country code for the given IP Address or FQDN.Úcountry_code©r,   ©r)   rE   r   r   r   rK   ¸   ó   zGeoIP2.country_codec                 C   rJ   )z9Return the country name for the given IP Address or FQDN.Úcountry_namerL   rM   r   r   r   rO   ¼   rN   zGeoIP2.country_namec                 C   s   | j |dd�}t|  |¡ƒ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.
        T)rF   )rG   r	   r6   rH   r   r   r   r,   À   s   zGeoIP2.country©Ú	longitudeÚlatitudec                    s,   |   |¡‰ ˆ d u rd S t‡ fdd„|D ƒƒS )Nc                 3   s   � | ]}ˆ | V  qd S r3   r   )Ú.0Úo©Úcdictr   r   Ú	<genexpr>Ð   s   € z GeoIP2.coords.<locals>.<genexpr>)r-   Útuple)r)   rE   Úorderingr   rU   r   ÚcoordsË   s   
zGeoIP2.coordsc                 C   s
   |   |¡S )z@Return a tuple of the (longitude, latitude) for the given query.©rZ   rM   r   r   r   Úlon_latÒ   ó   
zGeoIP2.lon_latc                 C   s   |   |d¡S )z@Return a tuple of the (latitude, longitude) for the given query.)rR   rQ   r[   rM   r   r   r   Úlat_lonÖ   r5   zGeoIP2.lat_lonc                 C   s   |   |¡}|rt|dd�S dS )z/Return a GEOS Point object for the given query.iæ  )ÚsridN)r\   r   )r)   rE   Úllr   r   r   ÚgeosÚ   s   
zGeoIP2.geosc                 C   s   | j  ¡ }d|j|jf S )z@Return information about the GeoIP library and databases in use.zGeoIP Library:
	%s.%s
)r%   r&   r;   r<   )r)   r>   r   r   r   Úinfoã   s
   
þzGeoIP2.infoc                 C   s
   t ||ƒS r3   )r   )r9   Ú	full_pathr+   r   r   r   Úopenì   r]   zGeoIP2.open)Nr   NN)FF)rP   ) r   r   r   Ú	MODE_AUTOÚMODE_MMAP_EXTÚ	MODE_MMAPÚ	MODE_FILEÚMODE_MEMORYÚ	frozensetr   r$   r"   r#   r!   r2   Úpropertyr%   r6   r8   r?   rG   r-   rK   rO   r,   rZ   r\   r^   ra   rb   Úclassmethodrd   r   r   r   r   r      sB    ÿ
H


	
	
r   )rC   Úgeoip2.databaser   Údjango.confr   Údjango.contrib.gis.geosr   Údjango.core.exceptionsr   Údjango.core.validatorsr   Údjango.utils._osr   Ú	resourcesr   r	   Úgetattrr   Ú	Exceptionr   r   r   r   r   r   Ú<module>   s    


ý