Permalink
Browse files

reworked plugins system so that functions have a 'metlog_name'

attribute that will be used for the method name
  • Loading branch information...
crankycoder committed Jun 21, 2012
1 parent 62f435e commit 25256a6ca77238623fa8425c4c913a767b9f945e
Showing with 57 additions and 7 deletions.
  1. +5 −2 metlog/client.py
  2. +2 −2 metlog/config.py
  3. +6 −1 metlog/tests/plugin.py
  4. +41 −0 metlog/tests/plugin_exception.py
  5. +3 −2 metlog/tests/test_config.py
View
@@ -185,16 +185,19 @@ def send_message(self, msg):
return
self.sender.send_message(msg)
- def add_method(self, name, method, override=False):
+ def add_method(self, method, override=False):
"""
Add a custom method to the MetlogClient instance.
- :param name: Name to use for the method.
:param method: Callable that will be used as the method.
:param override: Set this to True if you really want to
override an existing method.
"""
assert isinstance(method, types.FunctionType)
+
+ # Obtain the metlog name directly from the method
+ name = method.metlog_name
+
if not override and hasattr(self, name):
msg = "The name [%s] is already in use" % name
raise SyntaxError(msg)
View
@@ -187,12 +187,12 @@ class (required)
client.setup(sender, logger, severity, disabled_timers, filters)
# initialize plugins and attach to client
- for plugin_name, plugin_spec in plugins_data.items():
+ for ignored_plugin_name, plugin_spec in plugins_data.items():
# each plugin spec is a 2-tuple: (dotted_name, cfg)
plugin_config = plugin_spec[1]
plugin_override = plugin_config.pop('override', False)
plugin_fn = resolver.resolve(plugin_spec[0])(plugin_config)
- client.add_method(plugin_name, plugin_fn, plugin_override)
+ client.add_method(plugin_fn, plugin_override)
return client
View
@@ -8,12 +8,14 @@
"""
This is a sample plugin for the metlog clien
-You can find an entrypoint to it in the metlog-py setup.py file in the
+You can find an entrypoint to it in the metlog-py setup.py file in the
entry_points section.
Each plugin is bound into the metlog client if and only if there is
configuration for the plugin.
"""
+METLOG_PLUGIN_NAME = 'dummy'
+
def config_plugin(config_dict):
# Normally, the config_dict is unwrapped to get
# some variables that can be used in the closure
@@ -33,4 +35,7 @@ def my_plugin(self, *args, **kwargs):
# Just skip early if any verbose flag has been set to False
return
return mycopy
+
+ my_plugin.metlog_name = METLOG_PLUGIN_NAME
+
return my_plugin
@@ -0,0 +1,41 @@
+# ***** BEGIN LICENSE BLOCK *****
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file,
+# You can obtain one at http://mozilla.org/MPL/2.0/.
+# ***** END LICENSE BLOCK *****
+
+"""
+This is a sample plugin for the metlog clien
+
+You can find an entrypoint to it in the metlog-py setup.py file in the
+entry_points section.
+
+Each plugin is bound into the metlog client if and only if there is
+configuration for the plugin.
+"""
+METLOG_PLUGIN_NAME = 'exception'
+
+def config_plugin(config_dict):
+ # Normally, the config_dict is unwrapped to get
+ # some variables that can be used in the closure
+ # to disable features
+ default_verbose = config_dict.pop('verbose', False)
+
+ import copy
+ mycopy = copy.deepcopy(config_dict)
+
+ def my_plugin(self, *args, **kwargs):
+ # Most real plugin methods will use the variables captured in the
+ # config_dict to override arguments passed in by the developer through
+ # *args and **kwargs. This allows operations to disable specific
+ # features of metlog without having to deploy new code.
+
+ if not (default_verbose and kwargs.get('verbose', False)):
+ # Just skip early if any verbose flag has been set to False
+ return
+ return mycopy
+
+ my_plugin.metlog_name = METLOG_PLUGIN_NAME
+
+ return my_plugin
@@ -231,18 +231,19 @@ def test_plugin_override():
cfg_txt = """
[metlog]
sender_class = metlog.senders.DebugCaptureSender
+
[metlog_plugin_exception]
override=True
provider=metlog.tests.plugin:config_plugin
"""
client = client_from_text_config(cfg_txt, 'metlog')
- eq_('my_plugin', client.exception.__name__)
+ eq_('dummy', client.dummy.metlog_name)
cfg_txt = """
[metlog]
sender_class = metlog.senders.DebugCaptureSender
[metlog_plugin_exception]
- provider=metlog.tests.plugin:config_plugin
+ provider=metlog.tests.plugin_exception:config_plugin
"""
# Failure to set an override argument will throw an exception
assert_raises(SyntaxError, client_from_text_config, cfg_txt, 'metlog')

0 comments on commit 25256a6

Please sign in to comment.