changeset 305:9ae23409d6e9

plugins: Change how plugins are loaded. Add a `plugins` command.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 22 Mar 2015 22:20:18 -0700
parents 34ef6a2a0c97
children 7122976bc751
files piecrust/commands/builtin/plugins.py piecrust/plugins/base.py piecrust/plugins/builtin.py
diffstat 3 files changed, 64 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/piecrust/commands/builtin/plugins.py	Sun Mar 22 22:20:18 2015 -0700
@@ -0,0 +1,57 @@
+import logging
+from piecrust.commands.base import ChefCommand
+
+
+logger = logging.getLogger(__name__)
+
+
+class PluginsCommand(ChefCommand):
+    def __init__(self):
+        super(PluginsCommand, self).__init__()
+        self.name = 'plugins'
+        self.description = "Manage the plugins for the current website."
+
+    def setupParser(self, parser, app):
+        # Don't setup anything if this is a null app
+        # (for when `chef` is run from outside a website)
+        if app.root_dir is None:
+            return
+
+        subparsers = parser.add_subparsers()
+        p = subparsers.add_parser(
+                'list',
+                help="Lists the plugins installed in the current website.")
+        p.add_argument(
+                '-a', '--all',
+                action='store_true',
+                help=("Also list all the available plugins for the "
+                      "current environment. The installed one will have an "
+                      "asterix (*)."))
+        p.set_defaults(sub_func=self._listPlugins)
+
+    def checkedRun(self, ctx):
+        if not hasattr(ctx.args, 'sub_func'):
+            ctx.parser.parse_args(['plugins', '--help'])
+            return
+        ctx.args.sub_func(ctx)
+
+    def _listPlugins(self, ctx):
+        names = {}
+        installed_suffix = ''
+        if ctx.args.all:
+            import pip
+            prefix = 'PieCrust-'
+            installed_packages = pip.get_installed_distributions()
+            for plugin in installed_packages:
+                if not plugin.project_name.startswith(prefix):
+                    continue
+                name = plugin.project_name[len(prefix):]
+                names[name] = False
+            installed_suffix = '*'
+
+        for plugin in ctx.app.plugin_loader.plugins:
+            names[plugin.name] = True
+
+        for name, inst in names.items():
+            logger.info("%s%s" % (name, installed_suffix if inst else ''))
+
--- a/piecrust/plugins/base.py	Thu Mar 19 22:05:30 2015 -0700
+++ b/piecrust/plugins/base.py	Sun Mar 22 22:20:18 2015 -0700
@@ -47,7 +47,6 @@
     def __init__(self, app):
         self.app = app
         self._plugins = None
-        self._pluginsMeta = None
         self._componentCache = {}
 
     @property
@@ -96,7 +95,6 @@
 
         from piecrust.plugins.builtin import BuiltInPlugin
         self._plugins = [BuiltInPlugin()]
-        self._pluginsMeta = {self._plugins[0].name: False}
 
         for p in self.app.config.get('site/plugins'):
             self._loadPlugin(p)
@@ -106,9 +104,10 @@
 
     def _loadPlugin(self, plugin_name):
         try:
-            mod = importlib.import_module(plugin_name)
-        except ImportError:
-            logger.error("Failed to load plugin '%s'.")
+            mod = importlib.import_module('piecrust_' + plugin_name)
+        except ImportError as ex:
+            logger.error("Failed to load plugin '%s'." % plugin_name)
+            logger.error(ex)
             return
 
         plugin_class = getattr(mod, '__piecrust_plugin__', None)
--- a/piecrust/plugins/builtin.py	Thu Mar 19 22:05:30 2015 -0700
+++ b/piecrust/plugins/builtin.py	Sun Mar 22 22:20:18 2015 -0700
@@ -4,6 +4,7 @@
 from piecrust.commands.builtin.info import (
         RootCommand, ShowConfigCommand,
         FindCommand, ShowSourcesCommand, ShowRoutesCommand, ShowPathsCommand)
+from piecrust.commands.builtin.plugins import PluginsCommand
 from piecrust.commands.builtin.scaffolding import (
         PrepareCommand,
         DefaultPrepareTemplatesCommandExtension,
@@ -41,9 +42,7 @@
 
 
 class BuiltInPlugin(PieCrustPlugin):
-    def __init__(self):
-        super(BuiltInPlugin, self).__init__()
-        self.name = '__builtin__'
+    name = '__builtin__'
 
     def getCommands(self):
         return [
@@ -59,6 +58,7 @@
                 ShowRoutesCommand(),
                 ShowPathsCommand(),
                 ThemesCommand(),
+                PluginsCommand(),
                 BakeCommand(),
                 ShowRecordCommand(),
                 ServeCommand()]