PageRenderTime 101ms CodeModel.GetById 60ms app.highlight 3ms RepoModel.GetById 37ms app.codeStats 0ms

/django/contrib/gis/gdal/base.py

https://code.google.com/p/mango-py/
Python | 35 lines | 17 code | 6 blank | 12 comment | 2 complexity | fd696a7d2c3dc5bb3fb56de72b3d43fc MD5 | raw file
 1from ctypes import c_void_p
 2from types import NoneType
 3from django.contrib.gis.gdal.error import GDALException
 4
 5class GDALBase(object):
 6    """
 7    Base object for GDAL objects that has a pointer access property
 8    that controls access to the underlying C pointer.
 9    """
10    # Initially the pointer is NULL.
11    _ptr = None
12
13    # Default allowed pointer type.
14    ptr_type = c_void_p
15
16    # Pointer access property.
17    def _get_ptr(self):
18        # Raise an exception if the pointer isn't valid don't
19        # want to be passing NULL pointers to routines --
20        # that's very bad.
21        if self._ptr: return self._ptr
22        else: raise GDALException('GDAL %s pointer no longer valid.' % self.__class__.__name__)
23
24    def _set_ptr(self, ptr):
25        # Only allow the pointer to be set with pointers of the
26        # compatible type or None (NULL).
27        if isinstance(ptr, (int, long)):
28            self._ptr = self.ptr_type(ptr)
29        elif isinstance(ptr, (self.ptr_type, NoneType)):
30            self._ptr = ptr
31        else:
32            raise TypeError('Incompatible pointer type')
33
34    ptr = property(_get_ptr, _set_ptr)
35