Mercurial > libervia-backend
comparison src/core/sat_main.py @ 1503:f681788097ba
core (plugins): detect import_name conflicts
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 27 Aug 2015 17:59:15 +0200 |
parents | 704ca56f5ca9 |
children | 7d7e57a84792 |
comparison
equal
deleted
inserted
replaced
1502:566908d483f6 | 1503:f681788097ba |
---|---|
143 def _import_plugins(self): | 143 def _import_plugins(self): |
144 """Import all plugins found in plugins directory""" | 144 """Import all plugins found in plugins directory""" |
145 import sat.plugins | 145 import sat.plugins |
146 plugins_path = os.path.dirname(sat.plugins.__file__) | 146 plugins_path = os.path.dirname(sat.plugins.__file__) |
147 plug_lst = [os.path.splitext(plugin)[0] for plugin in map(os.path.basename, glob(os.path.join(plugins_path, "plugin*.py")))] | 147 plug_lst = [os.path.splitext(plugin)[0] for plugin in map(os.path.basename, glob(os.path.join(plugins_path, "plugin*.py")))] |
148 __plugins_to_import = {} # plugins we still have to import | 148 plugins_to_import = {} # plugins we still have to import |
149 for plug in plug_lst: | 149 for plug in plug_lst: |
150 plugin_path = 'sat.plugins.' + plug | 150 plugin_path = 'sat.plugins.' + plug |
151 try: | 151 try: |
152 __import__(plugin_path) | 152 __import__(plugin_path) |
153 except ImportError as e: | 153 except ImportError as e: |
154 log.error(_(u"Can't import plugin [%(path)s]: %(error)s") % {'path': plugin_path, 'error':e}) | 154 log.error(_(u"Can't import plugin [%(path)s]: %(error)s") % {'path': plugin_path, 'error':e}) |
155 continue | 155 continue |
156 mod = sys.modules[plugin_path] | 156 mod = sys.modules[plugin_path] |
157 plugin_info = mod.PLUGIN_INFO | 157 plugin_info = mod.PLUGIN_INFO |
158 __plugins_to_import[plugin_info['import_name']] = (plugin_path, mod, plugin_info) | 158 import_name = plugin_info['import_name'] |
159 if import_name in plugins_to_import: | |
160 log.error(_(u"Name conflict for import name [{import_name}], can't import plugin [{name}]").format(**plugin_info)) | |
161 continue | |
162 plugins_to_import[import_name] = (plugin_path, mod, plugin_info) | |
159 while True: | 163 while True: |
160 self._import_plugins_from_dict(__plugins_to_import) | 164 self._import_plugins_from_dict(plugins_to_import) |
161 if not __plugins_to_import: | 165 if not plugins_to_import: |
162 break | 166 break |
163 | 167 |
164 def _import_plugins_from_dict(self, plugins_to_import, import_name=None, optional=False): | 168 def _import_plugins_from_dict(self, plugins_to_import, import_name=None, optional=False): |
165 """Recursively import and their dependencies in the right order | 169 """Recursively import and their dependencies in the right order |
166 @param plugins_to_import: dict where key=import_name and values= (plugin_path, module, plugin_info) | 170 @param plugins_to_import: dict where key=import_name and values= (plugin_path, module, plugin_info) |