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 / plugins.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  1.6 KB  |  58 lines

  1. """Simple plug-in mechanism to provide replacement for setuptools plugins"""
  2.  
  3. class Plugin( object ):
  4.     """Base class for plugins to be loaded"""
  5.     loaded = False
  6.     def __init__( self, name, import_path, check = None ):
  7.         """Register the plug-in"""
  8.         self.name = name 
  9.         self.import_path = import_path
  10.         self.check = check
  11.         self.registry.append( self )
  12.     def load( self ):
  13.         """Attempt to load and return our entry point"""
  14.         return importByName( self.import_path )
  15.     @classmethod
  16.     def match( cls, *args ):
  17.         """Match to return the plugin which is appropriate to load"""
  18.     @classmethod
  19.     def all( cls ):
  20.         """Iterate over all registered plugins"""
  21.         return cls.registry[:]
  22.     
  23. def importByName( fullName ):
  24.     """Import a class by name"""
  25.     name = fullName.split(".")
  26.     moduleName = name[:-1]
  27.     className = name[-1]
  28.     module = __import__( ".".join(moduleName), {}, {}, moduleName)
  29.     return getattr( module, className )
  30.  
  31.         
  32. class PlatformPlugin( Plugin ):
  33.     """Platform-level plugin registration"""
  34.     registry = []
  35.     @classmethod
  36.     def match( cls, key ):
  37.         """Determine what platform module to load
  38.         
  39.         key -- (sys.platform,os.name) key to load 
  40.         """
  41.         for plugin in cls.registry:
  42.             if plugin.name in key:
  43.                 return plugin
  44.         raise KeyError( """No platform plugin registered for %s"""%(key,))
  45.  
  46. class FormatHandler( Plugin ):
  47.     """Data-type storage-format handler"""
  48.     registry = []
  49.     @classmethod
  50.     def match( cls, value ):
  51.         """Lookup appropriate handler based on value (a type)"""
  52.         key = '%s.%s'%( value.__module__, value.__name__ )
  53.         for plugin in cls.registry:
  54.             set = getattr( plugin, 'check', ())
  55.             if set and key in set:
  56.                 return plugin
  57.         return None
  58.