ó
ø¢TQc           @   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	 m
 Z
 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 e j d ƒ Z e j d ƒ Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   c_char_p(   t   ipv4_re(   t   GEOIP_SETTINGS(   t   GeoIPRecordt   GeoIPTagt
   GeoIP_opent   GeoIP_deletet   GeoIP_database_infot   GeoIP_lib_versiont   GeoIP_record_by_addrt   GeoIP_record_by_namet   GeoIP_country_code_by_addrt   GeoIP_country_code_by_namet   GeoIP_country_name_by_addrt   GeoIP_country_name_by_name(   t   sixs   ^GEO-\d{3}FREEs   ^GEO-\d{3}LITEt   GeoIPExceptionc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyR      s    t   GeoIPc           B   sD  e  Z d  Z d Z d Z d Z d Z e d „  d Dƒ ƒ Z d Z	 d Z
 d Z d Z d d  d d d „ Z d „  Z e e e 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 e d „  ƒ Z e d „  ƒ Z d „  Z  e Z! e Z" e  Z# e  Z$ e Z% e Z& e Z' e Z( RS(   i    i   i   i   i   c         c   s   |  ] } | d  f Vq d  S(   N(   t   None(   t   .0t   opt(    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pys	   <genexpr>/   s    t    c   	      C   sä  | |  j  k r | |  _ n t d | ƒ ‚ | s[ t j d d ƒ } | s[ t d ƒ ‚ q[ n  t | t j ƒ s‰ t	 d t
 | ƒ j ƒ ‚ n  t j j | ƒ rLt j j | | p¼ t j d d ƒ ƒ } t j j | ƒ rò t | | ƒ |  _ | |  _ n  t j j | | pt j d d ƒ ƒ } t j j | ƒ ràt | | ƒ |  _ | |  _ qàn” t j j | ƒ rÔt | | ƒ } t | ƒ } t j | ƒ r| |  _ | |  _ qàt j | ƒ rÁ| |  _ | |  _ qàt d	 | ƒ ‚ n t d
 ƒ ‚ d S(   s©  
        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.
        s    Invalid GeoIP caching option: %st
   GEOIP_PATHsD   GeoIP path must be provided via parameter or the GEOIP_PATH setting.s   Invalid path type: %st   GEOIP_COUNTRYs	   GeoIP.datt
   GEOIP_CITYs   GeoLiteCity.dats(   Unable to recognize database edition: %ss-   GeoIP path must be a valid file or directory.N(   t   cache_optionst   _cacheR   R   t   getR   t
   isinstanceR   t   string_typest	   TypeErrort   typeR   t   ost   patht   isdirt   joint   isfileR   t   _countryt   _country_filet   _cityt
   _city_fileR   t
   lite_regext   matcht
   free_regex(	   t   selfR#   t   cachet   countryt   cityt
   country_dbt   city_dbt   ptrt   info(    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt   __init__9   s:     ''		c         C   s6   |  j  r t |  j  ƒ n  |  j r2 t |  j ƒ n  d  S(   N(   R'   R   R)   (   R.   (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt   __del__~   s    	 	 c         C   sµ   t  | t j ƒ s. t d t | ƒ j ƒ ‚ n  | j d ƒ } | re |  j pR |  j re t	 d ƒ ‚ nL | r‹ |  j r‹ t	 d |  j
 ƒ ‚ n& | r± |  j r± t	 d |  j ƒ ‚ n  | S(   s@   Helper routine for checking the query and database availability.s)   GeoIP query must be a string, not type %st   asciis*   Invalid GeoIP country and city data files.s#   Invalid GeoIP country data file: %ss    Invalid GeoIP city data file: %s(   R   R   R   R    R!   R   t   encodeR'   R)   R   R(   R*   (   R.   t   queryR0   R1   t   city_or_country(    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt   _check_queryƒ   s    c         C   sT   |  j  | d t ƒ} t j | ƒ r: t |  j t | ƒ ƒ St |  j t | ƒ ƒ Sd S(   sÇ   
        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).
        R1   N(   R<   t   TrueR   R,   R	   R)   R    R
   (   R.   R:   (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyR1   —   s    c         C   se   |  j  | d t ƒ} |  j rP t j | ƒ r= t |  j | ƒ St |  j | ƒ Sn |  j | ƒ d Sd S(   s:   Returns the country code for the given IP Address or FQDN.R;   t   country_codeN(   R<   R=   R'   R   R,   R   R   R1   (   R.   R:   (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyR>   ¥   s    	c         C   se   |  j  | d t ƒ} |  j rP t j | ƒ r= t |  j | ƒ St |  j | ƒ Sn |  j | ƒ d Sd S(   s:   Returns the country name for the given IP Address or FQDN.R;   t   country_nameN(   R<   R=   R'   R   R,   R   R   R1   (   R.   R:   (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyR?   °   s    	c         C   s$   i |  j  | ƒ d 6|  j | ƒ d 6S(   sê   
        Returns a dictonary with 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>   R?   (   R>   R?   (   R.   R:   (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyR0   »   s    t	   longitudet   latitudec            s=   |  j  | ƒ ‰  ˆ  d  k r d  St ‡  f d †  | Dƒ ƒ Sd  S(   Nc         3   s   |  ] } ˆ  | Vq d  S(   N(    (   R   t   o(   t   cdict(    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pys	   <genexpr>Ê   s    (   R1   R   t   tuple(   R.   R:   t   ordering(    (   RC   s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt   coordsÇ   s     c         C   s   |  j  | ƒ S(   sA   Returns a tuple of the (longitude, latitude) for the given query.(   RF   (   R.   R:   (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt   lon_latÌ   s    c         C   s   |  j  | d ƒ S(   sA   Returns a tuple of the (latitude, longitude) for the given query.RA   R@   (   RA   R@   (   RF   (   R.   R:   (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt   lat_lonÐ   s    c         C   s=   |  j  | ƒ } | r5 d d l m } | | d d ƒSd Sd S(   s0   Returns a GEOS Point object for the given query.iÿÿÿÿ(   t   Pointt   sridiæ  N(   RG   t   django.contrib.gis.geosRI   R   (   R.   R:   t   llRI   (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt   geosÔ   s
    c         C   s2   |  j  d k r d |  j } n t |  j  ƒ } | S(   s5   Returns information about the GeoIP country database.s   No GeoIP Country data in "%s"N(   R'   R   R(   R   (   R.   t   ci(    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt   country_infoÞ   s    c         C   s2   |  j  d k r d |  j } n t |  j  ƒ } | S(   s1   Retuns information about the GeoIP city database.s   No GeoIP City data in "%s"N(   R)   R   R*   R   (   R.   RN   (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt	   city_infoç   s    c         C   s8   d } t  r  | d t  ƒ  7} n  | d |  j |  j f S(   sA   Returns information about the GeoIP library and databases in use.R   s   GeoIP Library:
	%s
s   Country:
	%s
City:
	%s(   R   RO   RP   (   R.   R5   (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyR5   ð   s    c         C   s   t  | | ƒ S(   N(   R   (   t   clst	   full_pathR/   (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt   openù   s    c         C   s'   |  j  r |  j | ƒ S|  j | ƒ Sd  S(   N(   R)   R1   R0   (   R.   t   arg(    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt   _rec_by_argý   s    	(   i    i   i   i   i   N(   s	   longitudes   latitude()   R   R   t   GEOIP_STANDARDt   GEOIP_MEMORY_CACHEt   GEOIP_CHECK_CACHEt   GEOIP_INDEX_CACHEt   GEOIP_MMAP_CACHEt   dictR   R*   R(   R   R)   R'   R6   R7   t   FalseR<   R1   R>   R?   R0   RF   RG   RH   RM   t   propertyRO   RP   R5   t   classmethodRS   RU   t   region_by_addrt   region_by_namet   record_by_addrt   record_by_namet   country_code_by_addrt   country_code_by_namet   country_name_by_addrt   country_name_by_name(    (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyR      sD   E								
				(   R"   t   ret   ctypesR    t   django.core.validatorsR   t!   django.contrib.gis.geoip.libgeoipR   t#   django.contrib.gis.geoip.prototypesR   R   R   R   R   R   R	   R
   R   R   R   R   t   django.utilsR   t   compileR-   R+   t	   ExceptionR   t   objectR   (    (    (    s6   ../Django//lib/python/django/contrib/gis/geoip/base.pyt   <module>   s   R