Package elisa :: Package extern :: Package coherence :: Module simple_plugin
[hide private]
[frames] | no frames]

Source Code for Module elisa.extern.coherence.simple_plugin

 1  # -*- coding: utf-8 -*- 
 2   
 3  # Licensed under the MIT license 
 4  # http://opensource.org/licenses/mit-license.php 
 5   
 6  # Copyright 2007, Frank Scholz <coherence@beebits.net> 
 7   
 8  """ real simple plugin system 
 9      meant as a replacement when setuptools/pkg_resources 
10      are not available 
11  """ 
12   
13  import os 
14  import sys 
15   
16 -class Plugin(object):
17 """ a new style class that 18 betrays all its sub-classes 19 """ 20 pass
21
22 -class Reception(object):
23 24 """ singleton class which holds information 25 about known plugins 26 27 currently a singleton, and even a class, 28 seems to be overkill for this, but maybe 29 we'll add some more functionality later 30 """ 31 32 _instance_ = None # Singleton 33
34 - def __new__(cls, *args, **kwargs):
35 """ creates the singleton """ 36 obj = getattr(cls,'_instance_',None) 37 if obj is not None: 38 return obj 39 else: 40 obj = super(Reception, cls).__new__(cls, *args, **kwargs) 41 cls._instance_ = obj 42 return obj
43
44 - def __init__(self,plugin_path=None,log=None):
45 """ initializes the class and 46 checks in if a path is provided 47 """ 48 self.log = log 49 if plugin_path is not None: 50 self.checkin(plugin_path)
51
52 - def checkin(self,plugin_path):
53 """ import all valid files from plugin_path """ 54 if not plugin_path in sys.path: 55 sys.path.insert(0, plugin_path) 56 for plugin in os.listdir(plugin_path): 57 p = os.path.join(plugin_path, plugin) 58 if plugin != '__init__.py' and os.path.isfile(p) and os.path.splitext(p)[1] == '.py': 59 try: 60 __import__(os.path.splitext(plugin)[0], None, None, ['']) 61 except Exception, msg: 62 if self.log is None: 63 print "can't import %r - %s" % (os.path.splitext(plugin)[0], msg) 64 else: 65 self.log("can't import %r - %r" % (os.path.splitext(plugin)[0], msg))
66 67
68 - def guestlist(self, plugin_class=Plugin):
69 """ returns a list of all Plugin subclasses """ 70 return plugin_class.__subclasses__()
71