测试gitnore
This commit is contained in:
@@ -11,7 +11,7 @@ downloaded from MaxMind at http://dev.maxmind.com/geoip/geoip2/geolite2/.
|
||||
Grab GeoLite2-Country.mmdb.gz and GeoLite2-City.mmdb.gz, and unzip them in the
|
||||
directory corresponding to settings.GEOIP_PATH.
|
||||
"""
|
||||
__all__ = ["HAS_GEOIP2"]
|
||||
__all__ = ['HAS_GEOIP2']
|
||||
|
||||
try:
|
||||
import geoip2 # NOQA
|
||||
@@ -19,6 +19,5 @@ except ImportError:
|
||||
HAS_GEOIP2 = False
|
||||
else:
|
||||
from .base import GeoIP2, GeoIP2Exception
|
||||
|
||||
HAS_GEOIP2 = True
|
||||
__all__ += ["GeoIP2", "GeoIP2Exception"]
|
||||
__all__ += ['GeoIP2', 'GeoIP2Exception']
|
||||
|
||||
@@ -11,9 +11,9 @@ from .resources import City, Country
|
||||
|
||||
# Creating the settings dictionary with any settings, if needed.
|
||||
GEOIP_SETTINGS = {
|
||||
"GEOIP_PATH": getattr(settings, "GEOIP_PATH", None),
|
||||
"GEOIP_CITY": getattr(settings, "GEOIP_CITY", "GeoLite2-City.mmdb"),
|
||||
"GEOIP_COUNTRY": getattr(settings, "GEOIP_COUNTRY", "GeoLite2-Country.mmdb"),
|
||||
'GEOIP_PATH': getattr(settings, 'GEOIP_PATH', None),
|
||||
'GEOIP_CITY': getattr(settings, 'GEOIP_CITY', 'GeoLite2-City.mmdb'),
|
||||
'GEOIP_COUNTRY': getattr(settings, 'GEOIP_COUNTRY', 'GeoLite2-Country.mmdb'),
|
||||
}
|
||||
|
||||
|
||||
@@ -33,13 +33,11 @@ class GeoIP2:
|
||||
MODE_FILE = 4
|
||||
# Load database into memory. Pure Python.
|
||||
MODE_MEMORY = 8
|
||||
cache_options = frozenset(
|
||||
(MODE_AUTO, MODE_MMAP_EXT, MODE_MMAP, MODE_FILE, MODE_MEMORY)
|
||||
)
|
||||
cache_options = frozenset((MODE_AUTO, MODE_MMAP_EXT, MODE_MMAP, MODE_FILE, MODE_MEMORY))
|
||||
|
||||
# Paths to the city & country binary databases.
|
||||
_city_file = ""
|
||||
_country_file = ""
|
||||
_city_file = ''
|
||||
_country_file = ''
|
||||
|
||||
# Initially, pointers to GeoIP file references are NULL.
|
||||
_city = None
|
||||
@@ -72,31 +70,29 @@ class GeoIP2:
|
||||
if cache in self.cache_options:
|
||||
self._cache = cache
|
||||
else:
|
||||
raise GeoIP2Exception("Invalid GeoIP caching option: %s" % cache)
|
||||
raise GeoIP2Exception('Invalid GeoIP caching option: %s' % cache)
|
||||
|
||||
# Getting the GeoIP data path.
|
||||
path = path or GEOIP_SETTINGS["GEOIP_PATH"]
|
||||
path = path or GEOIP_SETTINGS['GEOIP_PATH']
|
||||
if not path:
|
||||
raise GeoIP2Exception(
|
||||
"GeoIP path must be provided via parameter or the GEOIP_PATH setting."
|
||||
)
|
||||
raise GeoIP2Exception('GeoIP path must be provided via parameter or the GEOIP_PATH setting.')
|
||||
|
||||
path = to_path(path)
|
||||
if path.is_dir():
|
||||
# Constructing the GeoIP database filenames using the settings
|
||||
# dictionary. If the database files for the GeoLite country
|
||||
# and/or city datasets exist, then try to open them.
|
||||
country_db = path / (country or GEOIP_SETTINGS["GEOIP_COUNTRY"])
|
||||
country_db = path / (country or GEOIP_SETTINGS['GEOIP_COUNTRY'])
|
||||
if country_db.is_file():
|
||||
self._country = geoip2.database.Reader(str(country_db), mode=cache)
|
||||
self._country_file = country_db
|
||||
|
||||
city_db = path / (city or GEOIP_SETTINGS["GEOIP_CITY"])
|
||||
city_db = path / (city or GEOIP_SETTINGS['GEOIP_CITY'])
|
||||
if city_db.is_file():
|
||||
self._city = geoip2.database.Reader(str(city_db), mode=cache)
|
||||
self._city_file = city_db
|
||||
if not self._reader:
|
||||
raise GeoIP2Exception("Could not load a database from %s." % path)
|
||||
raise GeoIP2Exception('Could not load a database from %s.' % path)
|
||||
elif path.is_file():
|
||||
# Otherwise, some detective work will be needed to figure out
|
||||
# whether the given database path is for the GeoIP country or city
|
||||
@@ -104,20 +100,18 @@ class GeoIP2:
|
||||
reader = geoip2.database.Reader(str(path), mode=cache)
|
||||
db_type = reader.metadata().database_type
|
||||
|
||||
if db_type.endswith("City"):
|
||||
if db_type.endswith('City'):
|
||||
# GeoLite City database detected.
|
||||
self._city = reader
|
||||
self._city_file = path
|
||||
elif db_type.endswith("Country"):
|
||||
elif db_type.endswith('Country'):
|
||||
# GeoIP Country database detected.
|
||||
self._country = reader
|
||||
self._country_file = path
|
||||
else:
|
||||
raise GeoIP2Exception(
|
||||
"Unable to recognize database edition: %s" % db_type
|
||||
)
|
||||
raise GeoIP2Exception('Unable to recognize database edition: %s' % db_type)
|
||||
else:
|
||||
raise GeoIP2Exception("GeoIP path must be a valid file or directory.")
|
||||
raise GeoIP2Exception('GeoIP path must be a valid file or directory.')
|
||||
|
||||
@property
|
||||
def _reader(self):
|
||||
@@ -137,37 +131,27 @@ class GeoIP2:
|
||||
|
||||
def __repr__(self):
|
||||
meta = self._reader.metadata()
|
||||
version = "[v%s.%s]" % (
|
||||
meta.binary_format_major_version,
|
||||
meta.binary_format_minor_version,
|
||||
)
|
||||
return (
|
||||
'<%(cls)s %(version)s _country_file="%(country)s", _city_file="%(city)s">'
|
||||
% {
|
||||
"cls": self.__class__.__name__,
|
||||
"version": version,
|
||||
"country": self._country_file,
|
||||
"city": self._city_file,
|
||||
}
|
||||
)
|
||||
version = '[v%s.%s]' % (meta.binary_format_major_version, meta.binary_format_minor_version)
|
||||
return '<%(cls)s %(version)s _country_file="%(country)s", _city_file="%(city)s">' % {
|
||||
'cls': self.__class__.__name__,
|
||||
'version': version,
|
||||
'country': self._country_file,
|
||||
'city': self._city_file,
|
||||
}
|
||||
|
||||
def _check_query(self, query, country=False, city=False, city_or_country=False):
|
||||
"Check the query and database availability."
|
||||
# Making sure a string was passed in for the query.
|
||||
if not isinstance(query, str):
|
||||
raise TypeError(
|
||||
"GeoIP query must be a string, not type %s" % type(query).__name__
|
||||
)
|
||||
raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__)
|
||||
|
||||
# Extra checks for the existence of country and city databases.
|
||||
if city_or_country and not (self._country or self._city):
|
||||
raise GeoIP2Exception("Invalid GeoIP country and city data files.")
|
||||
raise GeoIP2Exception('Invalid GeoIP country and city data files.')
|
||||
elif country and not self._country:
|
||||
raise GeoIP2Exception(
|
||||
"Invalid GeoIP country data file: %s" % self._country_file
|
||||
)
|
||||
raise GeoIP2Exception('Invalid GeoIP country data file: %s' % self._country_file)
|
||||
elif city and not self._city:
|
||||
raise GeoIP2Exception("Invalid GeoIP city data file: %s" % self._city_file)
|
||||
raise GeoIP2Exception('Invalid GeoIP city data file: %s' % self._city_file)
|
||||
|
||||
# Return the query string back to the caller. GeoIP2 only takes IP addresses.
|
||||
try:
|
||||
@@ -189,12 +173,12 @@ class GeoIP2:
|
||||
def country_code(self, query):
|
||||
"Return the country code for the given IP Address or FQDN."
|
||||
enc_query = self._check_query(query, city_or_country=True)
|
||||
return self.country(enc_query)["country_code"]
|
||||
return self.country(enc_query)['country_code']
|
||||
|
||||
def country_name(self, query):
|
||||
"Return the country name for the given IP Address or FQDN."
|
||||
enc_query = self._check_query(query, city_or_country=True)
|
||||
return self.country(enc_query)["country_name"]
|
||||
return self.country(enc_query)['country_name']
|
||||
|
||||
def country(self, query):
|
||||
"""
|
||||
@@ -207,7 +191,7 @@ class GeoIP2:
|
||||
return Country(self._country_or_city(enc_query))
|
||||
|
||||
# #### Coordinate retrieval routines ####
|
||||
def coords(self, query, ordering=("longitude", "latitude")):
|
||||
def coords(self, query, ordering=('longitude', 'latitude')):
|
||||
cdict = self.city(query)
|
||||
if cdict is None:
|
||||
return None
|
||||
@@ -220,14 +204,13 @@ class GeoIP2:
|
||||
|
||||
def lat_lon(self, query):
|
||||
"Return a tuple of the (latitude, longitude) for the given query."
|
||||
return self.coords(query, ("latitude", "longitude"))
|
||||
return self.coords(query, ('latitude', 'longitude'))
|
||||
|
||||
def geos(self, query):
|
||||
"Return a GEOS Point object for the given query."
|
||||
ll = self.lon_lat(query)
|
||||
if ll:
|
||||
from django.contrib.gis.geos import Point
|
||||
|
||||
return Point(ll, srid=4326)
|
||||
else:
|
||||
return None
|
||||
@@ -237,10 +220,7 @@ class GeoIP2:
|
||||
def info(self):
|
||||
"Return information about the GeoIP library and databases in use."
|
||||
meta = self._reader.metadata()
|
||||
return "GeoIP Library:\n\t%s.%s\n" % (
|
||||
meta.binary_format_major_version,
|
||||
meta.binary_format_minor_version,
|
||||
)
|
||||
return 'GeoIP Library:\n\t%s.%s\n' % (meta.binary_format_major_version, meta.binary_format_minor_version)
|
||||
|
||||
@classmethod
|
||||
def open(cls, full_path, cache):
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
def City(response):
|
||||
return {
|
||||
"city": response.city.name,
|
||||
"continent_code": response.continent.code,
|
||||
"continent_name": response.continent.name,
|
||||
"country_code": response.country.iso_code,
|
||||
"country_name": response.country.name,
|
||||
"dma_code": response.location.metro_code,
|
||||
"is_in_european_union": response.country.is_in_european_union,
|
||||
"latitude": response.location.latitude,
|
||||
"longitude": response.location.longitude,
|
||||
"postal_code": response.postal.code,
|
||||
"region": response.subdivisions[0].iso_code if response.subdivisions else None,
|
||||
"time_zone": response.location.time_zone,
|
||||
'city': response.city.name,
|
||||
'continent_code': response.continent.code,
|
||||
'continent_name': response.continent.name,
|
||||
'country_code': response.country.iso_code,
|
||||
'country_name': response.country.name,
|
||||
'dma_code': response.location.metro_code,
|
||||
'is_in_european_union': response.country.is_in_european_union,
|
||||
'latitude': response.location.latitude,
|
||||
'longitude': response.location.longitude,
|
||||
'postal_code': response.postal.code,
|
||||
'region': response.subdivisions[0].iso_code if response.subdivisions else None,
|
||||
'time_zone': response.location.time_zone,
|
||||
}
|
||||
|
||||
|
||||
def Country(response):
|
||||
return {
|
||||
"country_code": response.country.iso_code,
|
||||
"country_name": response.country.name,
|
||||
'country_code': response.country.iso_code,
|
||||
'country_name': response.country.name,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user