Package Ganga :: Package Utility :: Package Plugin :: Module GangaPlugin
[hide private]
[frames] | no frames]

Source Code for Module Ganga.Utility.Plugin.GangaPlugin

 1  import Ganga.Utility.logging 
 2  logger = Ganga.Utility.logging.getLogger() 
 3   
4 -class PluginManagerError(ValueError):
5 - def __init__(self,x): ValueError.__init__(self,x)
6 7 # Simple Ganga Plugin Mechanism 8 # 9 # Any object may be registered (added) in the plugin manager provided that 10 # you are able to specify the name and the category to which it belongs. 11 # 12 # If you do not use category all plugins are registered in a flat list. Otherwise 13 # there is a list of names for each category seaprately. 14 15
16 -class PluginManager(object):
17 - def __init__(self):
18 self.all_dict = {} 19 self.first = {}
20
21 - def find(self,category,name):
22 """ 23 Return a plugin added with 'name' in the given 'category'. 24 If 'name' is None then the default plugin in the category is returned. 25 Typically the default plugin is the first added. 26 If plugin not found raise PluginManagerError. 27 """ 28 try: 29 if name is None: 30 return self.first[category] 31 else: 32 return self.all_dict[category][name] 33 except KeyError: 34 if name is None: 35 s = "cannot find default plugin for category "+category 36 else: 37 s = "cannot find '%s' in a category '%s'" %(name,category) 38 39 logger.debug(s) 40 raise PluginManagerError(s)
41
42 - def add(self,pluginobj,category,name):
43 """ Add a pluginobj to the plugin manager with the name and the category labels. 44 The first plugin is default unless changed explicitly. 45 """ 46 cat = self.all_dict.setdefault(category,{}) 47 self.first.setdefault(category,pluginobj) 48 cat[name] = pluginobj 49 logger.debug('adding plugin %s (category "%s") ' % (name,category))
50
51 - def setDefault(self,category,name):
52 """ Make the plugin 'name' be default in a given 'category'. 53 You must first add() the plugin object before calling this method. Otherwise 54 PluginManagerError is raised. 55 """ 56 assert(not name is None) 57 pluginobj = self.find(category,name) 58 self.first[category] = pluginobj
59 60
61 - def allCategories(self):
62 return self.all_dict
63
64 - def allClasses(self, category):
65 cat = self.all_dict.get(category) 66 if cat: 67 return cat 68 else: 69 return {}
70 71 allPlugins = PluginManager() 72