o xg$@sddlZddlZddlmZddlmZddlmZddl m Z ddl m Z m Z eeddeed d eed d d ZGdddeZGdddZdS)N)settings)ValidationError)validate_ipv46_address)to_path)CityCountry GEOIP_PATH GEOIP_CITYzGeoLite2-City.mmdb GEOIP_COUNTRYzGeoLite2-Country.mmdb)r r r c@s eZdZdS)GeoIP2ExceptionN)__name__ __module__ __qualname__rr@/usr/lib/python3/dist-packages/django/contrib/gis/geoip2/base.pyr sr c@seZdZdZdZdZdZdZeeeeeefZ dZ dZ dZ dZ d*dd Zed d Zed d ZddZddZd+ddZddZddZddZddZd,ddZd d!Zd"d#Zd$d%Zed&d'Zed(d)ZdS)-GeoIP2rrNc Cs>||jvr ||_ntd||ptdd}|stdt|}|rh||p-tdd}|rAtj j t ||d|_ ||_ ||pItdd }|r]tj j t ||d|_||_|jsftd |dS|rtj j t ||d}|j}|d r||_||_dS|d r||_ ||_ dStd |td)a 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. If neither is set, defaults to '/usr/share/GeoIP'. * 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. If neither is set, defaults to 'GeoIP.dat'. * city: The name of the GeoIP city data file. Defaults to 'GeoLite2-City.mmdb'; overrides the GEOIP_CITY setting. If neither is set, defaults to 'GeoIPCity.dat'. z Invalid GeoIP caching option: %sr z/usr/share/GeoIPzDGeoIP path must be provided via parameter or the GEOIP_PATH setting.r z GeoIP.dat)moder z GeoIPCity.datz"Could not load a database from %s.rrz(Unable to recognize database edition: %sz-GeoIP path must be a valid file or directory.N) cache_options_cacher GEOIP_SETTINGSgetris_diris_filegeoip2databaseReaderstr_country _country_file_city _city_file_readermetadata database_typeendswith) selfpathcachecountrycity country_dbcity_dbreaderdb_typerrr__init__.s<         zGeoIP2.__init__cCs |jp|jSN)r"r$r*rrrr&w zGeoIP2._readercCs|jr|jjS|jjSr4)r"r-r$r.r5rrr_country_or_city{szGeoIP2._country_or_citycCs|jr |jdSdSr4)r&closer5rrr__del__szGeoIP2.__del__cCs4|j}d|j|jf}d|jj||j|jdS)Nz[v%s.%s]zH<%(cls)s %(version)s _country_file="%(country)s", _city_file="%(city)s">)clsversionr-r.)r&r'binary_format_major_versionbinary_format_minor_version __class__r r#r%)r*metar;rrr__repr__s zGeoIP2.__repr__FcCst|tstdt|j|r|js|jstd|r&|js&td|j|r2|js2td|j zt |W|St yHt |}Y|Sw)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! TypeErrortyper r"r$r r#r%rrsocket gethostbyname)r*queryr-r.city_or_countryrrr _check_querys     zGeoIP2._check_querycCs|j|dd}t|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). T)r.)rHrr$r.r*rF enc_queryrrrr.sz GeoIP2.citycC|j|dd}||dS)z9Return the country code for the given IP Address or FQDN.TrG country_coderHr-rIrrrrMzGeoIP2.country_codecCrK)z9Return the country name for the given IP Address or FQDN.TrL country_namerNrIrrrrPrOzGeoIP2.country_namecCs|j|dd}t||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. TrL)rHrr7rIrrrr-szGeoIP2.country longitudelatitudecs,||dur dStfdd|DS)Nc3s|]}|VqdSr4r).0ocdictrr sz GeoIP2.coords..)r.tuple)r*rForderingrrVrcoordss z GeoIP2.coordscCs ||S)z@Return a tuple of the (longitude, latitude) for the given query.r[r*rFrrrlon_lat zGeoIP2.lon_latcCs ||dS)z@Return a tuple of the (latitude, longitude) for the given query.)rSrRr\r]rrrlat_lonr6zGeoIP2.lat_loncCs*||}|rddlm}||ddSdS)z/Return a GEOS Point object for the given query.r)Pointi)sridN)r^django.contrib.gis.geosra)r*rFllrarrrgeoss   z GeoIP2.geoscCs|j}d|j|jfS)z@Return information about the GeoIP library and databases in use.zGeoIP Library: %s.%s )r&r'r<r=)r*r?rrrinfos z GeoIP2.infocCs t||Sr4)r)r: full_pathr,rrropenr_z GeoIP2.open)NrNN)FFF)rQ) r rr MODE_AUTO MODE_MMAP_EXT MODE_MMAP MODE_FILE MODE_MEMORY frozensetrr%r#r$r"r3propertyr&r7r9r@rHr.rMrPr-r[r^r`rerf classmethodrhrrrrrs> I      r)rDgeoip2.databaser django.confrdjango.core.exceptionsrdjango.core.validatorsrdjango.utils._osr resourcesrrgetattrr Exceptionr rrrrrs