summaryrefslogtreecommitdiff
path: root/sleekxmpp/plugins/base.py
diff options
context:
space:
mode:
authorLance Stout <lancestout@gmail.com>2012-07-26 23:04:16 -0700
committerLance Stout <lancestout@gmail.com>2012-07-26 23:04:16 -0700
commita06fa2de677afad437622216fac7971b727ac569 (patch)
tree37e8774a4cf2515bd31685c801d873ce4ef87127 /sleekxmpp/plugins/base.py
parent35396d2977f9b19f6690b88a7c2f3c7f4f09356b (diff)
downloadslixmpp-a06fa2de677afad437622216fac7971b727ac569.tar.gz
slixmpp-a06fa2de677afad437622216fac7971b727ac569.tar.bz2
slixmpp-a06fa2de677afad437622216fac7971b727ac569.tar.xz
slixmpp-a06fa2de677afad437622216fac7971b727ac569.zip
Enhance plugin config with attribute accessors.
This makes updating the config after plugin initialization much easier.
Diffstat (limited to 'sleekxmpp/plugins/base.py')
-rw-r--r--sleekxmpp/plugins/base.py36
1 files changed, 35 insertions, 1 deletions
diff --git a/sleekxmpp/plugins/base.py b/sleekxmpp/plugins/base.py
index 26f0c827..67675908 100644
--- a/sleekxmpp/plugins/base.py
+++ b/sleekxmpp/plugins/base.py
@@ -14,6 +14,7 @@
"""
import sys
+import copy
import logging
import threading
@@ -272,6 +273,14 @@ class BasePlugin(object):
#: be initialized as needed if this plugin is enabled.
dependencies = set()
+ #: The basic, standard configuration for the plugin, which may
+ #: be overridden when initializing the plugin. The configuration
+ #: fields included here may be accessed directly as attributes of
+ #: the plugin. For example, including the configuration field 'foo'
+ #: would mean accessing `plugin.foo` returns the current value of
+ #: `plugin.config['foo']`.
+ default_config = {}
+
def __init__(self, xmpp, config=None):
self.xmpp = xmpp
if self.xmpp:
@@ -279,7 +288,32 @@ class BasePlugin(object):
#: A plugin's behaviour may be configurable, in which case those
#: configuration settings will be provided as a dictionary.
- self.config = config if config is not None else {}
+ self.config = copy.copy(self.default_config)
+ if config:
+ self.config.update(config)
+
+ def __getattr__(self, key):
+ """Provide direct access to configuration fields.
+
+ If the standard configuration includes the option `'foo'`, then
+ accessing `self.foo` should be the same as `self.config['foo']`.
+ """
+ if key in self.default_config:
+ return self.config.get(key, None)
+ else:
+ return object.__getattribute__(self, key)
+
+ def __setattr__(self, key, value):
+ """Provide direct assignment to configuration fields.
+
+ If the standard configuration includes the option `'foo'`, then
+ assigning to `self.foo` should be the same as assigning to
+ `self.config['foo']`.
+ """
+ if key in self.default_config:
+ self.config[key] = value
+ else:
+ super(BasePlugin, self).__setattr__(key, value)
def _init(self):
"""Initialize plugin state, such as registering event handlers.