Both getdict and getlist return copies of the requested values

This commit is contained in:
Julia Medina 2014-08-01 00:42:25 -03:00
parent 3ae971468f
commit 39c6a80f9d
2 changed files with 20 additions and 15 deletions

View File

@ -252,8 +252,8 @@ Settings API
.. method:: getlist(name, default=None)
Get a setting value as a list. If the setting original type is a list it
will be returned verbatim. If it's a string it will be split by ",".
Get a setting value as a list. If the setting original type is a list, a
copy of it will be returned. If it's a string it will be split by ",".
For example, settings populated through environment variables set to
``'one,two'`` will return a list ['one', 'two'] when using this method.
@ -264,6 +264,18 @@ Settings API
:param default: the value to return if no setting is found
:type default: any
.. method:: getdict(name, default=None)
Get a setting value as a dictionary. If the setting original type is a
dictionary, a copy of it will be returned. If it's a string it will
evaluated as a json dictionary.
:param name: the setting name
:type name: string
:param default: the value to return if no setting is found
:type default: any
.. method:: copy()
Make a deep copy of current settings.

View File

@ -76,23 +76,16 @@ class Settings(object):
return float(self.get(name, default))
def getlist(self, name, default=None):
value = self.get(name)
if value is None:
return default or []
elif hasattr(value, '__iter__'):
return value
else:
return str(value).split(',')
value = self.get(name, default or [])
if isinstance(value, six.string_types):
value = value.split(',')
return list(value)
def getdict(self, name, default=None):
value = self.get(name)
if value is None:
return default or {}
value = self.get(name, default or {})
if isinstance(value, six.string_types):
value = json.loads(value)
if isinstance(value, dict):
return value
raise ValueError("Cannot convert value for setting '%s' to dict: '%s'" % (name, value))
return dict(value)
def set(self, name, value, priority='project'):
assert not self.frozen, "Trying to modify an immutable Settings object"