î
ªÍ XS.  ã               @   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 Gd d „  d e ƒ Z Gd 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   úK/home/ubuntu/projects/ifolica/build/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 d	 d d	 d	 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 d0 d 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)1Ú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 | ƒ ‚ | sk t j d ƒ } | sk t d ƒ ‚ qk n  t | t	 j
 ƒ s™ t d t | ƒ j ƒ ‚ n  t j j | ƒ rht j j | | pÌ t j d d ƒ ƒ } t j j | ƒ rt t | ƒ | ƒ |  _ | |  _ n  t j j | | p)t j d	 d
 ƒ ƒ } t j j | ƒ rt t | ƒ | ƒ |  _ | |  _ qnš t j j | ƒ röt t | ƒ | ƒ } t | ƒ } t j | ƒ r¿| |  _ | |  _ qt j | ƒ rã| |  _ | |  _ q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   sF   t  d  k r d  S|  j r) t  |  j ƒ n  |  j rB t  |  j ƒ n  d  S)N)r	   r0   r2   )r7   r   r   r   Ú__del__‹   s    		zGeoIP.__del__c             C   s\   d } t  d  k	 r, | d t t  ƒ  ƒ 7} n  d i |  j j d 6| d 6|  j d 6|  j d 6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   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 | ƒ 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   sT   |  j  | d d ƒ} t j | ƒ r: t |  j t | ƒ ƒ St |  j t | ƒ ƒ Sd 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).
        r:   TN)rE   r   r5   r   r2   r   r   )r7   rC   Ú	enc_queryr   r   r   r:   °   s    z
GeoIP.cityc             C   se   |  j  | d d ƒ} |  j rP t j | ƒ r= t |  j | ƒ St |  j | ƒ Sn |  j | ƒ d Sd S)z:Returns the country code for the given IP Address or FQDN.rD   TÚcountry_codeN)rE   r0   r   r5   r   r   r:   )r7   rC   rF   r   r   r   rG   ¾   s    	zGeoIP.country_codec             C   se   |  j  | d d ƒ} |  j rP t j | ƒ r= t |  j | ƒ St |  j | ƒ Sn |  j | ƒ d Sd S)z:Returns the country name for the given IP Address or FQDN.rD   TÚcountry_nameN)rE   r0   r   r5   r   r   r:   )r7   rC   rF   r   r   r   rH   É   s    	zGeoIP.country_namec             C   s$   i |  j  | ƒ d 6|  j | ƒ d 6S)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                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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  | ƒ } | r5 d d l m } | | d d ƒSd Sd S)z0Returns a GEOS Point object for the given query.r   )ÚPointZsridiæ  N)rP   Zdjango.contrib.gis.geosrR   )r7   rC   ZllrR   r   r   r   Úgeosï   s
    z
GeoIP.geosc             C   s2   |  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   s2   |  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   s8   d } t  r  | d t  ƒ  7} n  | 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 | ƒ Sd  S)N)r2   r:   r9   )r7   Úargr   r   r   Ú_rec_by_arg  s    	zGeoIP._rec_by_arg)r   r   r   r   r   )z	longitudezlatitude)(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   F