Mercurial > piecrust2
comparison piecrust/commands/builtin/plugins.py @ 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 | |
children | f7ddd730c08d |
comparison
equal
deleted
inserted
replaced
304:34ef6a2a0c97 | 305:9ae23409d6e9 |
---|---|
1 import logging | |
2 from piecrust.commands.base import ChefCommand | |
3 | |
4 | |
5 logger = logging.getLogger(__name__) | |
6 | |
7 | |
8 class PluginsCommand(ChefCommand): | |
9 def __init__(self): | |
10 super(PluginsCommand, self).__init__() | |
11 self.name = 'plugins' | |
12 self.description = "Manage the plugins for the current website." | |
13 | |
14 def setupParser(self, parser, app): | |
15 # Don't setup anything if this is a null app | |
16 # (for when `chef` is run from outside a website) | |
17 if app.root_dir is None: | |
18 return | |
19 | |
20 subparsers = parser.add_subparsers() | |
21 p = subparsers.add_parser( | |
22 'list', | |
23 help="Lists the plugins installed in the current website.") | |
24 p.add_argument( | |
25 '-a', '--all', | |
26 action='store_true', | |
27 help=("Also list all the available plugins for the " | |
28 "current environment. The installed one will have an " | |
29 "asterix (*).")) | |
30 p.set_defaults(sub_func=self._listPlugins) | |
31 | |
32 def checkedRun(self, ctx): | |
33 if not hasattr(ctx.args, 'sub_func'): | |
34 ctx.parser.parse_args(['plugins', '--help']) | |
35 return | |
36 ctx.args.sub_func(ctx) | |
37 | |
38 def _listPlugins(self, ctx): | |
39 names = {} | |
40 installed_suffix = '' | |
41 if ctx.args.all: | |
42 import pip | |
43 prefix = 'PieCrust-' | |
44 installed_packages = pip.get_installed_distributions() | |
45 for plugin in installed_packages: | |
46 if not plugin.project_name.startswith(prefix): | |
47 continue | |
48 name = plugin.project_name[len(prefix):] | |
49 names[name] = False | |
50 installed_suffix = '*' | |
51 | |
52 for plugin in ctx.app.plugin_loader.plugins: | |
53 names[plugin.name] = True | |
54 | |
55 for name, inst in names.items(): | |
56 logger.info("%s%s" % (name, installed_suffix if inst else '')) | |
57 |