3
(hS.                 @   s   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m	Z	m
Z
mZmZmZmZmZmZ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jd	Zejd
ZG dd deZG dd de Z!dS )    N)c_char_p)GEOIP_SETTINGS)
GeoIP_country_code_by_addrGeoIP_country_code_by_nameGeoIP_country_name_by_addrGeoIP_country_name_by_nameGeoIP_database_infoGeoIP_deleteGeoIP_lib_version
GeoIP_openGeoIP_record_by_addrGeoIP_record_by_name)ipv4_re)six)RemovedInDjango20Warning)force_bytes
force_textz^GEO-\d{3}FREEz^GEO-\d{3}LITEc               @   s   e Zd ZdS )GeoIPExceptionN)__name__
__module____qualname__ r   r   A/tmp/pip-install-q3hcpn_q/Django/django/contrib/gis/geoip/base.pyr      s   r   c               @   s   e Zd ZdZdZdZdZdZdd d/D ZdZ	dZ
d	Zd	Zd0d
dZdd Zdd Zd1ddZdd Zdd Zdd Zdd Zd3ddZdd  Zd!d" Zd#d$ Zed%d& Zed'd( Zed)d* Zed+d, Zd-d. ZeZ eZ!eZ"eZ#eZ$eZ%eZ&eZ'd	S )4GeoIPr               c             C   s   i | ]
}d |qS )Nr   ).0optr   r   r   
<dictcomp>4   s    zGeoIP.<dictcomp> Nc       	      C   s\  t jdtd || jkr || _ntd| |sFtjd}|sFtdt|t	j
sdtdt|j tjj|rtjj||ptjdd}tjj|rtt||| _|| _tjj||ptjd	d
}tjj|rtt||| _|| _nntjj|rPtt||}t|}tj|r(|| _|| _n&tj|rB|| _|| _ntd| ntddS )a  
        Initializes the GeoIP object, no parameters are required to use default
        settings.  Keyword arguments may be passed in to customize the locations
        of the GeoIP data sets.

        * path: Base directory to where GeoIP data is located or the full path
            to where the city or country data files (*.dat) are located.
            Assumes that both the city and country data sets are located in
            this directory; overrides the GEOIP_PATH settings attribute.

        * cache: The cache settings when opening up the GeoIP datasets,
            and may be an integer in (0, 1, 2, 4, 8) corresponding to
            the GEOIP_STANDARD, GEOIP_MEMORY_CACHE, GEOIP_CHECK_CACHE,
            GEOIP_INDEX_CACHE, and GEOIP_MMAP_CACHE, `GeoIPOptions` C API
            settings,  respectively.  Defaults to 0, meaning that the data is read
            from the disk.

        * country: The name of the GeoIP country data file.  Defaults to
            'GeoIP.dat'; overrides the GEOIP_COUNTRY settings attribute.

        * city: The name of the GeoIP city data file.  Defaults to
            'GeoLiteCity.dat'; overrides the GEOIP_CITY settings attribute.
        zvdjango.contrib.gis.geoip is deprecated in favor of django.contrib.gis.geoip2 and the MaxMind GeoLite2 database format.r   z Invalid GeoIP caching option: %sZ
GEOIP_PATHzDGeoIP path must be provided via parameter or the GEOIP_PATH setting.zInvalid path type: %sZGEOIP_COUNTRYz	GeoIP.datZ
GEOIP_CITYzGeoLiteCity.datz(Unable to recognize database edition: %sz-GeoIP path must be a valid file or directory.N)warningswarnr   cache_options_cacher   r   get
isinstancer   string_types	TypeErrortyper   ospathisdirjoinisfiler   r   _country_country_file_city
_city_filer   
lite_regexmatch
free_regex)	selfr,   cachecountrycityZ
country_dbZcity_dbptrinfor   r   r   __init__>   s@    

zGeoIP.__init__c             C   s0   t d krd S | jrt | j | jr,t | j d S )N)r	   r0   r2   )r7   r   r   r   __del__   s    
zGeoIP.__del__c             C   s8   d}t d k	r|dtt   7 }d| jj|| j| jd S )Nr!   z [v%s]zG<%(cls)s%(version)s _country_file="%(country)s", _city_file="%(city)s">)clsversionr9   r:   )r
   r   	__class__r   r1   r3   )r7   r@   r   r   r   __repr__   s    zGeoIP.__repr__Fc             C   sx   t |tjstdt|j |r:| jp,| j r:tdn6|rV| j rVtd| j	 n|rp| j rptd| j
 t|S )z@Helper routine for checking 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)r'   r   r(   r)   r*   r   r0   r2   r   r1   r3   r   )r7   queryr9   r:   city_or_countryr   r   r   _check_query   s    
