Mercurial > libervia-backend
comparison src/core/sat_main.py @ 1145:4e1a0a1523f1
core: more robust plugins importation
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 01 Sep 2014 11:05:27 +0200 |
parents | adea30ca0b51 |
children | 8cdb97e89d9b |
comparison
equal
deleted
inserted
replaced
1144:2481fa96ac1c | 1145:4e1a0a1523f1 |
---|---|
128 log.info(_("Memory initialised")) | 128 log.info(_("Memory initialised")) |
129 self._import_plugins() | 129 self._import_plugins() |
130 ui_contact_list.ContactList(self) | 130 ui_contact_list.ContactList(self) |
131 ui_profile_manager.ProfileManager(self) | 131 ui_profile_manager.ProfileManager(self) |
132 self._initialised.callback(None) | 132 self._initialised.callback(None) |
133 log.info(_("Backend is ready")) | |
133 | 134 |
134 def _import_plugins(self): | 135 def _import_plugins(self): |
135 """Import all plugins found in plugins directory""" | 136 """Import all plugins found in plugins directory""" |
136 import sat.plugins | 137 import sat.plugins |
137 plugins_path = os.path.dirname(sat.plugins.__file__) | 138 plugins_path = os.path.dirname(sat.plugins.__file__) |
138 plug_lst = [os.path.splitext(plugin)[0] for plugin in map(os.path.basename, glob(os.path.join(plugins_path, "plugin*.py")))] | 139 plug_lst = [os.path.splitext(plugin)[0] for plugin in map(os.path.basename, glob(os.path.join(plugins_path, "plugin*.py")))] |
139 __plugins_to_import = {} # plugins we still have to import | 140 __plugins_to_import = {} # plugins we still have to import |
140 for plug in plug_lst: | 141 for plug in plug_lst: |
141 plugin_path = 'sat.plugins.' + plug | 142 plugin_path = 'sat.plugins.' + plug |
142 __import__(plugin_path) | 143 try: |
144 __import__(plugin_path) | |
145 except ImportError as e: | |
146 log.error(_("Can't import plugin [%(path)s]: %(error)s") % {'path': plugin_path, 'error':e}) | |
147 continue | |
143 mod = sys.modules[plugin_path] | 148 mod = sys.modules[plugin_path] |
144 plugin_info = mod.PLUGIN_INFO | 149 plugin_info = mod.PLUGIN_INFO |
145 __plugins_to_import[plugin_info['import_name']] = (plugin_path, mod, plugin_info) | 150 __plugins_to_import[plugin_info['import_name']] = (plugin_path, mod, plugin_info) |
146 while True: | 151 while True: |
147 self._import_plugins_from_dict(__plugins_to_import) | 152 self._import_plugins_from_dict(__plugins_to_import) |
171 dependencies = plugin_info.setdefault("dependencies", []) | 176 dependencies = plugin_info.setdefault("dependencies", []) |
172 recommendations = plugin_info.setdefault("recommendations", []) | 177 recommendations = plugin_info.setdefault("recommendations", []) |
173 for to_import in dependencies + recommendations: | 178 for to_import in dependencies + recommendations: |
174 if to_import not in self.plugins: | 179 if to_import not in self.plugins: |
175 log.debug('Recursively import dependency of [%s]: [%s]' % (import_name, to_import)) | 180 log.debug('Recursively import dependency of [%s]: [%s]' % (import_name, to_import)) |
176 self._import_plugins_from_dict(plugins_to_import, to_import, to_import not in dependencies) | 181 try: |
182 self._import_plugins_from_dict(plugins_to_import, to_import, to_import not in dependencies) | |
183 except ImportError as e: | |
184 log.error(_("Can't import plugin %(name)s: %(error)s") % {'name':plugin_info['name'], 'error':e}) | |
185 return | |
177 log.info(_("importing plugin: %s") % plugin_info['name']) | 186 log.info(_("importing plugin: %s") % plugin_info['name']) |
178 self.plugins[import_name] = getattr(mod, plugin_info['main'])(self) | 187 self.plugins[import_name] = getattr(mod, plugin_info['main'])(self) |
179 if 'handler' in plugin_info and plugin_info['handler'] == 'yes': | 188 if 'handler' in plugin_info and plugin_info['handler'] == 'yes': |
180 self.plugins[import_name].is_handler = True | 189 self.plugins[import_name].is_handler = True |
181 else: | 190 else: |