Skip to content

Commit 5787dca

Browse files
Update Shader __str__ and move _generate_enum_map.
_generate_enum_map is now in gl.py. Shader now generates __str__ descriptions that are in keeping with normal python __str__ results.
1 parent b79c358 commit 5787dca

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

pygly/gl.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@
3434
from pyrr.utils import all_parameters_as_numpy_arrays, parameters_as_numpy_arrays
3535

3636

37+
def _generate_enum_map( enum_names ):
38+
"""Convert dicts of format {'GL_ENUM_NAME': value, ...}
39+
to { GL_ENUM_NAME : value, ...}
40+
41+
Used to ignore NameErrors that would otherwise result from incomplete
42+
OpenGL implementations.
43+
"""
44+
map = {}
45+
for (key, value) in enum_names.items():
46+
try:
47+
map[ getattr(GL, key) ] = value
48+
except AttributeError:
49+
pass
50+
return map
51+
3752
def _extract_version(version):
3853
"""Extracts the major and minor versions from an OpenGL version string.
3954
@@ -144,6 +159,19 @@ def type_to_string( glType ):
144159
GL.GLdouble: "GLdouble",
145160
}[ glType ]
146161

162+
def enum_to_type( glEnum ):
163+
return {
164+
GL.constants.GL_CHAR: GL.constants.GLchar,
165+
GL.constants.GL_UNSIGNED_BYTE: GL.constants.GLubyte,
166+
GL.constants.GL_BYTE: GL.constants.GLbyte,
167+
GL.constants.GL_UNSIGNED_SHORT: GL.constants.GLushort,
168+
GL.constants.GL_SHORT: GL.constants.GLshort,
169+
GL.constants.GL_UNSIGNED_INT: GL.constants.GLuint,
170+
GL.constants.GL_INT: GL.constants.GLint,
171+
GL.constants.GL_FLOAT: GL.constants.GLfloat,
172+
GL.constants.GL_DOUBLE: GL.constants.GLdouble,
173+
}[ glEnum ]
174+
147175
@contextmanager
148176
def attributes( attribs ):
149177
"""Wraps glPushAttrib and glPopAttrib

pygly/shader.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969

7070
from pyrr.utils import parameters_as_numpy_arrays
7171

72+
from pygly.gl import _generate_enum_map
73+
7274

7375
def parse_shader_error( error ):
7476
"""Parses a single GLSL error and extracts the line number
@@ -242,21 +244,6 @@ def attribute_for_name( handle, name ):
242244
return None
243245

244246

245-
def _generate_enum_map( enum_names ):
246-
"""Convert dicts of format {'GL_ENUM_NAME': value, ...}
247-
to { GL_ENUM_NAME : value, ...}
248-
249-
Used to ignore NameErrors that would otherwise result from incomplete
250-
OpenGL implementations.
251-
"""
252-
map = {}
253-
for (key, value) in enum_names.items():
254-
try:
255-
map[ getattr(GL, key) ] = value
256-
except AttributeError:
257-
pass
258-
return map
259-
260247
#: processes our enumeration to string map and stores the result
261248
_enum_string_map = _generate_enum_map(
262249
{
@@ -470,7 +457,10 @@ def _print_shader_errors( self, buffer ):
470457
print( "\tCode: %s" % lines[ line - 1 ] )
471458

472459
def __str__( self ):
473-
string = "Shader:\t%s" % ( enum_to_string( self.type ) )
460+
string = "%s(type=%s)" % (
461+
self.__class__.__name__,
462+
enum_to_string( self.type )
463+
)
474464
return string
475465

476466

@@ -670,14 +660,10 @@ def __getitem__(self, name):
670660
raise KeyError( name )
671661

672662
def __str__( self ):
673-
string = \
674-
"ShaderProgram:\n" \
675-
"Linked:\t%s\n" \
676-
"%s\n" \
677-
"%s" % (
678-
str(self.linked),
679-
str(self.attributes),
680-
str(self.uniforms)
663+
string = "%s(uniforms=[%s], attributes=[%s])" % (
664+
self.__class__.__name__,
665+
str( self.uniforms ),
666+
str( self.attributes )
681667
)
682668
return string
683669

@@ -805,10 +791,13 @@ def __setitem__( self, name, value ):
805791
self[ name ].value = value
806792

807793
def __str__( self ):
808-
string = "Uniforms:\n"
794+
string = "%s(" % (self.__class__.__name__)
795+
809796
for uniform in self:
810-
string += str(uniform) + "\n"
811-
return string[:-1]
797+
string += str(uniform) + ", "
798+
string = string[:-2] + ")"
799+
800+
return string
812801

813802

814803
class Uniform( object ):
@@ -948,7 +937,7 @@ def value( self, *args ):
948937
def __str__( self ):
949938
"""Returns a human readable string representing the Uniform.
950939
"""
951-
return "%s:\t%s\t%s\t%d" % (
940+
return "%s(name=%s, type=%s, location=%d)" % (
952941
self.__class__.__name__,
953942
self.name,
954943
enum_to_string( self.type ),
@@ -1280,10 +1269,12 @@ def __setitem__( self, name, value ):
12801269
self[ name ].location = value
12811270

12821271
def __str__( self ):
1283-
string = "Attributes:\n"
1272+
string = "%s(" % (self.__class__.__name__)
1273+
12841274
for attribute in self:
1285-
string += str(attribute) + "\n"
1286-
return string[:-1]
1275+
string += str(attribute) + ", "
1276+
1277+
return string[:-2] + ")"
12871278

12881279

12891280
class Attribute( object ):
@@ -1338,7 +1329,7 @@ def location( self, location ):
13381329
def __str__( self ):
13391330
"""Returns a human readable string representing the Attribute.
13401331
"""
1341-
return "%s:\t%s\t%s\t%d" % (
1332+
return "%s(name=%s, type=%s, location=%d)" % (
13421333
self.__class__.__name__,
13431334
self.name,
13441335
enum_to_string( self.type ),

0 commit comments

Comments
 (0)