Mercurial > piecrust2
comparison piecrust/plugins/base.py @ 999:46025a1b5434
plugins: Support multiple customizable plugins directories.
Also add support for specifying the theme directory in some customizable paths.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Tue, 21 Nov 2017 09:54:56 -0800 |
parents | 8f8bbb2e70e1 |
children | 2e5c5d33d62c |
comparison
equal
deleted
inserted
replaced
998:a49e6846e0da | 999:46025a1b5434 |
---|---|
115 | 115 |
116 to_install = self.app.config.get('site/plugins') | 116 to_install = self.app.config.get('site/plugins') |
117 if to_install: | 117 if to_install: |
118 for name in to_install: | 118 for name in to_install: |
119 plugin = self._loadPlugin(name) | 119 plugin = self._loadPlugin(name) |
120 self._plugins.append(plugin) | 120 if plugin is not None: |
121 self._plugins.append(plugin) | |
121 | 122 |
122 for plugin in self._plugins: | 123 for plugin in self._plugins: |
123 plugin.initialize(self.app) | 124 plugin.initialize(self.app) |
124 | 125 |
125 def _loadPlugin(self, plugin_name): | 126 def _loadPlugin(self, plugin_name): |
129 mod = importlib.import_module(mod_name) | 130 mod = importlib.import_module(mod_name) |
130 except ImportError as ex: | 131 except ImportError as ex: |
131 mod = None | 132 mod = None |
132 | 133 |
133 if mod is None: | 134 if mod is None: |
134 # Import as a loose Python file from the plugins dir. | 135 # Import as a loose Python file from the plugins dirs. |
135 pfile = os.path.join(self.app.plugins_dir, plugin_name + '.py') | 136 for plugins_dir in self.app.plugins_dirs: |
136 if os.path.isfile(pfile): | 137 pfile = os.path.join(plugins_dir, plugin_name + '.py') |
137 spec = importlib.util.spec_from_file_location(plugin_name, | 138 if os.path.isfile(pfile): |
138 pfile) | 139 spec = importlib.util.spec_from_file_location(plugin_name, |
139 mod = importlib.util.module_from_spec(spec) | 140 pfile) |
140 spec.loader.exec_module(mod) | 141 mod = importlib.util.module_from_spec(spec) |
141 sys.modules[mod_name] = mod | 142 spec.loader.exec_module(mod) |
143 sys.modules[mod_name] = mod | |
144 break | |
142 | 145 |
143 if mod is None: | 146 if mod is None: |
144 logger.error("Failed to load plugin '%s'." % plugin_name) | 147 logger.error("Failed to load plugin '%s'." % plugin_name) |
145 logger.error(ex) | 148 logger.error("Looking in: %s" % self.app.plugins_dirs) |
146 return | 149 return |
147 | 150 |
148 plugin_class = getattr(mod, '__piecrust_plugin__', None) | 151 plugin_class = getattr(mod, '__piecrust_plugin__', None) |
149 if plugin_class is None: | 152 if plugin_class is None: |
150 logger.error("Plugin '%s' doesn't specify any " | 153 logger.error("Plugin '%s' doesn't specify any " |