
    Dvg$                         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	 ddlZd
Zeddgz  Zn# e$ r dZY nw xY wh dZ G d de          Z G 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_GEOIP2TGeoIP2GeoIP2ExceptionF>   GeoIP2-CityGeoLite2-CityDBIP-City-LiteGeoIP2-CountryGeoLite2-CountryDBIP-Country-Litec                       e Zd ZdS )r   N)__name__
__module____qualname__     V/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/django/contrib/gis/geoip2.pyr   r   1   s        Dr   c                       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Zd Zd	 Zed
             Zed             Zed             ZdddZd Zd Zd Zd ZddZd Zd Zd Zed             ZdS )r
   r               Nc                    || j         vrt          d|z            |pt          t          dd          }|pt          t          dd          }|pt          t          dd          }|st          d          t	          |          }|||z  ||z  fD ]E}|                                r/|| _        t          j        	                    ||	          | _
         nFt          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,   s         r   __init__zGeoIP2.__init__H   s?   . ***!"Du"LMMM<wxt<<Lwx7KLLXWX@WXX 	!V   t}} 4$;w7 	 	D||~~ !
%55d5GG
 "R   4 888!"W"W"WXXX 98r   c                 J    | j         r| j                                          d S d S N)r*   closer.   s    r   __del__zGeoIP2.__del__|   s0    < 	!L     	! 	!r   c                 j    | 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versions      r   __repr__zGeoIP2.__repr__   sL    NUa3UUa6SUUN4>*NNgNN
NNNNr   c                 4    | j                                         S r5   )r*   metadatar7   s    r   r+   zGeoIP2._metadata   s    |$$&&&r   c                     d| j         j        v S )NCityr+   r,   r7   s    r   is_cityzGeoIP2.is_city   s    555r   c                     d| j         j        v S )NCountryrG   r7   s    r   
is_countryzGeoIP2.is_country   s    DN888r   Frequire_cityc                   t          |t          t          j        t          j        f          s$t          dt          |          j        z            |r| j        st          d| j
                   	 t          |           n$# t          $ r t          j        |          }Y nw xY w| j        r| 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   rH   r   r&   r   r   socketgethostbynamer*   r2   r1   )r.   queryrM   functions       r   _queryzGeoIP2._query   s    %#y'<i>S!TUU 	+-1%[[-AB  
  	Q 	Q!"O4:"O"OPPP	0"5)))) 	0 	0 	0(//EEE	0 )-N4<$$$,:Nxs   2B B#"B#c                    |                      |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|r|j	        ndd|r|j        ndd|j        j        d|j        j        d|r|j	        n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).
        TrL   r   Naccuracy_radiusr2   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)rZ   subdivisionslocationr\   r2   name	continentcoder1   iso_codera   rb   rc   rd   postalrh   )r.   rX   responserj   s       r   r2   zGeoIP2.city   sf    ;;u4;88-5-BL&q))
x0@
HM&
 h05
 h05	

 H,5
 H,1
 #H$4$I
 )2
 *4
 (+6
 8?/
 f>6??$
 &:6;;d
 *4
  )4!
" 9fooT#
 	
r   c                 8    |                      |          d         S )z9Return the country code for the given IP Address or FQDN.r_   r1   r.   rX   s     r   r_   zGeoIP2.country_code       ||E"">22r   c                 8    |                      |          d         S )z9Return the country name for the given IP Address or FQDN.r`   rt   ru   s     r   r`   zGeoIP2.country_name   rv   r   c                     |                      |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.
        FrL   )r]   r^   r_   r`   ra   )rZ   rn   ro   rm   r1   rp   ra   )r.   rX   rr   s      r   r1   zGeoIP2.country   sU     ;;u5;99&05&05$,5$,1$,$4$I
 
 	
r   rc   rb   c                     t          j        dt          d           |                     |          t	          fd|D                       S )Nz<GeoIP2.coords() is deprecated. Use GeoIP2.lon_lat() instead.r   
stacklevelc              3   (   K   | ]}|         V  d S r5   r   ).0odatas     r   	<genexpr>z GeoIP2.coords.<locals>.<genexpr>   s'      //T!W//////r   )warningswarnr   r2   tuple)r.   rX   orderingr   s      @r   coordszGeoIP2.coords   s[    J$	
 	
 	
 	

 yy////h//////r   c                 L    |                      |          }|d         |d         fS )z@Return a tuple of the (longitude, latitude) for the given query.rc   rb   r2   r.   rX   r   s      r   lon_latzGeoIP2.lon_lat   s(    yyK $z"222r   c                 L    |                      |          }|d         |d         fS )z@Return a tuple of the (latitude, longitude) for the given query.rb   rc   r   r   s      r   lat_lonzGeoIP2.lat_lon   s(    yyJk!222r   c                 N    ddl m}  ||                     |          d          S )z/Return a GEOS Point object for the given query.r   )Pointi  )srid)django.contrib.gis.geosr   r   )r.   rX   r   s      r   geoszGeoIP2.geos   s7     	211111uT\\%((t4444r   c                 Z    t          j        dt          d           t          ||          S )Nz2GeoIP2.open() is deprecated. Use GeoIP2() instead.r   r{   )r   r   r   r
   )cls	full_pathr0   s      r   openzGeoIP2.open   s6    @$	
 	
 	
 	

 i'''r   )Nr   NN)ry   )r   r   r   	MODE_AUTOMODE_MMAP_EXT	MODE_MMAP	MODE_FILEMODE_MEMORY	frozensetr#   r&   r*   r3   r8   rB   r   r+   rH   rK   rZ   r2   r_   r`   r1   r   r   r   r   classmethodr   r   r   r   r
   r
   5   s        IMIIKI	M9iE M EG2Y 2Y 2Y 2Yh! ! !
O O O
 ' ' _' 6 6 _6 9 9 _9 -2     &
 
 
83 3 33 3 3
 
 
0 0 0 03 3 3
3 3 3
5 5 5 ( ( [( ( (r   )__doc__rQ   rV   r   django.confr   django.core.exceptionsr   django.core.validatorsr   django.utils._osr   django.utils.deprecationr   django.utils.functionalr   __all__geoip2.databaser'   r	   ImportErrorr-   	Exceptionr   r
   r   r   r   <module>r      sj                      2 2 2 2 2 2 9 9 9 9 9 9 $ $ $ $ $ $ = = = = = = 3 3 3 3 3 3.- J+,,GG	    JJJ   	 	 	 	 	i 	 	 	D( D( D( D( D( D( D( D( D( D(s   A AA