Add Config.from_mapping
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Daniel Neuhäuser · committed by Daniel Neuhäuser · parent 02f0c75 · Browse files · 3 files·+51 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+51 added−0 removed
- other:+51 added−0 removed
Provenance
- Commit
aa40b17- Tree
52f5f1d- Author
Daniel Neuhäuser <dasdasich@gmail.com>- Committer
Daniel Neuhäuser <dasdasich@gmail.com>- Parent
02f0c75
3 files changed+51 added−0 removed
modified:flask/testsuite/config.py+28 added
45 unmodified lines46 current_dir = os.path.dirname(os.path.abspath(__file__))47 app.config.from_json(os.path.join(current_dir, 'static', 'config.json'))48 self.common_object_test(app)49 50+ def test_config_from_mapping(self):51+ app = flask.Flask(__name__)52+ app.config.from_mapping({53+ 'SECRET_KEY': 'devkey',54+ 'TEST_KEY': 'foo'55+ })56+ self.common_object_test(app)57+58+ app = flask.Flask(__name__)59+ app.config.from_mapping([60+ ('SECRET_KEY', 'devkey'),61+ ('TEST_KEY', 'foo')62+ ])63+ self.common_object_test(app)64+65+ app = flask.Flask(__name__)66+ app.config.from_mapping(67+ SECRET_KEY='devkey',68+ TEST_KEY='foo'69+ )70+ self.common_object_test(app)71+72+ app = flask.Flask(__name__)73+ with self.assert_raises(TypeError):74+ app.config.from_mapping(75+ {}, {}76+ )77+78 def test_config_from_class(self):79 class Base(object):80 TEST_KEY = 'foo'81 class Test(Base):303 unmodified linesmodified:flask/config.py+22 added
191 unmodified lines192 if key.isupper():193 self[key] = obj[key]194 return True195 196+ def from_mapping(self, *mapping, **kwargs):197+ """Updates the config like :meth:`update` ignoring items with non-upper198+ keys.199+200+ .. versionadded:: 1.0201+ """202+ mappings = []203+ if len(mapping) == 1:204+ if hasattr(mapping[0], 'items'):205+ mappings.append(mapping[0].items())206+ else:207+ mappings.append(mapping[0])208+ elif len(mapping) > 1:209+ raise TypeError(210+ 'expected at most 1 positional argument, got %d' % len(mapping)211+ )212+ mappings.append(kwargs.items())213+ for mapping in mappings:214+ for (key, value) in mapping:215+ if key.isupper():216+ self[key] = value217+218 def get_namespace(self, namespace, lowercase=True):219 """Returns a dictionary containing a subset of configuration options220 that match the specified namespace/prefix. Example usage::221 34 unmodified linesmodified:CHANGES+1 added
34 unmodified lines35 exceptions (in ``werkzeug.execptions``). This makes it possible36 for an extension author to create exceptions that will by default37 result in the HTTP error of their choosing, but may be caught with38 a custom error handler if desired.39+- Added :meth:`flask.Config.from_mapping`.40 41 Version 0.10.242 --------------43 478 unmodified lines