<s> from ctypes import POINTER , Structure , c_char_p , c_float , c_int , string_at <EOL> from django . contrib . gis . geoip . libgeoip import free , lgeoip <EOL> class GeoIPRecord ( Structure ) : <EOL> _fields_ = [ ( '<STR_LIT>' , c_char_p ) , <EOL> ( '<STR_LIT>' , c_char_p ) , <EOL> ( '<STR_LIT>' , c_char_p ) , <EOL> ( '<STR_LIT>' , c_char_p ) , <EOL> ( '<STR_LIT>' , c_char_p ) , <EOL> ( '<STR_LIT>' , c_char_p ) , <EOL> ( '<STR_LIT>' , c_float ) , <EOL> ( '<STR_LIT>' , c_float ) , <EOL> ( '<STR_LIT>' , c_int ) , <EOL> ( '<STR_LIT>' , c_int ) , <EOL> ( '<STR_LIT>' , c_int ) , <EOL> ( '<STR_LIT>' , c_char_p ) , <EOL> ] <EOL> geoip_char_fields = [ name for name , ctype in GeoIPRecord . _fields_ if ctype is c_char_p ] <EOL> GEOIP_DEFAULT_ENCODING = '<STR_LIT>' <EOL> geoip_encodings = { <EOL> <NUM_LIT:0> : '<STR_LIT>' , <EOL> <NUM_LIT:1> : '<STR_LIT:utf8>' , <EOL> } <EOL> class GeoIPTag ( Structure ) : <EOL> pass <EOL> RECTYPE = POINTER ( GeoIPRecord ) <EOL> DBTYPE = POINTER ( GeoIPTag ) <EOL> if hasattr ( lgeoip , '<STR_LIT>' ) : <EOL> GeoIP_lib_version = lgeoip . GeoIP_lib_version <EOL> GeoIP_lib_version . argtypes = None <EOL> GeoIP_lib_version . restype = c_char_p <EOL> else : <EOL> GeoIP_lib_version = None <EOL> GeoIPRecord_delete = lgeoip . GeoIPRecord_delete <EOL> GeoIPRecord_delete . argtypes = [ RECTYPE ] <EOL> GeoIPRecord_delete . restype = None <EOL> def check_record ( result , func , cargs ) : <EOL> if result : <EOL> rec = result . contents <EOL> record = { fld : getattr ( rec , fld ) for fld , ctype in rec . _fields_ } <EOL> encoding = geoip_encodings [ record [ '<STR_LIT>' ] ] <EOL> for char_field in geoip_char_fields : <EOL> if record [ char_field ] : <EOL> record [ char_field ] = record [ char_field ] . decode ( encoding ) <EOL> GeoIPRecord_delete ( result ) <EOL> return record <EOL> else : <EOL> return None <EOL> def record_output ( func ) : <EOL> func . argtypes = [ DBTYPE , c_char_p ] <EOL> func . restype = RECTYPE <EOL> func . errcheck = check_record <EOL> return func <EOL> GeoIP_record_by_addr = record_output ( lgeoip . GeoIP_record_by_addr ) <EOL> GeoIP_record_by_name = record_output ( lgeoip . GeoIP_record_by_name ) <EOL> GeoIP_open = lgeoip . GeoIP_open <EOL> GeoIP_open . restype = DBTYPE <EOL> GeoIP_delete = lgeoip . GeoIP_delete <EOL> GeoIP_delete . argtypes = [ DBTYPE ] <EOL> GeoIP_delete . restype = None <EOL> class geoip_char_p ( c_char_p ) : <EOL> pass <EOL> def check_string ( result , func , cargs ) : <EOL> if result : <EOL> s = string_at ( result ) <EOL> free ( result ) <EOL> else : <EOL> s = '<STR_LIT>' <EOL> return s . decode ( GEOIP_DEFAULT_ENCODING ) <EOL> GeoIP_database_info = lgeoip . GeoIP_database_info <EOL> GeoIP_database_info . restype = geoip_char_p <EOL> GeoIP_database_info . errcheck = check_string <EOL> def string_output ( func ) : <EOL> def _err_check ( result , func , cargs ) : <EOL> if result : <EOL> return result . decode ( GEOIP_DEFAULT_ENCODING ) <EOL> return result <EOL> func . restype = c_char_p <EOL> func . errcheck = _err_check <EOL> return func <EOL> GeoIP_country_code_by_addr = string_output ( lgeoip . GeoIP_country_code_by_addr ) <EOL> GeoIP_country_code_by_name = string_output ( lgeoip . GeoIP_country_code_by_name ) <EOL> GeoIP_country_name_by_addr = string_output ( lgeoip . GeoIP_country_name_by_addr ) <EOL> GeoIP_country_name_by_name = string_output ( lgeoip . GeoIP_country_name_by_name ) </s>
