Mercurial > piecrust2
annotate piecrust/plugins/base.py @ 1:aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Added `init` command.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 22 Dec 2013 08:00:24 -0800 |
parents | piecrust/plugins.py@a212a3f2e3ee |
children | f485ba500df3 |
rev | line source |
---|---|
0 | 1 import os |
2 | |
3 | |
4 class PieCrustPlugin(object): | |
5 def getFormatters(self): | |
6 return [] | |
7 | |
8 def getTemplateEngines(self): | |
9 return [] | |
10 | |
11 def getDataProviders(self): | |
12 return [] | |
13 | |
14 def getFileSystems(self): | |
15 return [] | |
16 | |
17 def getProcessors(self): | |
18 return [] | |
19 | |
20 def getImporters(self): | |
21 return [] | |
22 | |
23 def getCommands(self): | |
24 return [] | |
25 | |
26 def getRepositories(self): | |
27 return [] | |
28 | |
29 def getBakerAssistants(self): | |
30 return [] | |
31 | |
32 def initialize(self, app): | |
33 pass | |
34 | |
35 | |
36 class PluginLoader(object): | |
37 def __init__(self, app): | |
38 self.app = app | |
39 self._plugins = None | |
40 self._pluginsMeta = None | |
41 self._componentCache = {} | |
42 | |
43 @property | |
44 def plugins(self): | |
45 self._ensureLoaded() | |
46 return self._plugins | |
47 | |
48 def getFormatters(self): | |
49 return self._getPluginComponents('getFormatters', True, | |
50 order_key=lambda f: f.priority) | |
51 | |
52 def getTemplateEngines(self): | |
53 return self._getPluginComponents('getTemplateEngines', True) | |
54 | |
55 def getDataProviders(self): | |
56 return self._getPluginComponents('getDataProviders') | |
57 | |
58 def getFileSystems(self): | |
59 return self._getPluginComponents('getFileSystems') | |
60 | |
61 def getProcessors(self): | |
62 return self._getPluginComponents('getProcessors', True, | |
63 order_key=lambda p: p.priority) | |
64 | |
65 def getImporters(self): | |
66 return self._getPluginComponents('getImporters') | |
67 | |
68 def getCommands(self): | |
69 return self._getPluginComponents('getCommands') | |
70 | |
71 def getRepositories(self): | |
72 return self._getPluginComponents('getRepositories', True) | |
73 | |
74 def getBakerAssistants(self): | |
75 return self._getPluginComponents('getBakerAssistants') | |
76 | |
77 def _ensureLoaded(self): | |
78 if self._plugins is not None: | |
79 return | |
80 | |
1
aaa8fb7c8918
Re-arranged modules to reduce dependencies to builtin stuff.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
81 from piecrust.plugins.builtin import BuiltInPlugin |
0 | 82 self._plugins = [BuiltInPlugin()] |
83 self._pluginsMeta = {self._plugins[0].name: False} | |
84 | |
85 for d in self.app.plugins_dirs: | |
86 _, dirs, __ = next(os.walk(d)) | |
87 for dd in dirs: | |
88 self._loadPlugin(os.path.join(d, dd)) | |
89 | |
90 for plugin in self._plugins: | |
91 plugin.initialize(self.app) | |
92 | |
93 def _loadPlugin(self, plugin_dir): | |
94 pass | |
95 | |
96 def _getPluginComponents(self, name, initialize=False, order_cmp=None, order_key=None): | |
97 if name in self._componentCache: | |
98 return self._componentCache[name] | |
99 | |
100 all_components = [] | |
101 for plugin in self.plugins: | |
102 plugin_components = getattr(plugin, name)() | |
103 all_components += plugin_components | |
104 if initialize: | |
105 for comp in plugin_components: | |
106 comp.initialize(self.app) | |
107 | |
108 if order_cmp is not None or order_key is not None: | |
109 all_components.sort(cmp=order_cmp, key=order_key) | |
110 | |
111 self._componentCache[name] = all_components | |
112 return all_components | |
113 |