
    Hwg"                         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y)    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y)GeoIP2ExceptionN)__name__
__module____qualname__     U/var/www/horilla/myenv/lib/python3.12/site-packages/django/contrib/gis/geoip2/base.pyr   r      s    r   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y)GeoIP2r   r             Nc                 X   || j                   vrt        d|z        |xs	 t        d   }|st        d      t        |      }|j	                         r||xs	 t        d   z  }|j                         r6t        j                  j                  t        |      |      | _
        || _        ||xs	 t        d   z  }|j                         r6t        j                  j                  t        |      |      | _        || _        | j                  st        d|z        y|j                         rxt        j                  j                  t        |      |      }|j                         j                   }d|v r|| _        || _        yd	|v r|| _
        || _        y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 3~l3!V  t};;= !KN?,KLJ!!# & 6 6s:U 6 S%/"dBn\&BCG #__33CLu3M
")<<%&JT&QRR  \\^ __++CIE+BFoo'55G #
"&g% &%)"%>H  ""QRRr   c                 6    | j                   xs | j                  S N)r%   r'   r,   s    r   r)   zGeoIP2._readerx   s    }}*

*r   c                 r    | 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                 R    | j                   r| j                   j                          y y r7   )r)   closer8   s    r   __del__zGeoIP2.__del__   s    <<LL  r   c                     | j                   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__   sb    ||$$&,,,,

 W~~.."--		
r   c                 `   t        |t              s!t        dt        |      j                  z        |r#| j
                  s| j                  st        d      |r$| j                  st        d| j                  z        	 t        |       |S # t        $ r t        j                  |      }Y |S w xY 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,   queryr0   city_or_countrys       r   _check_queryzGeoIP2._check_query   s     %%;d5k>R>RR 
 DMMTZZ!"NOO$**!"Dt"VWW	0"5)   	0((/E	0s   >B B-,B-c                 p    | j                  |d      }t        | j                  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   s1     %%e$%7	DJJOOI.//r   c                 *    | j                  |      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                 *    | j                  |      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                 \    | j                  |d      }t        | j                  |            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   s/     %%eT%B	t,,Y788r   c                 T    | j                  |      y t        fd|D              S )Nc              3   (   K   | ]	  }|     y wr7   r   ).0ocdicts     r   	<genexpr>z GeoIP2.coords.<locals>.<genexpr>   s     4aq4s   )r0   tuple)r,   rN   orderingr`   s      @r   coordszGeoIP2.coords   s*    		% =48444r   c                 $    | j                  |      S )z@Return a tuple of the (longitude, latitude) for the given query.rd   rW   s     r   lon_latzGeoIP2.lon_lat   s    {{5!!r   c                 &    | j                  |d      S )z@Return a tuple of the (latitude, longitude) for the given query.)latitude	longituderf   rW   s     r   lat_lonzGeoIP2.lat_lon   s    {{5";<<r   c                 J    | j                  |      }|rddlm}  ||d      S y)z/Return a GEOS Point object for the given query.r   )Pointi  )sridN)rg   django.contrib.gis.geosrm   )r,   rN   llrm   s       r   geoszGeoIP2.geos   s&    \\% 5$''r   c                 p    | j                   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   s2     ||$$&,,,,
 	
r   c                     t        ||      S r7   )r   )rA   	full_pathr.   s      r   openzGeoIP2.open   s    i''r   )Nr   NN)FF))rj   ri   ) 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/   rd   rg   rk   rq   rt   classmethodrw   r   r   r   r   r      s     IMIIK	M9iEM
 JM EHFSP + + # #!

 ,03395"=	 
 
 ( (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      sa        2 9 $ $ (L$7(L2FGX8OP	i 	X( X(r   