zGeoIP._check_queryc             C   s<   | j |dd}tj|r(t| jt|S t| jt|S dS )z
        Returns 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)r:   N)rE   r   r5   r   r2   r   r   )r7   rC   	enc_queryr   r   r   r:      s    
z
GeoIP.cityc             C   sJ   | j |dd}| jr8tj|r*t| j|S t| j|S n| j|d S dS )z:Returns the country code for the given IP Address or FQDN.T)rD   country_codeN)rE   r0   r   r5   r   r   r:   )r7   rC   rF   r   r   r   rG      s    
zGeoIP.country_codec             C   sJ   | j |dd}| jr8tj|r*t| j|S t| j|S n| j|d S dS )z:Returns the country name for the given IP Address or FQDN.T)rD   country_nameN)rE   r0   r   r5   r   r   r:   )r7   rC   rF   r   r   r   rH      s    
zGeoIP.country_namec             C   s   | j || j|dS )z
        Returns 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.
        )rG   rH   )rG   rH   )r7   rC   r   r   r   r9      s    zGeoIP.country	longitudelatitudec                s0   | j |  d krd S t fdd|D S d S )Nc             3   s   | ]} | V  qd S )Nr   )r   o)cdictr   r   	<genexpr>   s    zGeoIP.coords.<locals>.<genexpr>)r:   tuple)r7   rC   Zorderingr   )rL   r   coords   s    
zGeoIP.coordsc             C   s
   | j |S )zAReturns a tuple of the (longitude, latitude) for the given query.)rO   )r7   rC   r   r   r   lon_lat   s    zGeoIP.lon_latc             C   s   | j |dS )zAReturns a tuple of the (latitude, longitude) for the given query.rJ   rI   )rJ   rI   )rO   )r7   rC   r   r   r   lat_lon   s    zGeoIP.lat_lonc             C   s.   | j |}|r&ddlm} ||ddS dS dS )z0Returns a GEOS Point object for the given query.r   )Pointi  )ZsridN)rP   Zdjango.contrib.gis.geosrR   )r7   rC   ZllrR   r   r   r   geos   s
    
z
GeoIP.geosc             C   s$   | j dkrd| j }n
t| j }|S )z5Returns information about the GeoIP country database.NzNo GeoIP Country data in "%s")r0   r1   r   )r7   cir   r   r   country_info   s    

zGeoIP.country_infoc             C   s$   | j dkrd| j }n
t| j }|S )z2Returns information about the GeoIP city database.NzNo GeoIP City data in "%s")r2   r3   r   )r7   rT   r   r   r   	city_info  s    

zGeoIP.city_infoc             C   s*   d}t r|dt   7 }|d| j| jf  S )zAReturns information about the GeoIP library and databases in use.r!   zGeoIP Library:
	%s
zCountry:
	%s
City:
	%s)r
   rU   rV   )r7   r<   r   r   r   r<     s    z
GeoIP.infoc             C   s
   t ||S )N)r   )r?   	full_pathr8   r   r   r   open  s    z
GeoIP.openc             C   s   | j r| j|S | j|S d S )N)r2   r:   r9   )r7   argr   r   r   _rec_by_arg  s    
zGeoIP._rec_by_arg)r   r   r   r   r   )Nr   NN)FFFrI   rJ   )r[   )(r   r   r   ZGEOIP_STANDARDZGEOIP_MEMORY_CACHEZGEOIP_CHECK_CACHEZGEOIP_INDEX_CACHEZGEOIP_MMAP_CACHEr$   r3   r1   r2   r0   r=   r>   rB   rE   r:   rG   rH   r9   rO   rP   rQ   rS   propertyrU   rV   r<   classmethodrX   rZ   Zregion_by_addrZregion_by_nameZrecord_by_addrZrecord_by_nameZcountry_code_by_addrZcountry_code_by_nameZcountry_name_by_addrZcountry_name_by_namer   r   r   r   r      sF   
M	


			r   )"r+   rer"   ctypesr   Z!django.contrib.gis.geoip.libgeoipr   Z#django.contrib.gis.geoip.prototypesr   r   r   r   r   r	   r
   r   r   r   Zdjango.core.validatorsr   Zdjango.utilsr   Zdjango.utils.deprecationr   Zdjango.utils.encodingr   r   compiler6   r4   	Exceptionr   objectr   r   r   r   r   <module>   s   0

