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>   zGeoIP2-CityzGeoLite2-CountryzGeoIP2-CountryzGeoLite2-CityzDBIP-Country-LitezDBIP-City-Litec                   @   s   e Zd ZdS )r
   N)__name__
__module____qualname__ r   r   /home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/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: %sZ
GEOIP_PATHNZ
GEOIP_CITYzGeoLite2-City.mmdbZ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geoip2Z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   Zbinary_format_major_versionZbinary_format_minor_version	__class__r   r   )r   mversionr   r   r   __repr__   s   zGeoIP2.__repr__c                 C   s
   | j  S r%   )r   metadatar'   r   r   r   r      s   
zGeoIP2._metadatac                 C      d| j jv S )NZCityr   r   r'   r   r   r   is_city      zGeoIP2.is_cityc                 C   r1   )NZCountryr2   r'   r   r   r   
is_country   r4   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   r3   r
   r   r   r   socketgethostbynamer   r#   r"   )r   queryr7   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).
        Tr6   r   Naccuracy_radiusr#   continent_codecontinent_namecountry_codecountry_nameis_in_european_unionlatitude	longitude
metro_codeZpostal_codeZregion_codeZregion_name	time_zoneZdma_coderegion)rC   ZsubdivisionslocationrD   r#   name	continentcoder"   iso_coderI   rJ   rK   rL   ZpostalrM   )r   rA   responserN   r   r   r   r#      sJ   	

zGeoIP2.cityc                 C      |  |d S )z9Return the country code for the given IP Address or FQDN.rG   r"   r   rA   r   r   r   rG         zGeoIP2.country_codec                 C   rU   )z9Return the country name for the given IP Address or FQDN.rH   rV   rW   r   r   r   rH      rX   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.
        Fr6   )rE   rF   rG   rH   rI   )rC   rQ   rR   rP   r"   rS   rI   )r   rA   rT   r   r   r   r"      s   zGeoIP2.countryrK   rJ   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   rA   Zorderingr   r^   r   coords   s   
zGeoIP2.coordsc                 C      |  |}|d |d fS )z@Return a tuple of the (longitude, latitude) for the given query.rK   rJ   r#   r   rA   r_   r   r   r   lon_lat      
zGeoIP2.lon_latc                 C   re   )z@Return a tuple of the (latitude, longitude) for the given query.rJ   rK   rf   rg   r   r   r   lat_lon   ri   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  )Zsrid)Zdjango.contrib.gis.geosrk   rh   )r   rA   rk   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   rZ   )ra   rb   r   r	   )cls	full_pathr!   r   r   r   open   s   
zGeoIP2.open)Nr   NN)rY   )r   r   r   Z	MODE_AUTOZMODE_MMAP_EXTZ	MODE_MMAPZ	MODE_FILEZMODE_MEMORY	frozensetr   r   r   r$   r(   r/   r   r   r3   r5   rC   r#   rG   rH   r"   rd   rh   rj   rl   classmethodro   r   r   r   r   r	   5   s>    
4



	)__doc__r:   r?   ra   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.validatorsr   Zdjango.utils._osr   Zdjango.utils.deprecationr   Zdjango.utils.functionalr   __all__Zgeoip2.databaser   ImportErrorr   r   	Exceptionr
   r	   r   r   r   r   <module>   s*    
