
    `f"                         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mZ  eedd           eed	d
           eedd          dZ G d de          Z G d d          ZdS )    N)settings)ValidationError)validate_ipv46_address)to_path   )CityCountry
GEOIP_PATH
GEOIP_CITYzGeoLite2-City.mmdbGEOIP_COUNTRYzGeoLite2-Country.mmdb)r
   r   r   c                       e Zd ZdS )GeoIP2ExceptionN)__name__
__module____qualname__     P/var/www/html/env/lib/python3.11/site-packages/django/contrib/gis/geoip2/base.pyr   r      s        Dr   r   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ZdZddZed	             Zed
             Zd Zd ZddZd Zd Zd Zd ZddZd Zd Zd Zed             Zed             ZdS )GeoIP2r   r             Nc                    || j         vrt          d|z            |pt          d         }|st          d          t          |          }|                                r||pt          d         z  }|                                r:t          j                            t          |          |          | _
        || _        ||pt          d         z  }|                                r:t          j                            t          |          |          | _        || _        | j        st          d|z            dS |                                rt          j                            t          |          |          }|                                j        }d|v r|| _        || _        dS d	|v r|| _
        || _        dS t          d
|z            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)	selfpathcachecountrycity
country_dbcity_dbreaderdb_types	            r   __init__zGeoIP2.__init__0   s   0 ***!"Du"LMMM 3~l3 	!V   t}};;== #	S !KN?,KLJ!!## 0 & 6 6s:U 6 S S%/"dBn\&BCG   *#_33CLLu3MM
")< S%&JT&QRRRS S\\^^ 	S _++CIIE+BBFoo''5G  #
"&g%% &%)"""%>H   ""QRRRr   c                     | j         p| j        S N)r%   r'   r,   s    r   r)   zGeoIP2._readerx   s    }*
*r   c                 @    | j         r| j         j        S | j        j        S r7   )r%   r/   r'   r0   r8   s    r   _country_or_cityzGeoIP2._country_or_city|   s!    = 	#=((:?"r   c                 J    | j         r| j                                          d S d S r7   )r)   closer8   s    r   __del__zGeoIP2.__del__   s0    < 	!L     	! 	!r   c                     | j                                         }d|j        d|j        d}d| j        j        || j        | j        dz  S )Nz[v.]zH<%(cls)s %(version)s _country_file="%(country)s", _city_file="%(city)s">)clsversionr/   r0   )r)   r*   binary_format_major_versionbinary_format_minor_version	__class__r   r&   r(   )r,   metarB   s      r   __repr__zGeoIP2.__repr__   sf    |$$&&,,,,,,

 W~."-	 	
r   Fc                 `   t          |t                    s$t          dt          |          j        z            |r| j        s| j        st          d          |r| j        st          d| j        z            	 t          |           n$# t          $ r t          j        |          }Y nw xY w|S )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,   queryr0   city_or_countrys       r   _check_queryzGeoIP2._check_query   s     %%% 	;d5kk>RR  
  	XDM 	XTZ 	X!"NOOO 	X$* 	X!"Dt"VWWW	0"5)))) 	0 	0 	0(//EEE	0 s   :B
 
B+*B+c                 ~    |                      |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)r0   )rP   r   r'   r0   r,   rN   	enc_querys      r   r0   zGeoIP2.city   s8     %%e$%77	DJOOI..///r   c                 8    |                      |          d         S )z9Return the country code for the given IP Address or FQDN.country_coder/   r,   rN   s     r   rU   zGeoIP2.country_code       ||E"">22r   c                 8    |                      |          d         S )z9Return the country name for the given IP Address or FQDN.country_namerV   rW   s     r   rZ   zGeoIP2.country_name   rX   r   c                 t    |                      |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)rO   )rP   r	   r:   rR   s      r   r/   zGeoIP2.country   s8     %%eT%BB	t,,Y77888r   	longitudelatitudec                 l    |                      |          d S t          fd|D                       S )Nc              3   (   K   | ]}|         V  d S r7   r   ).0ocdicts     r   	<genexpr>z GeoIP2.coords.<locals>.<genexpr>   s'      44aq444444r   )r0   tuple)r,   rN   orderingrc   s      @r   coordszGeoIP2.coords   sA    		%  =444448444444r   c                 ,    |                      |          S )z@Return a tuple of the (longitude, latitude) for the given query.rg   rW   s     r   lon_latzGeoIP2.lon_lat   s    {{5!!!r   c                 .    |                      |d          S )z@Return a tuple of the (latitude, longitude) for the given query.)r^   r]   ri   rW   s     r   lat_lonzGeoIP2.lat_lon   s    {{5";<<<r   c                 Z    |                      |          }|rddlm}  ||d          S dS )z/Return a GEOS Point object for the given query.r   )Pointi  )sridN)rj   django.contrib.gis.geosrn   )r,   rN   llrn   s       r   geoszGeoIP2.geos   sG    \\%   	5555555$''''4r   c                 Z    | j                                         }d|j        d|j        dS )z@Return information about the GeoIP library and databases in use.zGeoIP Library:
	r?   
)r)   r*   rC   rD   )r,   rF   s     r   infozGeoIP2.info   s;     |$$&&,,,,,,
 	
r   c                 "    t          ||          S r7   )r   )rA   	full_pathr.   s      r   openzGeoIP2.open   s    i'''r   )Nr   NN)FF)r\   ) r   r   r   	MODE_AUTOMODE_MMAP_EXT	MODE_MMAP	MODE_FILEMODE_MEMORY	frozensetr   r(   r&   r'   r%   r5   propertyr)   r:   r=   rG   rP   r0   rU   rZ   r/   rg   rj   rl   rr   ru   classmethodrx   r   r   r   r   r      s        IMIIKI	M9iE M
 JM EHFS FS FS FSP + + X+ # # X#! ! !

 
 
    ,0 0 03 3 33 3 39 9 95 5 5 5" " "= = =	 	 	 
 
 X
 ( ( [( ( (r   r   )rL   geoip2.databaser!   django.confr   django.core.exceptionsr   django.core.validatorsr   django.utils._osr   	resourcesr   r	   getattrr   	Exceptionr   r   r   r   r   <module>r      s#                    2 2 2 2 2 2 9 9 9 9 9 9 $ $ $ $ $ $ $ $ $ $ $ $ $ $ '(L$77'(L2FGGWX8OPP 	 	 	 	 	i 	 	 	X( X( X( X( X( X( X( X( X( X(r   