1
"""Base class for the various Python data-format storage type APIs
3
Data-type handlers are specified using OpenGL.plugins module
6
from OpenGL import plugins
8
class FormatHandler( object ):
9
"""Abstract class describing the handler interface
11
Each data-type handler is responsible for providing a number of methods
12
which allow it to manipulate (and create) instances of the data-type
15
LAZY_TYPE_REGISTRY = {} # more registrations
20
preferredOutput = None
22
GENERIC_OUTPUT_PREFERENCES = ['numpy','numeric','ctypesarrays']
23
ALL_OUTPUT_HANDLERS = []
25
"""Load all setuptools-registered FormatHandler classes
27
register a new datatype with code similar to this in your
28
package's setup.py for setuptools:
31
'OpenGL.arrays.formathandler':[
32
'numpy = OpenGL.arrays.numpymodule.NumpyHandler',
36
for entrypoint in plugins.FormatHandler.all():
37
cls.loadPlugin( entrypoint )
39
def loadPlugin( cls, entrypoint ):
40
"""Load a single entry-point via plugins module"""
41
if not entrypoint.loaded:
42
from OpenGL.arrays.arraydatatype import ArrayDatatype
44
plugin_class = entrypoint.load()
45
except ImportError, err:
46
from OpenGL import logs,WARN_ON_FORMAT_UNAVAILABLE
47
log = logs.getLog( 'OpenGL.formathandler' )
48
if WARN_ON_FORMAT_UNAVAILABLE:
53
'Unable to load registered array format handler %s:\n%s',
54
entrypoint.name, log.getException( err )
57
handler = plugin_class()
58
handler.register( handler.HANDLED_TYPES )
59
ArrayDatatype.getRegistry()[ entrypoint.name ] = handler
60
entrypoint.loaded = True
62
def typeLookup( cls, type ):
63
"""Lookup handler by data-type"""
64
registry = ArrayDatatype.getRegistry()
66
return registry[ type ]
68
key = '%s.%s'%(type.__module__,type.__name__)
69
plugin = cls.LAZY_TYPE_REGISTRY.get( key )
71
cls.loadPlugin( plugin )
72
return registry[ type ]
73
raise KeyError( """Unable to find data-format handler for %s"""%( type,))
74
loadAll = classmethod( loadAll )
76
def register( self, types=None ):
77
"""Register this class as handler for given set of types"""
78
from OpenGL.arrays.arraydatatype import ArrayDatatype
79
ArrayDatatype.getRegistry().register( self, types )
80
def registerReturn( self ):
81
"""Register this handler as the default return-type handler"""
82
from OpenGL.arrays.arraydatatype import ArrayDatatype
83
ArrayDatatype.getRegistry().registerReturn( self )
85
def from_param( self, value, typeCode=None ):
86
"""Convert to a ctypes pointer value"""
87
def dataPointer( self, value ):
88
"""return long for pointer value"""
89
def asArray( self, value, typeCode=None ):
90
"""Given a value, convert to array representation"""
91
def arrayToGLType( self, value ):
92
"""Given a value, guess OpenGL type of the corresponding pointer"""
93
def arraySize( self, value, typeCode = None ):
94
"""Given a data-value, calculate dimensions for the array"""
95
def unitSize( self, value, typeCode=None ):
96
"""Determine unit size of an array (if possible)"""
97
if self.baseType is not None:
99
def dimensions( self, value, typeCode=None ):
100
"""Determine dimensions of the passed array value (if possible)"""