summaryrefslogtreecommitdiffstats
path: root/scripts/lib/mic/plugin.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/mic/plugin.py')
-rw-r--r--scripts/lib/mic/plugin.py121
1 files changed, 121 insertions, 0 deletions
diff --git a/scripts/lib/mic/plugin.py b/scripts/lib/mic/plugin.py
new file mode 100644
index 0000000000..df03c15081
--- /dev/null
+++ b/scripts/lib/mic/plugin.py
@@ -0,0 +1,121 @@
1#!/usr/bin/python -tt
2#
3# Copyright (c) 2011 Intel, Inc.
4#
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
8#
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
12# for more details.
13#
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.
17
18import os, sys
19
20from mic import msger
21from mic import pluginbase
22from mic.utils import errors
23
24
25__ALL__ = ['PluginMgr', 'pluginmgr']
26
27PLUGIN_TYPES = ["imager", "source"] # TODO "hook"
28
29
30class PluginMgr(object):
31 plugin_dirs = {}
32
33 # make the manager class as singleton
34 _instance = None
35 def __new__(cls, *args, **kwargs):
36 if not cls._instance:
37 cls._instance = super(PluginMgr, cls).__new__(cls, *args, **kwargs)
38
39 return cls._instance
40
41 def __init__(self):
42 mic_path = os.path.dirname(__file__)
43 eos = mic_path.find('scripts') + len('scripts')
44 scripts_path = mic_path[:eos]
45
46 self.plugin_dir = scripts_path + "/lib/mic/plugins"
47
48 def append_dirs(self, dirs):
49 for path in dirs:
50 self._add_plugindir(path)
51
52 # load all the plugins AGAIN
53 self._load_all()
54
55 def _add_plugindir(self, path):
56 path = os.path.abspath(os.path.expanduser(path))
57
58 if not os.path.isdir(path):
59 msger.warning("Plugin dir is not a directory or does not exist: %s"\
60 % path)
61 return
62
63 if path not in self.plugin_dirs:
64 self.plugin_dirs[path] = False
65 # the value True/False means "loaded"
66
67 def _load_all(self):
68 for (pdir, loaded) in self.plugin_dirs.iteritems():
69 if loaded: continue
70
71 sys.path.insert(0, pdir)
72 for mod in [x[:-3] for x in os.listdir(pdir) if x.endswith(".py")]:
73 if mod and mod != '__init__':
74 if mod in sys.modules:
75 #self.plugin_dirs[pdir] = True
76 msger.warning("Module %s already exists, skip" % mod)
77 else:
78 try:
79 pymod = __import__(mod)
80 self.plugin_dirs[pdir] = True
81 msger.debug("Plugin module %s:%s imported"\
82 % (mod, pymod.__file__))
83 except ImportError, err:
84 msg = 'Failed to load plugin %s/%s: %s' \
85 % (os.path.basename(pdir), mod, err)
86 msger.warning(msg)
87
88 del(sys.path[0])
89
90 def get_plugins(self, ptype):
91 """ the return value is dict of name:class pairs """
92
93 if ptype not in PLUGIN_TYPES:
94 raise errors.CreatorError('%s is not valid plugin type' % ptype)
95
96 self._add_plugindir(os.path.join(self.plugin_dir, ptype))
97 self._load_all()
98
99 return pluginbase.get_plugins(ptype)
100
101 def get_source_plugin_methods(self, source_name, methods):
102 """
103 The methods param is a dict with the method names to find. On
104 return, the dict values will be filled in with pointers to the
105 corresponding methods. If one or more methods are not found,
106 None is returned.
107 """
108 return_methods = None
109 for _source_name, klass in self.get_plugins('source').iteritems():
110 if _source_name == source_name:
111 for _method_name in methods.keys():
112 if not hasattr(klass, _method_name):
113 msger.warning("Unimplemented %s source interface for: %s"\
114 % (_method_name, _source_name))
115 return None
116 func = getattr(klass, _method_name)
117 methods[_method_name] = func
118 return_methods = methods
119 return return_methods
120
121pluginmgr = PluginMgr()