Mercurial > piecrust2
comparison piecrust/plugins/base.py @ 413:eacf0a3afd0c
internal: Register performance timers for plugin components.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 20 Jun 2015 19:16:38 -0700 |
parents | 10bb8e8600f5 |
children | e2e955a3bb25 |
comparison
equal
deleted
inserted
replaced
412:a1567766c83c | 413:eacf0a3afd0c |
---|---|
48 self._ensureLoaded() | 48 self._ensureLoaded() |
49 return self._plugins | 49 return self._plugins |
50 | 50 |
51 def getFormatters(self): | 51 def getFormatters(self): |
52 return self._getPluginComponents( | 52 return self._getPluginComponents( |
53 'getFormatters', True, order_key=lambda f: f.priority) | 53 'getFormatters', |
54 initialize=True, register_timer=True, | |
55 order_key=lambda f: f.priority) | |
54 | 56 |
55 def getTemplateEngines(self): | 57 def getTemplateEngines(self): |
56 return self._getPluginComponents('getTemplateEngines', True) | 58 return self._getPluginComponents( |
59 'getTemplateEngines', | |
60 initialize=True, register_timer=True) | |
57 | 61 |
58 def getDataProviders(self): | 62 def getDataProviders(self): |
59 return self._getPluginComponents('getDataProviders') | 63 return self._getPluginComponents('getDataProviders') |
60 | 64 |
61 def getProcessors(self): | 65 def getProcessors(self): |
62 return self._getPluginComponents( | 66 return self._getPluginComponents( |
63 'getProcessors', True, order_key=lambda p: p.priority) | 67 'getProcessors', |
68 initialize=True, register_timer=True, | |
69 order_key=lambda p: p.priority) | |
64 | 70 |
65 def getImporters(self): | 71 def getImporters(self): |
66 return self._getPluginComponents('getImporters') | 72 return self._getPluginComponents('getImporters') |
67 | 73 |
68 def getCommands(self): | 74 def getCommands(self): |
84 from piecrust.plugins.builtin import BuiltInPlugin | 90 from piecrust.plugins.builtin import BuiltInPlugin |
85 self._plugins = [BuiltInPlugin()] | 91 self._plugins = [BuiltInPlugin()] |
86 | 92 |
87 to_install = self.app.config.get('site/plugins') | 93 to_install = self.app.config.get('site/plugins') |
88 if to_install: | 94 if to_install: |
89 for p in to_install: | 95 for name in to_install: |
90 self._loadPlugin(p) | 96 plugin = self._loadPlugin(name) |
97 self._plugins.append(plugin) | |
91 | 98 |
92 for plugin in self._plugins: | 99 for plugin in self._plugins: |
93 plugin.initialize(self.app) | 100 plugin.initialize(self.app) |
94 | 101 |
95 def _loadPlugin(self, plugin_name): | 102 def _loadPlugin(self, plugin_name): |
111 except Exception as ex: | 118 except Exception as ex: |
112 logger.error("Failed to create plugin '%s': %s" % | 119 logger.error("Failed to create plugin '%s': %s" % |
113 (plugin_name, ex)) | 120 (plugin_name, ex)) |
114 return | 121 return |
115 | 122 |
116 self._plugins.append(plugin) | 123 return plugin |
117 | 124 |
118 def _getPluginComponents(self, name, initialize=False, order_key=None): | 125 def _getPluginComponents(self, name, *, |
126 initialize=False, register_timer=False, | |
127 order_key=None): | |
119 if name in self._componentCache: | 128 if name in self._componentCache: |
120 return self._componentCache[name] | 129 return self._componentCache[name] |
121 | 130 |
122 all_components = [] | 131 all_components = [] |
123 for plugin in self.plugins: | 132 for plugin in self.plugins: |
124 plugin_components = getattr(plugin, name)() | 133 plugin_components = getattr(plugin, name)() |
125 all_components += plugin_components | 134 all_components += plugin_components |
135 | |
126 if initialize: | 136 if initialize: |
127 for comp in plugin_components: | 137 for comp in plugin_components: |
128 comp.initialize(self.app) | 138 comp.initialize(self.app) |
139 | |
140 if register_timer: | |
141 for comp in plugin_components: | |
142 self.app.env.registerTimer(comp.__class__.__name__) | |
129 | 143 |
130 if order_key is not None: | 144 if order_key is not None: |
131 all_components.sort(key=order_key) | 145 all_components.sort(key=order_key) |
132 | 146 |
133 self._componentCache[name] = all_components | 147 self._componentCache[name] = all_components |