
vÅÏ^#  ã            	   @   s×   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 d e e d d ƒ d	 e e d	 d
 ƒ d e e d d ƒ i Z Gd d „  d e ƒ Z Gd 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.mmdbc               @   s   e  Z d  Z d S)ÚGeoIP2ExceptionN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú@/tmp/pip-build-8lau8j11/django/django/contrib/gis/geoip2/base.pyr      s   r   c               @   sQ  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 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   é   é   é   Ú Nc       	      C   s¹  | |  j  k r | |  _ n t d | ƒ ‚ | p8 t d } | sM t d ƒ ‚ t | ƒ } | j ƒ  r| | pu t d } | j ƒ  r¯ t j j	 t
 | ƒ d | ƒ|  _ | |  _ | | p¿ t d } | j ƒ  rù t j j	 t
 | ƒ d | ƒ|  _ | |  _ |  j sµt d | ƒ ‚ n  | j ƒ  r©t j j	 t
 | ƒ d | ƒ} | j ƒ  j } | j d ƒ rr| |  _ | |  _ 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.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Úgeoip2ZdatabaseÚReaderÚstrÚ_countryÚ_country_fileÚ_cityÚ
_city_fileÚ_readerÚmetadataZdatabase_typeÚendswith)	ÚselfÚpathÚcacheÚcountryÚcityZ
country_dbZcity_dbÚreaderZdb_typer   r   r   Ú__init__.   s:    !	!				zGeoIP2.__init__c             C   s   |  j  p |  j S)N)r    r"   )r'   r   r   r   r$   t   s    zGeoIP2._readerc             C   s!   |  j  r |  j  j S|  j j Sd  S)N)r    r*   r"   r+   )r'   r   r   r   Ú_country_or_cityx   s    	
zGeoIP2._country_or_cityc             C   s   |  j  r |  j  j ƒ  d  S)N)r$   Úclose)r'   r   r   r   Ú__del__   s    	zGeoIP2.__del__c          	   C   sQ   |  j  j ƒ  } d | j | j f } d d |  j j d | d |  j d |  j i S)Nz[v%s.%s]zH<%(cls)s %(version)s _country_file="%(country)s", _city_file="%(city)s">ÚclsÚversionr*   r+   )r$   r%   Úbinary_format_major_versionÚbinary_format_minor_versionÚ	__class__r   r!   r#   )r'   Úmetar2   r   r   r   Ú__repr__„   s    	zGeoIP2.__repr__Fc             C   sÏ   t  | t ƒ s( t d t | ƒ j ƒ ‚ | rP |  j p= |  j rP t d ƒ ‚ nI | rv |  j rv t d |  j ƒ ‚ n# | r™ |  j r™ t d |  j	 ƒ ‚ y t
 | ƒ Wn! t k
 rÊ t j | ƒ } Yn X| 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 country data file: %sz Invalid GeoIP city data file: %s)Ú
isinstancer   Ú	TypeErrorÚtyper   r    r"   r   r!   r#   r   r   ÚsocketÚgethostbyname)r'   Úqueryr*   r+   Ú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).
        r+   T)r?   r   r"   r+   )r'   r=   Ú	enc_queryr   r   r   r+   ¤   s    zGeoIP2.cityc             C   s&   |  j  | d d ƒ} |  j | ƒ d S)z9Return the country code for the given IP Address or FQDN.r>   TÚcountry_code)r?   r*   )r'   r=   r@   r   r   r   rA   ­   s    zGeoIP2.country_codec             C   s&   |  j  | d d ƒ} |  j | ƒ d S)z9Return the country name for the given IP Address or FQDN.r>   TÚcountry_name)r?   r*   )r'   r=   r@   r   r   r   rB   ²   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.
        r>   T)r?   r   r.   )r'   r=   r@   r   r   r   r*   ·   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   )Ú.0Úo)Úcdictr   r   ú	<genexpr>Ç   s    z GeoIP2.coords.<locals>.<genexpr>)r+   Útuple)r'   r=   Zorderingr   )rG   r   ÚcoordsÂ   s    zGeoIP2.coordsc             C   s   |  j  | ƒ S)z@Return a tuple of the (longitude, latitude) for the given query.)rJ   )r'   r=   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.rD   rC   )rD   rC   )rJ   )r'   r=   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)rK   Zdjango.contrib.gis.geosrM   )r'   r=   ZllrM   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
)r$   r%   r3   r4   )r'   r6   r   r   r   ÚinfoÛ   s    zGeoIP2.infoc             C   s   t  | | ƒ S)N)r   )r1   Ú	full_pathr)   r   r   r   Úopená   s    zGeoIP2.open)z	longitudezlatitude) r   r   r   Z	MODE_AUTOZMODE_MMAP_EXTZ	MODE_MMAPZ	MODE_FILEZMODE_MEMORYÚ	frozensetr   r#   r!   r"   r    r-   Úpropertyr$   r.   r0   r7   r?   r+   rA   rB   r*   rJ   rK   rL   rN   rO   ÚclassmethodrQ   r   r   r   r   r      s4   F
	
r   )r;   Zgeoip2.databaser   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.validatorsr   Zdjango.utils._osr   Ú	resourcesr   r   Úgetattrr   Ú	Exceptionr   r   r   r   r   r   Ú<module>   s   