<s> from __future__ import unicode_literals <EOL> import logging <EOL> import os <EOL> import re <EOL> from ctypes import c_char_p , c_int , CDLL , CFUNCTYPE <EOL> from ctypes . util import find_library <EOL> from django . contrib . gis . gdal . error import OGRException <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> try : <EOL> from django . conf import settings <EOL> lib_path = settings . GDAL_LIBRARY_PATH <EOL> except ( AttributeError , EnvironmentError , <EOL> ImportError , ImproperlyConfigured ) : <EOL> lib_path = None <EOL> if lib_path : <EOL> lib_names = None <EOL> elif os . name == '<STR_LIT>' : <EOL> lib_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> elif os . name == '<STR_LIT>' : <EOL> lib_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> raise OGRException ( '<STR_LIT>' % os . name ) <EOL> if lib_names : <EOL> for lib_name in lib_names : <EOL> lib_path = find_library ( lib_name ) <EOL> if not lib_path is None : break <EOL> if lib_path is None : <EOL> raise OGRException ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> '<STR_LIT>' . join ( lib_names ) ) <EOL> lgdal = CDLL ( lib_path ) <EOL> if os . name == '<STR_LIT>' : <EOL> from ctypes import WinDLL <EOL> lwingdal = WinDLL ( lib_path ) <EOL> def std_call ( func ) : <EOL> """<STR_LIT>""" <EOL> if os . name == '<STR_LIT>' : <EOL> return lwingdal [ func ] <EOL> else : <EOL> return lgdal [ func ] <EOL> _version_info = std_call ( '<STR_LIT>' ) <EOL> _version_info . argtypes = [ c_char_p ] <EOL> _version_info . restype = c_char_p <EOL> def gdal_version ( ) : <EOL> "<STR_LIT>" <EOL> return _version_info ( b'<STR_LIT>' ) <EOL> def gdal_full_version ( ) : <EOL> "<STR_LIT>" <EOL> return _version_info ( '<STR_LIT>' ) <EOL> version_regex = re . compile ( r'<STR_LIT>' ) <EOL> def gdal_version_info ( ) : <EOL> ver = gdal_version ( ) . decode ( ) <EOL> m = version_regex . match ( ver ) <EOL> if not m : raise OGRException ( '<STR_LIT>' % ver ) <EOL> return dict ( [ ( key , m . group ( key ) ) for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> _verinfo = gdal_version_info ( ) <EOL> GDAL_MAJOR_VERSION = int ( _verinfo [ '<STR_LIT>' ] ) <EOL> GDAL_MINOR_VERSION = int ( _verinfo [ '<STR_LIT>' ] ) <EOL> GDAL_SUBMINOR_VERSION = _verinfo [ '<STR_LIT>' ] and int ( _verinfo [ '<STR_LIT>' ] ) <EOL> GDAL_VERSION = ( GDAL_MAJOR_VERSION , GDAL_MINOR_VERSION , GDAL_SUBMINOR_VERSION ) <EOL> del _verinfo <EOL> CPLErrorHandler = CFUNCTYPE ( None , c_int , c_int , c_char_p ) <EOL> def err_handler ( error_class , error_number , message ) : <EOL> logger . error ( '<STR_LIT>' % ( error_number , message ) ) <EOL> err_handler = CPLErrorHandler ( err_handler ) <EOL> def function ( name , args , restype ) : <EOL> func = std_call ( name ) <EOL> func . argtypes = args <EOL> func . restype = restype <EOL> return func <EOL> set_error_handler = function ( '<STR_LIT>' , [ CPLErrorHandler ] , CPLErrorHandler ) <EOL> set_error_handler ( err_handler ) </s>
