3 # Copyright (c) 2011 Intel, Inc.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 from mic.utils import errors
23 __ALL__ = ['PluginMgr', 'pluginmgr']
25 DEFAULT_PLUGIN_LOCATION = "/usr/lib/mic/plugins"
27 PLUGIN_TYPES = ["imager", "backend"] # TODO "hook"
29 class PluginMgr(object):
32 # make the manager class as singleton
34 def __new__(cls, *args, **kwargs):
36 cls._instance = super(PluginMgr, cls).__new__(cls, *args, **kwargs)
43 def append_dirs(self, dirs):
45 self._add_plugindir(path)
47 # load all the plugins AGAIN
50 def _add_plugindir(self, path):
51 path = os.path.abspath(os.path.expanduser(path))
53 if not os.path.isdir(path):
54 msger.warning("Plugin dir is not a directory or does not exist: %s" % path)
57 if path not in self.plugin_dirs:
58 self.plugin_dirs[path] = False
59 # the value True/False means "loaded"
62 for (pdir, loaded) in self.plugin_dirs.iteritems():
65 sys.path.insert(0, pdir)
66 for mod in [x[:-3] for x in os.listdir(pdir) if x.endswith(".py")]:
67 if mod and mod != '__init__':
68 if mod in sys.modules:
69 #self.plugin_dirs[pdir] = True
70 msger.warning("Module %s already exists, skip" % mod)
73 pymod = __import__(mod)
74 self.plugin_dirs[pdir] = True
75 msger.debug("Plugin module %s:%s imported" % (mod, pymod.__file__))
76 except ImportError, e:
77 msger.warning('%s, skip plugin %s/%s' %(str(e), os.path.basename(pdir), mod))
81 def get_plugins(self, ptype):
82 """ the return value is dict of name:class pairs """
83 if ptype in PLUGIN_TYPES:
84 self._add_plugindir(os.path.join(DEFAULT_PLUGIN_LOCATION, ptype))
87 raise errors.CreatorError('%s is not valid plugin type' % ptype)
89 return pluginbase.get_plugins(ptype)
91 pluginmgr = PluginMgr()