6 DEFAULT_PLUGIN_LOCATION = "/usr/lib/micng/plugins"
7 DEFINED_PLGUIN_TYPES = ["imager", "backend", "hook"]
8 STRING_PLUGIN_MARK = "mic_plugin"
9 STRING_PTYPE_MARK = "plugin_type"
11 class PluginMgr(object):
12 def __init__(self, plugin_dirs=[]):
13 self.plugin_locations = []
15 self.plugin_types = DEFINED_PLGUIN_TYPES
16 # initial plugin directory
17 self.addPluginDir(DEFAULT_PLUGIN_LOCATION)
18 for directory in plugin_dirs:
19 self.addPluginDir(os.path.expanduser(directory))
21 for plugintype in self.plugin_types:
22 self.plugin_sets[plugintype] = []
24 def addPluginDir(self, plugin_dir=None):
25 if not os.path.exists(plugin_dir):
26 logging.debug("Directory already exists: %s" % plugin_dir)
28 if not os.path.isdir(plugin_dir):
29 logging.debug("Not a directory: %s" % plugin_dir)
31 if not (plugin_dir in self.plugin_locations):
32 self.plugin_locations.append(plugin_dir)
34 def pluginCheck(self, pymod):
35 if not hasattr(pymod, STRING_PLUGIN_MARK):
36 logging.debug("Not a valid plugin: %s" % pymod.__file__)
37 logging.debug("Please check whether %s given" % STRING_PLUGIN_MARK)
39 plclass = getattr(pymod, STRING_PLUGIN_MARK)[1]
40 if not hasattr(plclass, STRING_PTYPE_MARK):
41 logging.debug("Not a valid plugin: %s" % pymod.__file__)
42 logging.debug("Please check whether %s given" % STRING_PTYPE_MARK)
44 pltype = getattr(plclass, STRING_PTYPE_MARK)
45 if not (pltype in self.plugin_types):
46 logging.debug("Unsupported plugin type in %s: %s" % (pymod.__file__, plugintype))
50 def importModule(self, dir_path, plugin_filename):
51 if plugin_filename.endswith(".pyc"):
53 if not plugin_filename.endswith(".py"):
54 logging.debug("Not a python file: %s" % os.path.join(dir_path, plugin_filename))
56 if plugin_filename == "__init__.py":
57 logging.debug("Unsupported python file: %s" % os.path.join(dir_path, plugin_filename))
59 modname = os.path.splitext(plugin_filename)[0]
60 if sys.modules.has_key(modname):
61 pymod = sys.modules[modname]
62 logging.debug("Module %s already exists: %s" % (modname, pymod.__file__))
64 pymod = __import__(modname)
65 pymod.__file__ = os.path.join(dir_path, plugin_filename)
66 if not self.pluginCheck(pymod):
67 logging.warn("Failed to check plugin: %s" % os.path.join(dir_path, plugin_filename))
69 (pname, pcls) = pymod.__dict__[STRING_PLUGIN_MARK]
70 plugintype = getattr(pcls, STRING_PTYPE_MARK)
71 self.plugin_sets[plugintype].append((pname, pcls))
73 def loadPlugins(self):
74 for pdir in map(os.path.abspath, self.plugin_locations):
75 for pitem in os.walk(pdir):
76 sys.path.insert(0, pitem[0])
78 self.importModule(pitem[0], pf)
81 def getPluginByCateg(self, categ = None):
82 if not (categ in self.plugin_types):
83 logging.warn("Failed to get plugin category: %s" % categ)
86 return self.plugin_sets[categ]
88 def getImagerPlugins(self):
89 return self.plugin_sets['imager']
91 def getBackendPlugins(self):
92 return self.plugin_sets['backend']
94 def getHookPlugins(self):
95 return self.plugin_sets['hook']
97 def listAllPlugins(self):
98 for key in self.plugin_sets.keys():
99 sys.stdout.write("plugin type (%s) :::\n" % key)
100 for item in self.plugin_sets[key]:
101 sys.stdout.write("%-6s: %s\n" % (item[0], item[1]))
103 def getPluginType(self, plugin_str):
106 if __name__ == "__main__":
107 logging.getLogger().setLevel(logging.DEBUG)
109 pluginmgr = PluginMgr()
110 pluginmgr.loadPlugins()
111 pluginmgr.listAllPlugins()