2011-09-11 02:04:27 +08:00
|
|
|
import os
|
|
|
|
from ctypes import CDLL
|
|
|
|
from ctypes.util import find_library
|
2015-01-28 20:35:27 +08:00
|
|
|
|
2011-09-11 02:04:27 +08:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
# Creating the settings dictionary with any settings, if needed.
|
2014-12-07 05:00:09 +08:00
|
|
|
GEOIP_SETTINGS = {key: getattr(settings, key)
|
|
|
|
for key in ('GEOIP_PATH', 'GEOIP_LIBRARY_PATH', 'GEOIP_COUNTRY', 'GEOIP_CITY')
|
|
|
|
if hasattr(settings, key)}
|
2015-05-14 02:51:18 +08:00
|
|
|
lib_path = GEOIP_SETTINGS.get('GEOIP_LIBRARY_PATH')
|
2011-09-11 02:04:27 +08:00
|
|
|
|
|
|
|
# The shared library for the GeoIP C API. May be downloaded
|
|
|
|
# from http://www.maxmind.com/download/geoip/api/c/
|
|
|
|
if lib_path:
|
|
|
|
lib_name = None
|
|
|
|
else:
|
|
|
|
# TODO: Is this really the library name for Windows?
|
|
|
|
lib_name = 'GeoIP'
|
|
|
|
|
|
|
|
# Getting the path to the GeoIP library.
|
2013-10-17 16:17:41 +08:00
|
|
|
if lib_name:
|
|
|
|
lib_path = find_library(lib_name)
|
|
|
|
if lib_path is None:
|
|
|
|
raise RuntimeError('Could not find the GeoIP library (tried "%s"). '
|
|
|
|
'Try setting GEOIP_LIBRARY_PATH in your settings.' % lib_name)
|
2011-09-11 02:04:27 +08:00
|
|
|
lgeoip = CDLL(lib_path)
|
|
|
|
|
|
|
|
# Getting the C `free` for the platform.
|
|
|
|
if os.name == 'nt':
|
|
|
|
libc = CDLL('msvcrt')
|
|
|
|
else:
|
|
|
|
libc = CDLL(None)
|
|
|
|
free = libc.free
|