Skip to content

Commit

Permalink
Config object can now be used as a dict
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarsa committed May 2, 2013
1 parent 181b5dc commit 955d588
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
8 changes: 8 additions & 0 deletions derpconf/config.py
Expand Up @@ -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 = []
Expand Down
2 changes: 1 addition & 1 deletion derpconf/version.py
Expand Up @@ -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"
18 changes: 18 additions & 0 deletions vows/config_vows.py
Expand Up @@ -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.