
    h#                         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__     a/var/www/html/ranktracker/api/venv/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                    || j                   v r|| _        n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                         rt        j                  j                  t        |      |      }|j!                         j"                  }|j%                  d      r|| _        || _        y|j%                  d	      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_options_cacher   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_types	            r   __init__zGeoIP2.__init__.   s   0 D&&&DK!"Du"LMM 3~l3!"hii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'#
"&!!), &%)"%&PSZ&Z[[!"QRRr   c                 6    | j                   xs | j                  S N)r&   r(   r.   s    r   r*   zGeoIP2._readert   s    }}*

*r   c                 r    | j                   r| j                   j                  S | j                  j                  S r9   )r&   r1   r(   r2   r:   s    r   _country_or_cityzGeoIP2._country_or_cityx   s'    ====(((::??"r   c                 R    | j                   r| j                   j                          y y r9   )r*   closer:   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versionr1   r2   )r*   r+   binary_format_major_versionbinary_format_minor_version	__class__r   r'   r)   )r.   metarD   s      r   __repr__zGeoIP2.__repr__   s]    ||$$& $ @ @$BbBbcY>>**))OO	]
 
 	
r   c                    t        |t              s!t        dt        |      j                  z        |r#| j
                  s| j                  st        d      |r$| j
                  st        d| j                  z        |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 country data file: %sz Invalid GeoIP city data file: %s)
isinstancer%   	TypeErrortyper   r&   r(   r   r'   r)   r   r   socketgethostbyname)r.   queryr1   r2   city_or_countrys        r   _check_queryzGeoIP2._check_query   s     %%G$u+J^J^^__ DMMTZZ!"NOOT]]!"G$J\J\"\]]$**!"Dt"VWW	0"5)   	0((/E	0s   $B1 1CC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)r2   )rR   r   r(   r2   r.   rP   	enc_querys      r   r2   zGeoIP2.city   s1     %%e$%7	DJJOOI.//r   c                 P    | j                  |d      }| j                  |      d   S )z9Return the country code for the given IP Address or FQDN.TrQ   country_coderR   r1   rT   s      r   rX   zGeoIP2.country_code   +    %%eT%B	||I&~66r   c                 P    | j                  |d      }| j                  |      d   S )z9Return the country name for the given IP Address or FQDN.TrW   country_namerY   rT   s      r   r\   zGeoIP2.country_name   rZ   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.
        TrW   )rR   r	   r<   rT   s      r   r1   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r9   r   ).0ocdicts     r   	<genexpr>z GeoIP2.coords.<locals>.<genexpr>   s     4aq4s   )r2   tuple)r.   rP   orderingrb   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.rf   r.   rP   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	longituderh   ri   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)rj   django.contrib.gis.geosrp   )r.   rP   llrp   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:
	rA   
)r*   r+   rE   rF   )r.   rH   s     r   infozGeoIP2.info   s.     ||$$&.2.N.NPTPpPpqqr   c                     t        ||      S r9   )r   )rC   	full_pathr0   s      r   openzGeoIP2.open   s    i''r   )Nr   NN)FFF))rm   rl   ) r   r   r   	MODE_AUTOMODE_MMAP_EXT	MODE_MMAP	MODE_FILEMODE_MEMORY	frozensetr   r)   r'   r(   r&   r7   propertyr*   r<   r?   rI   rR   r2   rX   r\   r1   rf   rj   rn   rt   rw   classmethodrz   r   r   r   r   r      s     IMIIKy-I{[\M JM EHDSL + + # #!

,07
7
95"= r r
 ( (r   r   )rN   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 	K( K(r   