~matobet/pyopengl/Python3

« back to all changes in this revision

Viewing changes to arrays/formathandler.py.bak

  • Committer: matobet at gmail
  • Date: 2010-06-26 14:11:04 UTC
  • Revision ID: matobet@gmail.com-20100626141104-k011ofmltgiiu60g
Initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Base class for the various Python data-format storage type APIs
 
2
 
 
3
Data-type handlers are specified using OpenGL.plugins module
 
4
"""
 
5
import ctypes
 
6
from OpenGL import plugins
 
7
 
 
8
class FormatHandler( object ):
 
9
    """Abstract class describing the handler interface
 
10
    
 
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 
 
13
    it represents.
 
14
    """
 
15
    LAZY_TYPE_REGISTRY = {}  # more registrations
 
16
    HANDLER_REGISTRY = {}
 
17
    baseType = None
 
18
    typeConstant = None
 
19
    HANDLED_TYPES = ()
 
20
    preferredOutput = None
 
21
    isOutput = False
 
22
    GENERIC_OUTPUT_PREFERENCES = ['numpy','numeric','ctypesarrays']
 
23
    ALL_OUTPUT_HANDLERS = []
 
24
    def loadAll( cls ):
 
25
        """Load all setuptools-registered FormatHandler classes
 
26
        
 
27
        register a new datatype with code similar to this in your
 
28
        package's setup.py for setuptools:
 
29
        
 
30
        entry_points = {
 
31
            'OpenGL.arrays.formathandler':[
 
32
                'numpy = OpenGL.arrays.numpymodule.NumpyHandler',
 
33
            ],
 
34
        }
 
35
        """
 
36
        for entrypoint in plugins.FormatHandler.all():
 
37
            cls.loadPlugin( entrypoint )
 
38
    @classmethod
 
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
 
43
            try:
 
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:
 
49
                    logFunc = log.warn
 
50
                else:
 
51
                    logFunc = log.info 
 
52
                logFunc(
 
53
                    'Unable to load registered array format handler %s:\n%s', 
 
54
                    entrypoint.name, log.getException( err )
 
55
                )
 
56
            else:
 
57
                handler = plugin_class()
 
58
                handler.register( handler.HANDLED_TYPES )
 
59
                ArrayDatatype.getRegistry()[ entrypoint.name ] = handler
 
60
            entrypoint.loaded = True
 
61
    @classmethod
 
62
    def typeLookup( cls, type ):
 
63
        """Lookup handler by data-type"""
 
64
        registry = ArrayDatatype.getRegistry()
 
65
        try:
 
66
            return registry[ type ]
 
67
        except KeyError, err:
 
68
            key = '%s.%s'%(type.__module__,type.__name__)
 
69
            plugin = cls.LAZY_TYPE_REGISTRY.get( key )
 
70
            if plugin:
 
71
                cls.loadPlugin( plugin )
 
72
                return registry[ type ]
 
73
            raise KeyError( """Unable to find data-format handler for %s"""%( type,))
 
74
    loadAll = classmethod( loadAll )
 
75
 
 
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 )
 
84
 
 
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:
 
98
            return 
 
99
    def dimensions( self, value, typeCode=None ):
 
100
        """Determine dimensions of the passed array value (if possible)"""