Package brisa :: Package core :: Module plugin_manager
[hide private]
[frames] | no frames]

Source Code for Module brisa.core.plugin_manager

  1  # Licensed under the MIT license 
  2  # http://opensource.org/licenses/mit-license.php or see LICENSE file. 
  3  # Copyright 2007-2008 Brisa Team <brisa-develop@garage.maemo.org> 
  4   
  5  """ Manager for plugins that implement the plugin interface defined on the 
  6  plugin module. 
  7  """ 
  8   
  9  __all__ = ('PluginManager', ) 
 10   
 11   
 12  import os 
 13   
 14  from brisa.core import log 
 15  from brisa.core.singleton import Singleton 
 16  from brisa.core.plugin import PluginInterface 
 17  from brisa.upnp.didl.didl_lite import Container 
 18   
 19   
 20  log = log.getLogger('core.PluginManager') 
 21   
 22   
23 -class RootPlugin(PluginInterface):
24 """ Root plugin for all plugins. Receives the first browse which returns 25 the initial folder structure. 26 """ 27 name = "rootplugin" 28 containers = {} 29
30 - def __init__(self):
31 """ Constructor for the _RootPlugin class. """ 32 self._id = 0
33
34 - def _get_next_id(self):
35 """ Method to get the next plugin id. 36 37 @return: the next plugin id 38 @rtype: integer 39 """ 40 41 self._id += 1 42 return self._id
43
44 - def load(self):
45 """ Adds root containers for the root plugin. 46 """ 47 self.add_container("root")
48
49 - def get_container(self, title):
50 """ Method to get the container which is equal to the title parameter 51 52 @param title: the title of the container which you want to get 53 @type title: string 54 55 @return: The container, if it's there 56 @rtype: None or dict 57 """ 58 for id, container in self.containers.items(): 59 if container.title == title: 60 return container 61 return None
62
63 - def unload(self):
64 """ Unload implementation. 65 """ 66 self.containers.clear()
67
68 - def browse(self, id, browse_flag, filter, starting_index, 69 requested_count, sort_criteria):
70 """ Browse implementation. 71 72 @param id: object to be browsed 73 @param browse_flag: UPnP flag 74 @param filter: filter parameter 75 @param starting_index: The starting intex of the browser 76 @param requested_count: Requested number of entries under the object 77 @param sort_criteria: sorting criteria 78 79 @type id: string 80 @type browse_flag: string 81 @type filter: string 82 @type starting_index: ui4 83 @type requested_count: ui4 84 @type sort_criteria: string 85 86 @return: the results of the browsing action 87 @rtype: string 88 """ 89 if browse_flag == "BrowseDirectChildren": 90 return self.containers[id].containers 91 else: 92 return [self.containers[id]]
93
94 - def search(self, id, browse_flag, filter, starting_index, requested_count, 95 sort_criteria):
96 """ TODO Search implementation. 97 """ 98 return None
99
100 - def add_container(self, title, parent_id=-1, plugin=None):
101 """ Adds a container to the root plugin. 102 103 @param title: title 104 @param parent_id: parent id 105 @param plugin: container owner plugin 106 107 @type title: string 108 @type parent_id: string 109 @type plugin: Plugin 110 111 @return: the added container 112 @rtype: MemDIDLContainer 113 """ 114 # Object id 115 object_id = 0 116 117 if parent_id != -1: 118 object_id = self._get_next_id() 119 120 # Namespace if plugin is present 121 if plugin != None: 122 if object_id == None: 123 raise Exception('Error adding root plugin container: you'\ 124 ' must define the object_id') 125 if not hasattr(plugin, 'browse') or not hasattr(plugin, "name"): 126 raise NotImplementedError('The plugin must implement the'\ 127 '\'browse\' method and assign '\ 128 'an attribute \'name\'.') 129 130 object_id = '%s:%d' % (plugin.name, object_id) 131 132 new_container = Container(id=str(object_id), 133 title=title, 134 parent_id=parent_id) 135 136 self.containers[str(object_id)] = new_container 137 138 if parent_id != -1: 139 self.containers[str(parent_id)].add_container(new_container) 140 141 return new_container
142 143
144 -class PluginManager(Singleton):
145 """ Manages plugins.' 146 """ 147 plugins_instances = {} 148 plugins_classes = {} 149
150 - def __init__(self, plugins_folder, plugins_modules_path):
151 """ Instantiates the plugin manager and recognizes the plugins. 152 153 @param plugins_folder: the name of the plugin folder 154 @param plugins_modules_path: the path to the plugin folder 155 156 @type plugins_folder: string 157 @type plugins_modules_path: string 158 """ 159 self._find_and_import_plugins(plugins_folder, plugins_modules_path) 160 self.root_plugin = RootPlugin() 161 self.root_plugin.load() 162 self.plugins_classes[self.root_plugin.name] = \ 163 self.root_plugin.__class__ 164 self._recognize_plugins()
165
166 - def _find_and_import_plugins(self, plugins_folder, plugins_modules_path):
167 for root, dirs, files in os.walk(plugins_folder): 168 if root != plugins_folder: 169 break 170 171 for dir in dirs: 172 if not dir.startswith("."): 173 try: 174 module_path = '%s.%s' % (plugins_modules_path, dir) 175 __import__(module_path) 176 except Exception, e: 177 msg = 'Error while importing %s plugin. The module '\ 178 'path used to load was: %s. Error: %s' % \ 179 (dir, module_path, e) 180 log.error(msg)
181
182 - def publish_plugins(self, webserver):
183 log.info('Publishing plugins for ContentDirectory') 184 for v in self.plugins_instances.values(): 185 v.publish(webserver)
186
187 - def load_plugins(self):
188 """ Instantiates plugins and loads them using PluginLoader. 189 """ 190 self._instantiate_plugins() 191 names = [p.name for p in self.plugins_instances.values() \ 192 if p.name!='rootplugin'] 193 log.info('Detected plugins: %s', ', '.join(names)) 194 self._load_plugins()
195
196 - def unload_plugins(self):
197 """ Unload all plugins. 198 """ 199 for p in self.plugins_instances.values(): 200 log.info('Unloading plugin %s', p.name) 201 p.unload()
202
203 - def _recognize_plugins(self):
204 """ Recognizes plugins classes. 205 """ 206 classes = PluginInterface.__subclasses__() 207 208 plugins_class_ref = self.plugins_classes.values() 209 for _class in classes: 210 if _class not in plugins_class_ref: 211 self.plugins_classes[_class.name] = _class
212
213 - def _instantiate_plugins(self):
214 """ Instantiates all plugin classes into the plugin manager. 215 """ 216 plugins = self.plugins_classes.values() 217 for plugin in plugins: 218 if plugin.usage: 219 try: 220 self.plugins_instances[plugin.name] = plugin() 221 except Exception, e: 222 log.error("Error while instantiating %s: %s" % \ 223 (plugin.name, str(e)))
224
225 - def _load_plugin(self, plugin):
226 """ Loads a plugin using PluginLoader. 227 228 @param plugin: plugin instance to be loaded 229 @type plugin: Plugin 230 """ 231 log.info('Loading plugin %s', plugin.name) 232 plugin.plugin_manager = self 233 plugin.execute()
234
235 - def _load_plugins(self):
236 """ Loads all plugins selected using _load_plugin() method. 237 """ 238 try: 239 plugins = self.plugins_instances.values() 240 for plugin in plugins: 241 self._load_plugin(plugin) 242 except Exception, e: 243 log.error("Error loading plugin %s: %s", plugin.name, e)
244