From c2924402ad4c4531fe3c79f39dacf331daa7d85a Mon Sep 17 00:00:00 2001 From: Deniz Turgut Date: Fri, 5 Apr 2013 02:32:58 -0400 Subject: [PATCH] Fix for issue #428: Use PLUGIN_PATH properly --- docs/plugins.rst | 3 ++- pelican/__init__.py | 20 +++++++++++++++----- pelican/settings.py | 5 +++-- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/docs/plugins.rst b/docs/plugins.rst index 9985f25e..857180f0 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -24,7 +24,8 @@ Alternatively, another method is to import them and add them to the list:: PLUGINS = [gravatar,] If your plugins are not in an importable path, you can specify a ``PLUGIN_PATH`` -in the settings:: +in the settings. ``PLUGIN_PATH`` can be an absolute path or a path relative to +the settings file:: PLUGIN_PATH = "plugins" PLUGINS = ["list", "of", "plugins"] diff --git a/pelican/__init__.py b/pelican/__init__.py index a2bdefa4..7072f484 100644 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -57,15 +57,25 @@ class Pelican(object): sys.path.insert(0, '') def init_plugins(self): - self.plugins = self.settings['PLUGINS'] - for plugin in self.plugins: + self.plugins = [] + logger.debug('Temporarily adding PLUGIN_PATH to system path') + _sys_path = sys.path[:] + sys.path.insert(0, self.settings['PLUGIN_PATH']) + for plugin in self.settings['PLUGINS']: # if it's a string, then import it if isinstance(plugin, six.string_types): - logger.debug("Loading plugin `{0}' ...".format(plugin)) - plugin = __import__(plugin, globals(), locals(), 'module') + logger.debug("Loading plugin `{0}`".format(plugin)) + try: + plugin = __import__(plugin, globals(), locals(), 'module') + except ImportError as e: + logger.error("Can't find plugin `{0}`: {1}".format(plugin, e)) + continue - logger.debug("Registering plugin `{0}'".format(plugin.__name__)) + logger.debug("Registering plugin `{0}`".format(plugin.__name__)) plugin.register() + self.plugins.append(plugin) + logger.debug('Restoring system path') + sys.path = _sys_path def _handle_deprecation(self): diff --git a/pelican/settings.py b/pelican/settings.py index 3ff83b6b..df1de0c7 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -89,6 +89,7 @@ _DEFAULT_CONFIG = {'PATH': os.curdir, 'ARTICLE_PERMALINK_STRUCTURE': '', 'TYPOGRIFY': False, 'SUMMARY_MAX_LENGTH': 50, + 'PLUGIN_PATH': '', 'PLUGINS': [], 'TEMPLATE_PAGES': {}, 'IGNORE_FILES': [] @@ -99,12 +100,12 @@ def read_settings(path=None, override=None): if path: local_settings = get_settings_from_file(path) # Make the paths relative to the settings file - for p in ['PATH', 'OUTPUT_PATH', 'THEME']: + for p in ['PATH', 'OUTPUT_PATH', 'THEME', 'PLUGIN_PATH']: if p in local_settings and local_settings[p] is not None \ and not isabs(local_settings[p]): absp = os.path.abspath(os.path.normpath(os.path.join( os.path.dirname(path), local_settings[p]))) - if p != 'THEME' or os.path.exists(absp): + if p not in ('THEME', 'PLUGIN_PATH') or os.path.exists(absp): local_settings[p] = absp else: local_settings = copy.deepcopy(_DEFAULT_CONFIG)