<s> import os <EOL> import re <EOL> from ctypes import c_char_p <EOL> from django . core . validators import ipv4_re <EOL> from django . contrib . gis . geoip . libgeoip import GEOIP_SETTINGS <EOL> from django . contrib . gis . geoip . prototypes import ( <EOL> GeoIPRecord , GeoIPTag , GeoIP_open , GeoIP_delete , GeoIP_database_info , <EOL> GeoIP_lib_version , GeoIP_record_by_addr , GeoIP_record_by_name , <EOL> GeoIP_country_code_by_addr , GeoIP_country_code_by_name , <EOL> GeoIP_country_name_by_addr , GeoIP_country_name_by_name ) <EOL> from django . utils import six <EOL> free_regex = re . compile ( r'<STR_LIT>' ) <EOL> lite_regex = re . compile ( r'<STR_LIT>' ) <EOL> class GeoIPException ( Exception ) : pass <EOL> class GeoIP ( object ) : <EOL> GEOIP_STANDARD = <NUM_LIT:0> <EOL> GEOIP_MEMORY_CACHE = <NUM_LIT:1> <EOL> GEOIP_CHECK_CACHE = <NUM_LIT:2> <EOL> GEOIP_INDEX_CACHE = <NUM_LIT:4> <EOL> GEOIP_MMAP_CACHE = <NUM_LIT:8> <EOL> cache_options = dict ( ( opt , None ) for opt in ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:8> ) ) <EOL> _city_file = '<STR_LIT>' <EOL> _country_file = '<STR_LIT>' <EOL> _city = None <EOL> _country = None <EOL> def __init__ ( self , path = None , cache = <NUM_LIT:0> , country = None , city = None ) : <EOL> """<STR_LIT>""" <EOL> if cache in self . cache_options : <EOL> self . _cache = cache <EOL> else : <EOL> raise GeoIPException ( '<STR_LIT>' % cache ) <EOL> if not path : <EOL> path = GEOIP_SETTINGS . get ( '<STR_LIT>' , None ) <EOL> if not path : raise GeoIPException ( '<STR_LIT>' ) <EOL> if not isinstance ( path , six . string_types ) : <EOL> raise TypeError ( '<STR_LIT>' % type ( path ) . __name__ ) <EOL> if os . path . isdir ( path ) : <EOL> country_db = os . path . join ( path , country or GEOIP_SETTINGS . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if os . path . isfile ( country_db ) : <EOL> self . _country = GeoIP_open ( country_db , cache ) <EOL> self . _country_file = country_db <EOL> city_db = os . path . join ( path , city or GEOIP_SETTINGS . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if os . path . isfile ( city_db ) : <EOL> self . _city = GeoIP_open ( city_db , cache ) <EOL> self . _city_file = city_db <EOL> elif os . path . isfile ( path ) : <EOL> ptr = GeoIP_open ( path , cache ) <EOL> info = GeoIP_database_info ( ptr ) <EOL> if lite_regex . match ( info ) : <EOL> self . _city = ptr <EOL> self . _city_file = path <EOL> elif free_regex . match ( info ) : <EOL> self . _country = ptr <EOL> self . _country_file = path <EOL> else : <EOL> raise GeoIPException ( '<STR_LIT>' % info ) <EOL> else : <EOL> raise GeoIPException ( '<STR_LIT>' ) <EOL> def __del__ ( self ) : <EOL> if self . _country : GeoIP_delete ( self . _country ) <EOL> if self . _city : GeoIP_delete ( self . _city ) <EOL> def _check_query ( self , query , country = False , city = False , city_or_country = False ) : <EOL> "<STR_LIT>" <EOL> if not isinstance ( query , six . string_types ) : <EOL> raise TypeError ( '<STR_LIT>' % type ( query ) . __name__ ) <EOL> query = query . encode ( '<STR_LIT:ascii>' ) <EOL> if city_or_country and not ( self . _country or self . _city ) : <EOL> raise GeoIPException ( '<STR_LIT>' ) <EOL> elif country and not self . _country : <EOL> raise GeoIPException ( '<STR_LIT>' % self . _country_file ) <EOL> elif city and not self . _city : <EOL> raise GeoIPException ( '<STR_LIT>' % self . _city_file ) <EOL> return query <EOL> def city ( self , query ) : <EOL> """<STR_LIT>""" <EOL> query = self . _check_query ( query , city = True ) <EOL> if ipv4_re . match ( query ) : <EOL> return GeoIP_record_by_addr ( self . _city , c_char_p ( query ) ) <EOL> else : <EOL> return GeoIP_record_by_name ( self . _city , c_char_p ( query ) ) <EOL> def country_code ( self , query ) : <EOL> "<STR_LIT>" <EOL> query = self . _check_query ( query , city_or_country = True ) <EOL> if self . _country : <EOL> if ipv4_re . match ( query ) : <EOL> return GeoIP_country_code_by_addr ( self . _country , query ) <EOL> else : <EOL> return GeoIP_country_code_by_name ( self . _country , query ) <EOL> else : <EOL> return self . city ( query ) [ '<STR_LIT>' ] <EOL> def country_name ( self , query ) : <EOL> "<STR_LIT>" <EOL> query = self . _check_query ( query , city_or_country = True ) <EOL> if self . _country : <EOL> if ipv4_re . match ( query ) : <EOL> return GeoIP_country_name_by_addr ( self . _country , query ) <EOL> else : <EOL> return GeoIP_country_name_by_name ( self . _country , query ) <EOL> else : <EOL> return self . city ( query ) [ '<STR_LIT>' ] <EOL> def country ( self , query ) : <EOL> """<STR_LIT>""" <EOL> return { '<STR_LIT>' : self . country_code ( query ) , <EOL> '<STR_LIT>' : self . country_name ( query ) , <EOL> } <EOL> def coords ( self , query , ordering = ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> cdict = self . city ( query ) <EOL> if cdict is None : return None <EOL> else : return tuple ( cdict [ o ] for o in ordering ) <EOL> def lon_lat ( self , query ) : <EOL> "<STR_LIT>" <EOL> return self . coords ( query ) <EOL> def lat_lon ( self , query ) : <EOL> "<STR_LIT>" <EOL> return self . coords ( query , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def geos ( self , query ) : <EOL> "<STR_LIT>" <EOL> ll = self . lon_lat ( query ) <EOL> if ll : <EOL> from django . contrib . gis . geos import Point <EOL> return Point ( ll , srid = <NUM_LIT> ) <EOL> else : <EOL> return None <EOL> @ property <EOL> def country_info ( self ) : <EOL> "<STR_LIT>" <EOL> if self . _country is None : <EOL> ci = '<STR_LIT>' % self . _country_file <EOL> else : <EOL> ci = GeoIP_database_info ( self . _country ) <EOL> return ci <EOL> @ property <EOL> def city_info ( self ) : <EOL> "<STR_LIT>" <EOL> if self . _city is None : <EOL> ci = '<STR_LIT>' % self . _city_file <EOL> else : <EOL> ci = GeoIP_database_info ( self . _city ) <EOL> return ci <EOL> @ property <EOL> def info ( self ) : <EOL> "<STR_LIT>" <EOL> info = '<STR_LIT>' <EOL> if GeoIP_lib_version : <EOL> info += '<STR_LIT>' % GeoIP_lib_version ( ) <EOL> return info + '<STR_LIT>' % ( self . country_info , self . city_info ) <EOL> @ classmethod <EOL> def open ( cls , full_path , cache ) : <EOL> return GeoIP ( full_path , cache ) <EOL> def _rec_by_arg ( self , arg ) : <EOL> if self . _city : <EOL> return self . city ( arg ) <EOL> else : <EOL> return self . country ( arg ) <EOL> region_by_addr = city <EOL> region_by_name = city <EOL> record_by_addr = _rec_by_arg <EOL> record_by_name = _rec_by_arg <EOL> country_code_by_addr = country_code <EOL> country_code_by_name = country_code <EOL> country_name_by_addr = country_name <EOL> country_name_by_name = country_name </s>
