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