home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / OpenGL / lazywrapper.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  1.2 KB  |  43 lines

  1.  
  2. def lazy( baseFunction ):
  3.     """Produce a lazy-binding decorator that uses baseFunction
  4.     """
  5.     def wrap( wrapper ):
  6.         """Wrap wrapper with baseFunction"""
  7.         def __call__( self, *args, **named ):
  8.             if baseFunction:
  9.                 return wrapper( baseFunction, *args, **named )
  10.             else:
  11.                 return baseFunction( *args, **named )
  12.         def __nonzero__( self ):
  13.             return bool( baseFunction )
  14.         _with_wrapper = type( wrapper.__name__, (object,), {
  15.             '__call__': __call__,
  16.             '__doc__': wrapper.__doc__,
  17.             '__nonzero__': __nonzero__,
  18.             'restype': getattr(wrapper, 'restype',getattr(baseFunction,'restype',None)),
  19.         } )
  20.         with_wrapper = _with_wrapper()
  21.         with_wrapper.__name__ = wrapper.__name__
  22.         with_wrapper.baseFunction = baseFunction 
  23.         with_wrapper.wrapperFunction = wrapper
  24.         return with_wrapper
  25.     return wrap 
  26.  
  27.  
  28. if __name__ == "__main__":
  29.     from OpenGL.raw import GLU
  30.     func = GLU.gluNurbsCallbackData
  31.     output = []
  32.     def testwrap( base ):
  33.         "Testing"
  34.         output.append( base )
  35.     testlazy = lazy( func )( testwrap )
  36.     testlazy( )
  37.     assert testlazy.__doc__ == "Testing" 
  38.     assert testlazy.__class__.__name__ == 'testwrap'
  39.     assert testlazy.__name__ == 'testwrap'
  40.     assert testlazy.baseFunction is func 
  41.     assert testlazy.wrapperFunction is testwrap
  42.     assert output 
  43.