[mousetrap/gnome3-wip: 129/240] Simplify access to class-based configuration.
- From: Heidi Ellis <heidiellis src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [mousetrap/gnome3-wip: 129/240] Simplify access to class-based configuration.
- Date: Mon, 8 Sep 2014 15:23:24 +0000 (UTC)
commit 703ba6546e30f8024c61c9278f773499a813b394
Author: Stoney Jackson <dr stoney gmail com>
Date: Mon Jun 23 15:21:15 2014 -0400
Simplify access to class-based configuration.
src/mousetrap/config.py | 19 +++++++++++++++----
src/mousetrap/plugins/display.py | 2 +-
2 files changed, 16 insertions(+), 5 deletions(-)
---
diff --git a/src/mousetrap/config.py b/src/mousetrap/config.py
index 7a23f9a..9d4c026 100644
--- a/src/mousetrap/config.py
+++ b/src/mousetrap/config.py
@@ -46,13 +46,24 @@ class Config(dict):
}
}
- self['plugins'] = {
+ self['classes'] = {
'mousetrap.plugins.display.DisplayPlugin': {
'window_title': 'MouseTrap',
}
}
+ def __getitem__(self, key):
+ '''
+ Allow access to class configuration by passing instance of class as
+ the key. For example,
- def for_plugin(self, plugin_object):
- class_ = plugin_object.__class__
- return self['plugins'][class_.__module__ + '.' + class_.__name__]
+ x = config[self]['x']
+
+ is equivelant to
+
+ x = config['classes'][self.__class__.__module__+'.'+self.__class__.__name__]['x']
+ '''
+ if isinstance(key, basestring):
+ return super(Config, self).__getitem__(key)
+ class_ = key.__class__
+ return self['classes'][class_.__module__ + '.' + class_.__name__]
diff --git a/src/mousetrap/plugins/display.py b/src/mousetrap/plugins/display.py
index e747a7f..16f90bc 100644
--- a/src/mousetrap/plugins/display.py
+++ b/src/mousetrap/plugins/display.py
@@ -5,7 +5,7 @@ import logging
class DisplayPlugin(interface.Plugin):
def __init__(self, config):
self._config = config
- self._window_title = config.for_plugin(self)['window_title']
+ self._window_title = config[self]['window_title']
def run(self, app):
app.gui.show_image(self._window_title, app.image)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]