Permalink
Browse files

Config object can now be used as a dict

  • Loading branch information...
cezarsa committed May 2, 2013
1 parent 181b5dc commit 955d588683c3bb597f1f0b0a001e44a504bf343d
Showing with 27 additions and 1 deletion.
  1. +8 −0 derpconf/config.py
  2. +1 −1 derpconf/version.py
  3. +18 −0 vows/config_vows.py
View
@@ -147,6 +147,14 @@ def __getattr__(self, name):
raise AttributeError(name)
+ def __getitem__(self, name):
+ if hasattr(self, name):
+ return getattr(self, name)
+ raise KeyError('No config called \'%s\'' % name)
+
+ def __setitem__(self, name, value):
+ setattr(self, name, value)
+
@classmethod
def get_config_text(cls):
result = []
View
@@ -8,4 +8,4 @@
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2012 globo.com timehome@corp.globo.com
-__version__ = "0.4.7"
+__version__ = "0.4.8"
View
@@ -118,3 +118,21 @@ def topic(self):
def should_be_lengthy(self, topic):
expect(topic).to_length(1)
+
+ class WhenUsedAsDict(Vows.Context):
+ def topic(self):
+ return Config.load(fix('sample.conf'))
+
+ def should_have_get_value_as_dict(self, topic):
+ expect(topic['FOO']).to_equal('bar')
+
+ def should_have_set_value_as_dict(self, topic):
+ topic['X'] = 'something'
+ expect(topic['X']).to_equal('something')
+
+ class WithError(Vows.Context):
+ def topic(self, parent_topic):
+ return parent_topic['INVALID_KEY']
+
+ def should_raise_key_error(self, topic):
+ expect(topic).to_be_an_error_like(KeyError)

0 comments on commit 955d588

Please sign in to comment.