PageRenderTime 19ms CodeModel.GetById 6ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/gis/gdal/prototypes/generation.py

https://code.google.com/p/mango-py/
Python | 119 lines | 63 code | 14 blank | 42 comment | 6 complexity | 8f1b7fbdf0adad1aaf6a3c3af9acd7f4 MD5 | raw file
  1"""
  2 This module contains functions that generate ctypes prototypes for the
  3 GDAL routines.
  4"""
  5
  6from ctypes import c_char_p, c_double, c_int, c_void_p
  7from django.contrib.gis.gdal.prototypes.errcheck import \
  8    check_arg_errcode, check_errcode, check_geom, check_geom_offset, \
  9    check_pointer, check_srs, check_str_arg, check_string, check_const_string
 10
 11class gdal_char_p(c_char_p):
 12    pass
 13
 14def double_output(func, argtypes, errcheck=False, strarg=False):
 15    "Generates a ctypes function that returns a double value."
 16    func.argtypes = argtypes
 17    func.restype = c_double
 18    if errcheck: func.errcheck = check_arg_errcode
 19    if strarg: func.errcheck = check_str_arg
 20    return func
 21
 22def geom_output(func, argtypes, offset=None):
 23    """
 24    Generates a function that returns a Geometry either by reference
 25    or directly (if the return_geom keyword is set to True).
 26    """
 27    # Setting the argument types
 28    func.argtypes = argtypes
 29
 30    if not offset:
 31        # When a geometry pointer is directly returned.
 32        func.restype = c_void_p
 33        func.errcheck = check_geom
 34    else:
 35        # Error code returned, geometry is returned by-reference.
 36        func.restype = c_int
 37        def geomerrcheck(result, func, cargs):
 38            return check_geom_offset(result, func, cargs, offset)
 39        func.errcheck = geomerrcheck
 40
 41    return func
 42
 43def int_output(func, argtypes):
 44    "Generates a ctypes function that returns an integer value."
 45    func.argtypes = argtypes
 46    func.restype = c_int
 47    return func
 48
 49def srs_output(func, argtypes):
 50    """
 51    Generates a ctypes prototype for the given function with
 52    the given C arguments that returns a pointer to an OGR
 53    Spatial Reference System.
 54    """
 55    func.argtypes = argtypes
 56    func.restype = c_void_p
 57    func.errcheck = check_srs
 58    return func
 59
 60def const_string_output(func, argtypes, offset=None):
 61    func.argtypes = argtypes
 62    if offset:
 63        func.restype = c_int
 64    else:
 65        func.restype = c_char_p
 66
 67    def _check_const(result, func, cargs):
 68        return check_const_string(result, func, cargs, offset=offset)
 69    func.errcheck = _check_const
 70
 71    return func
 72
 73def string_output(func, argtypes, offset=-1, str_result=False):
 74    """
 75    Generates a ctypes prototype for the given function with the
 76    given argument types that returns a string from a GDAL pointer.
 77    The `const` flag indicates whether the allocated pointer should 
 78    be freed via the GDAL library routine VSIFree -- but only applies
 79    only when `str_result` is True.
 80    """
 81    func.argtypes = argtypes
 82    if str_result:
 83        # Use subclass of c_char_p so the error checking routine
 84        # can free the memory at the pointer's address.
 85        func.restype = gdal_char_p
 86    else:
 87        # Error code is returned
 88        func.restype = c_int
 89
 90    # Dynamically defining our error-checking function with the
 91    # given offset.
 92    def _check_str(result, func, cargs):
 93        return check_string(result, func, cargs,
 94                            offset=offset, str_result=str_result)
 95    func.errcheck = _check_str
 96    return func
 97
 98def void_output(func, argtypes, errcheck=True):
 99    """
100    For functions that don't only return an error code that needs to
101    be examined.
102    """
103    if argtypes: func.argtypes = argtypes
104    if errcheck:
105        # `errcheck` keyword may be set to False for routines that
106        # return void, rather than a status code.
107        func.restype = c_int
108        func.errcheck = check_errcode
109    else:
110        func.restype = None
111        
112    return func
113
114def voidptr_output(func, argtypes):
115    "For functions that return c_void_p."
116    func.argtypes = argtypes
117    func.restype = c_void_p
118    func.errcheck = check_pointer
119    return func