
 X^#                 @   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	 d  d l
 m Z d d l m Z m Z i e e d d  d 6e e d	 d
  d	 6e e d d  d 6Z Gd d   d e  Z Gd d   d e  Z d S)    N)settings)ipv4_re)six)is_valid_ipv6_address   )CityCountry
GEOIP_PATH
GEOIP_CITYzGeoLite2-City.mmdbGEOIP_COUNTRYzGeoLite2-Country.mmdbc               @   s   e  Z d  Z d S)GeoIP2ExceptionN)__name__
__module____qualname__ r   r   L/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/geoip2/base.pyr      s   r   c               @   sI  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 d	 d d	 d	 d
 d  Z e d d    Z e d d    Z d d   Z d d   Z d d d d d  Z d d   Z d d   Z d d   Z d d   Z d. d! d"  Z d# d$   Z d% d&   Z d' d(   Z e d) d*    Z e d+ d,    Z d	 S)/GeoIP2r   r            c             C   s   i  |  ] } d  |  q S)Nr   ).0optr   r   r   
<dictcomp>%   s   	 zGeoIP2.<dictcomp> Nc       	      C   s  | |  j  k r | |  _ n t d |   | sS t d } | sS t d   qS n  t | t j  s t d t |  j	   n  t
 j j |  rFt
 j j | | p t d  } t
 j j |  r t j j | d | |  _ | |  _ n  t
 j j | | pt d  } t
 j j |  rt j j | d | |  _ | |  _ qn t
 j j |  rt j j | d | } | j   j } | j d  r| |  _ | |  _ q| j d	  r| |  _ | |  _ qt d
 |   n 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: %sr	   zDGeoIP path must be provided via parameter or the GEOIP_PATH setting.zInvalid path type: %sr   moder
   r   r   z(Unable to recognize database edition: %sz-GeoIP path must be a valid file or directory.N)cache_options_cacher   GEOIP_SETTINGS
isinstancer   string_types	TypeErrortyper   ospathisdirjoinisfilegeoip2ZdatabaseReader_country_country_file_city
_city_filemetadataZdatabase_typeendswith)	selfr#   cachecountrycityZ
country_dbZcity_dbreaderZdb_typer   r   r   __init__/   s:    
		zGeoIP2.__init__c             C   s   |  j  r |  j  S|  j Sd  S)N)r)   r+   )r/   r   r   r   _readeru   s    	zGeoIP2._readerc             C   s!   |  j  r |  j  j S|  j j Sd  S)N)r)   r1   r+   r2   )r/   r   r   r   _country_or_city|   s    	
zGeoIP2._country_or_cityc             C   s   |  j  r |  j  j   n  d  S)N)r5   close)r/   r   r   r   __del__   s    	zGeoIP2.__del__c             C   sU   |  j  j   } d | j | j f } d i |  j j d 6| d 6|  j d 6|  j d 6S)Nz[v%s.%s]zH<%(cls)s %(version)s _country_file="%(country)s", _city_file="%(city)s">clsversionr1   r2   )r5   r-   binary_format_major_versionbinary_format_minor_version	__class__r   r*   r,   )r/   metar:   r   r   r   __repr__   s    
zGeoIP2.__repr__Fc             C   s   t  | t j  s. t d t |  j   n  | rV |  j pC |  j rV t d   nL | r| |  j r| t d |  j	   n& | r |  j r t d |  j
   n  t j |  p t |  s t j |  } n  | 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   r)   r+   r   r*   r,   r   matchr   socketgethostbyname)r/   queryr1   r2   city_or_countryr   r   r   _check_query   s    zGeoIP2._check_queryc             C   s+   |  j  | d 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).
        r2   T)rE   r   r+   r2   )r/   rC   	enc_queryr   r   r   r2      s    zGeoIP2.cityc             C   s&   |  j  | d d } |  j |  d S)z9Return the country code for the given IP Address or FQDN.rD   Tcountry_code)rE   r1   )r/   rC   rF   r   r   r   rG      s    zGeoIP2.country_codec             C   s&   |  j  | d d } |  j |  d S)z9Return the country name for the given IP Address or FQDN.rD   Tcountry_name)rE   r1   )r/   rC   rF   r   r   r   rH      s    zGeoIP2.country_namec             C   s(   |  j  | d 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.
        rD   T)rE   r   r6   )r/   rC   rF   r   r   r   r1      s    zGeoIP2.country	longitudelatitudec                s@   |  j  |      d  k r d  St   f d d   | D  Sd  S)Nc             3   s   |  ] }   | Vq d  S)Nr   )r   o)cdictr   r   	<genexpr>   s    z GeoIP2.coords.<locals>.<genexpr>)r2   tuple)r/   rC   Zorderingr   )rL   r   coords   s    zGeoIP2.coordsc             C   s   |  j  |  S)z@Return a tuple of the (longitude, latitude) for the given query.)rO   )r/   rC   r   r   r   lon_lat   s    zGeoIP2.lon_latc             C   s   |  j  | d  S)z@Return a tuple of the (latitude, longitude) for the given query.rJ   rI   )rJ   rI   )rO   )r/   rC   r   r   r   lat_lon   s    zGeoIP2.lat_lonc             C   s=   |  j  |  } | r5 d d l m } | | d d Sd Sd S)z/Return a GEOS Point object for the given query.r   )PointZsridi  N)rP   Zdjango.contrib.gis.geosrR   )r/   rC   ZllrR   r   r   r   geos   s
    zGeoIP2.geosc             C   s#   |  j  j   } d | j | j f S)z@Return information about the GeoIP library and databases in use.zGeoIP Library:
	%s.%s
)r5   r-   r;   r<   )r/   r>   r   r   r   info   s    zGeoIP2.infoc             C   s   t  | |  S)N)r   )r9   	full_pathr0   r   r   r   open   s    zGeoIP2.open)r   r   r   r   r   )z	longitudezlatitude)r   r   r   Z	MODE_AUTOZMODE_MMAP_EXTZ	MODE_MMAPZ	MODE_FILEZMODE_MEMORYr   r,   r*   r+   r)   r4   propertyr5   r6   r8   r?   rE   r2   rG   rH   r1   rO   rP   rQ   rS   rT   classmethodrV   r   r   r   r   r      s4   F
	
r   )r"   rA   Zgeoip2.databaser'   Zdjango.confr   Zdjango.core.validatorsr   Zdjango.utilsr   Zdjango.utils.ipv6r   	resourcesr   r   getattrr   	Exceptionr   objectr   r   r   r   r   <module>   s   