forked from orbit-oss/flask
Add Config.from_mapping
This commit is contained in:
parent
02f0c755a3
commit
aa40b1731e
3 changed files with 51 additions and 0 deletions
|
|
@ -193,6 +193,28 @@ class Config(dict):
|
|||
self[key] = obj[key]
|
||||
return True
|
||||
|
||||
def from_mapping(self, *mapping, **kwargs):
|
||||
"""Updates the config like :meth:`update` ignoring items with non-upper
|
||||
keys.
|
||||
|
||||
.. versionadded:: 1.0
|
||||
"""
|
||||
mappings = []
|
||||
if len(mapping) == 1:
|
||||
if hasattr(mapping[0], 'items'):
|
||||
mappings.append(mapping[0].items())
|
||||
else:
|
||||
mappings.append(mapping[0])
|
||||
elif len(mapping) > 1:
|
||||
raise TypeError(
|
||||
'expected at most 1 positional argument, got %d' % len(mapping)
|
||||
)
|
||||
mappings.append(kwargs.items())
|
||||
for mapping in mappings:
|
||||
for (key, value) in mapping:
|
||||
if key.isupper():
|
||||
self[key] = value
|
||||
|
||||
def get_namespace(self, namespace, lowercase=True):
|
||||
"""Returns a dictionary containing a subset of configuration options
|
||||
that match the specified namespace/prefix. Example usage::
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue