Skip to content
Myr

Use uppercase for config and support any object.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 4d16486 · Browse files · 2 files+40 added−31 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+40 added−31 removed

  • tests:+14 added−5 removed
  • other:+26 added−26 removed

2 files+40 added−31 removed

modified:tests/flask_tests.py+14 added-5 removed
691 unmodified lines
692693694class ConfigTestCase(unittest.TestCase):695696 def common_module_test(self, app):696 def common_object_test(self, app):697 assert app.secret_key == 'devkey'698 assert app.config['test_key'] == 'foo'698 assert app.config['TEST_KEY'] == 'foo'699 assert 'ConfigTestCase' not in app.config700701 def test_config_from_file(self):702 app = flask.Flask(__name__)703 app.config.from_pyfile('flask_tests.py')704 self.common_module_test(app)704 self.common_object_test(app)705706 def test_config_from_module(self):707 app = flask.Flask(__name__)708 app.config.from_module(__name__)709 self.common_module_test(app)708 app.config.from_object(__name__)709 self.common_object_test(app)710711 def test_config_from_class(self):712 class Base(object):713 TEST_KEY = 'foo'714 class Test(Base):715 SECRET_KEY = 'devkey'716 app = flask.Flask(__name__)717 app.config.from_object(Test)718 self.common_object_test(app)719720721def suite():722 from minitwit_tests import MiniTwitTestCase723 from flaskr_tests import FlaskrTestCase724 suite = unittest.TestSuite()
16 unmodified lines
modified:flask.py+26 added-26 removed
678 unmodified lines
679 filename = os.path.join(self.root_path, filename)680 d = type(sys)('config')681 d.__file__ = filename682 execfile(filename, d.__dict__)683 self.from_module(d)683 self.from_object(d)684685 def from_module(self, module):686 """Updates the values from the given module. A module can be of one685 def from_object(self, obj):686 """Updates the values from the given object. An object can be of one687 of the following two types:688689 - a string: in this case the module with that name will be imported690 - an actual module reference: that module is used directly689 - a string: in this case the object with that name will be imported690 - an actual object reference: that object is used directly691692 Objects are usually either modules or classes.693692 Just the uppercase variables in that module are stored in the config694 Just the uppercase variables in that object are stored in the config695 after lowercasing. Example usage::696695 app.config.from_module('yourapplication.default_config')697 app.config.from_object('yourapplication.default_config')698 from yourapplication import default_config697 app.config.from_module(default_config)699 app.config.from_object(default_config)700701 You should not use this function to load the actual configuration but702 rather configuration defaults. The actual config should be loaded701 with :meth;`from_pyfile` and ideally from a location not within the703 with :meth:`from_pyfile` and ideally from a location not within the704 package because the package might be installed system wide.705704 :param module: an import name or module706 :param obj: an import name or object707 """706 if isinstance(module, basestring):707 d = import_string(module).__dict__708 else:709 d = module.__dict__710 for key, value in d.iteritems():708 if isinstance(obj, basestring):709 obj = import_string(obj)710 for key in dir(obj):711 if key.isupper():712 self[key.lower()] = value712 self[key] = getattr(obj, key)713714 def __repr__(self):715 return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))716
34 unmodified lines
751 #: the debug flag. Set this to `True` to enable debugging of the752 #: application. In debug mode the debugger will kick in when an unhandled753 #: exception ocurrs and the integrated server will automatically reload754 #: the application if changes in the code are detected.755 debug = ConfigAttribute('debug')755 debug = ConfigAttribute('DEBUG')756757 #: if a secret key is set, cryptographic components can use this to758 #: sign cookies and other things. Set this to a complex random value759 #: when you want to use the secure cookie for instance.760 secret_key = ConfigAttribute('secret_key')760 secret_key = ConfigAttribute('SECRET_KEY')761762 #: The secure cookie uses this for the name of the session cookie763 session_cookie_name = ConfigAttribute('session_cookie_name')763 session_cookie_name = ConfigAttribute('SESSION_COOKIE_NAME')764765 #: A :class:`~datetime.timedelta` which is used to set the expiration766 #: date of a permanent session. The default is 31 days which makes a767 #: permanent session survive for roughly one month.768 permanent_session_lifetime = ConfigAttribute('permanent_session_lifetime')768 permanent_session_lifetime = ConfigAttribute('PERMANENT_SESSION_LIFETIME')769770 #: Enable this if you want to use the X-Sendfile feature. Keep in771 #: mind that the server has to support this. This only affects files772 #: sent with the :func:`send_file` method.773 #:774 #: .. versionadded:: 0.2775 use_x_sendfile = ConfigAttribute('use_x_sendfile')775 use_x_sendfile = ConfigAttribute('USE_X_SENDFILE')776777 #: the logging format used for the debug logger. This is only used when778 #: the application is in debug mode, otherwise the attached logging779 #: handler does the formatting.
13 unmodified lines
793 )794795 #: default configuration parameters796 default_config = ImmutableDict({797 'debug': False,798 'secret_key': None,799 'session_cookie_name': 'session',800 'permanent_session_lifetime': timedelta(days=31),801 'use_x_sendfile': False797 'DEBUG': False,798 'SECRET_KEY': None,799 'SESSION_COOKIE_NAME': 'session',800 'PERMANENT_SESSION_LIFETIME': timedelta(days=31),801 'USE_X_SENDFILE': False802 })803804 def __init__(self, import_name):805 _PackageBoundObject.__init__(self, import_name)
574 unmodified lines