1
2
3
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
24 """ Root plugin for all plugins. Receives the first browse which returns
25 the initial folder structure.
26 """
27 name = "rootplugin"
28 containers = {}
29
31 """ Constructor for the _RootPlugin class. """
32 self._id = 0
33
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
45 """ Adds root containers for the root plugin.
46 """
47 self.add_container("root")
48
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
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
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
115 object_id = 0
116
117 if parent_id != -1:
118 object_id = self._get_next_id()
119
120
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
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
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
186
195
202
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
224
234
244