Mercurial > piecrust2
annotate piecrust/plugins/base.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 | 5dbab01daaba |
| children | 7122976bc751 |
| rev | line source |
|---|---|
|
303
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
1 import logging |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
2 import importlib |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
3 |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
4 |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
5 logger = logging.getLogger(__name__) |
| 0 | 6 |
| 7 | |
| 8 class PieCrustPlugin(object): | |
| 9 def getFormatters(self): | |
| 10 return [] | |
| 11 | |
| 12 def getTemplateEngines(self): | |
| 13 return [] | |
| 14 | |
| 15 def getDataProviders(self): | |
| 16 return [] | |
| 17 | |
| 18 def getFileSystems(self): | |
| 19 return [] | |
| 20 | |
| 21 def getProcessors(self): | |
| 22 return [] | |
| 23 | |
| 24 def getImporters(self): | |
| 25 return [] | |
| 26 | |
| 27 def getCommands(self): | |
| 28 return [] | |
| 29 | |
|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
30 def getCommandExtensions(self): |
|
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
31 return [] |
|
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
32 |
| 0 | 33 def getRepositories(self): |
| 34 return [] | |
| 35 | |
| 36 def getBakerAssistants(self): | |
| 37 return [] | |
| 38 | |
|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
39 def getSources(self): |
|
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
40 return [] |
|
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
41 |
| 0 | 42 def initialize(self, app): |
| 43 pass | |
| 44 | |
| 45 | |
| 46 class PluginLoader(object): | |
| 47 def __init__(self, app): | |
| 48 self.app = app | |
| 49 self._plugins = None | |
| 50 self._componentCache = {} | |
| 51 | |
| 52 @property | |
| 53 def plugins(self): | |
| 54 self._ensureLoaded() | |
| 55 return self._plugins | |
| 56 | |
| 57 def getFormatters(self): | |
| 58 return self._getPluginComponents('getFormatters', True, | |
| 59 order_key=lambda f: f.priority) | |
| 60 | |
| 61 def getTemplateEngines(self): | |
| 62 return self._getPluginComponents('getTemplateEngines', True) | |
| 63 | |
| 64 def getDataProviders(self): | |
| 65 return self._getPluginComponents('getDataProviders') | |
| 66 | |
| 67 def getFileSystems(self): | |
| 68 return self._getPluginComponents('getFileSystems') | |
| 69 | |
| 70 def getProcessors(self): | |
| 71 return self._getPluginComponents('getProcessors', True, | |
| 72 order_key=lambda p: p.priority) | |
| 73 | |
| 74 def getImporters(self): | |
| 75 return self._getPluginComponents('getImporters') | |
| 76 | |
| 77 def getCommands(self): | |
| 78 return self._getPluginComponents('getCommands') | |
| 79 | |
|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
80 def getCommandExtensions(self): |
|
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
81 return self._getPluginComponents('getCommandExtensions') |
|
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
82 |
| 0 | 83 def getRepositories(self): |
| 84 return self._getPluginComponents('getRepositories', True) | |
| 85 | |
| 86 def getBakerAssistants(self): | |
| 87 return self._getPluginComponents('getBakerAssistants') | |
| 88 | |
|
3
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
89 def getSources(self): |
|
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
90 return self._getPluginComponents('getSources') |
|
f485ba500df3
Gigantic change to basically make PieCrust 2 vaguely functional.
Ludovic Chabant <ludovic@chabant.com>
parents:
1
diff
changeset
|
91 |
| 0 | 92 def _ensureLoaded(self): |
| 93 if self._plugins is not None: | |
| 94 return | |
| 95 | |
|
1
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
96 from piecrust.plugins.builtin import BuiltInPlugin |
| 0 | 97 self._plugins = [BuiltInPlugin()] |
| 98 | |
|
303
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
99 for p in self.app.config.get('site/plugins'): |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
100 self._loadPlugin(p) |
| 0 | 101 |
| 102 for plugin in self._plugins: | |
| 103 plugin.initialize(self.app) | |
| 104 | |
|
303
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
105 def _loadPlugin(self, plugin_name): |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
106 try: |
|
305
9ae23409d6e9
plugins: Change how plugins are loaded. Add a `plugins` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
303
diff
changeset
|
107 mod = importlib.import_module('piecrust_' + plugin_name) |
|
9ae23409d6e9
plugins: Change how plugins are loaded. Add a `plugins` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
303
diff
changeset
|
108 except ImportError as ex: |
|
9ae23409d6e9
plugins: Change how plugins are loaded. Add a `plugins` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
303
diff
changeset
|
109 logger.error("Failed to load plugin '%s'." % plugin_name) |
|
9ae23409d6e9
plugins: Change how plugins are loaded. Add a `plugins` command.
Ludovic Chabant <ludovic@chabant.com>
parents:
303
diff
changeset
|
110 logger.error(ex) |
|
303
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
111 return |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
112 |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
113 plugin_class = getattr(mod, '__piecrust_plugin__', None) |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
114 if plugin_class is None: |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
115 logger.error("Plugin '%s' doesn't specify any " |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
116 "`__piecrust_plugin__` class." % plugin_name) |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
117 return |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
118 |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
119 try: |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
120 plugin = plugin_class() |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
121 except Exception as ex: |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
122 logger.error("Failed to create plugin '%s': %s" % |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
123 (plugin_name, ex)) |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
124 return |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
125 |
|
5dbab01daaba
plugins: First pass for a working plugin loader functionality.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
126 self._plugins.append(plugin) |
| 0 | 127 |
|
7
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
128 def _getPluginComponents(self, name, initialize=False, order_key=None): |
| 0 | 129 if name in self._componentCache: |
| 130 return self._componentCache[name] | |
| 131 | |
| 132 all_components = [] | |
| 133 for plugin in self.plugins: | |
| 134 plugin_components = getattr(plugin, name)() | |
| 135 all_components += plugin_components | |
| 136 if initialize: | |
| 137 for comp in plugin_components: | |
| 138 comp.initialize(self.app) | |
| 139 | |
|
7
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
140 if order_key is not None: |
|
343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
Ludovic Chabant <ludovic@chabant.com>
parents:
3
diff
changeset
|
141 all_components.sort(key=order_key) |
| 0 | 142 |
| 143 self._componentCache[name] = all_components | |
| 144 return all_components | |
| 145 |
