Browse Source

Started working on config support

pull/1638/head
Armin Ronacher 15 years ago
parent
commit
68278fd78e
  1. 50
      flask.py

50
flask.py

@ -615,6 +615,21 @@ class Module(_PackageBoundObject):
self._register_events.append(func) self._register_events.append(func)
class ConfigAttribute(object):
"""Makes an attribute forward to the config"""
def __init__(self, name):
self.__name__ = name
def __get__(self, obj, type=None):
if obj is None:
return self
return obj.config[self.__name__]
def __set__(self, obj, value):
obj.config[self.__name__] = value
class Flask(_PackageBoundObject): class Flask(_PackageBoundObject):
"""The flask object implements a WSGI application and acts as the central """The flask object implements a WSGI application and acts as the central
object. It is passed the name of the module or package of the object. It is passed the name of the module or package of the
@ -648,25 +663,31 @@ class Flask(_PackageBoundObject):
#: and the development server will no longer serve any static files. #: and the development server will no longer serve any static files.
static_path = '/static' static_path = '/static'
#: the debug flag. Set this to `True` to enable debugging of the
#: application. In debug mode the debugger will kick in when an unhandled
#: exception ocurrs and the integrated server will automatically reload
#: the application if changes in the code are detected.
debug = ConfigAttribute('debug')
#: if a secret key is set, cryptographic components can use this to #: if a secret key is set, cryptographic components can use this to
#: sign cookies and other things. Set this to a complex random value #: sign cookies and other things. Set this to a complex random value
#: when you want to use the secure cookie for instance. #: when you want to use the secure cookie for instance.
secret_key = None secret_key = ConfigAttribute('secret_key')
#: The secure cookie uses this for the name of the session cookie #: The secure cookie uses this for the name of the session cookie
session_cookie_name = 'session' session_cookie_name = ConfigAttribute('session.cookie_name')
#: A :class:`~datetime.timedelta` which is used to set the expiration #: A :class:`~datetime.timedelta` which is used to set the expiration
#: date of a permanent session. The default is 31 days which makes a #: date of a permanent session. The default is 31 days which makes a
#: permanent session survive for roughly one month. #: permanent session survive for roughly one month.
permanent_session_lifetime = timedelta(days=31) permanent_session_lifetime = ConfigAttribute('session.permanent_lifetime')
#: Enable this if you want to use the X-Sendfile feature. Keep in #: Enable this if you want to use the X-Sendfile feature. Keep in
#: mind that the server has to support this. This only affects files #: mind that the server has to support this. This only affects files
#: sent with the :func:`send_file` method. #: sent with the :func:`send_file` method.
#: #:
#: .. versionadded:: 0.2 #: .. versionadded:: 0.2
use_x_sendfile = False use_x_sendfile = ConfigAttribute('use_x_sendfile')
#: the logging format used for the debug logger. This is only used when #: the logging format used for the debug logger. This is only used when
#: the application is in debug mode, otherwise the attached logging #: the application is in debug mode, otherwise the attached logging
@ -686,15 +707,22 @@ class Flask(_PackageBoundObject):
extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_'] extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_']
) )
def __init__(self, import_name): #: default configuration parameters
default_config = ImmutableDict({
'debug': False,
'secret_key': None,
'session.cookie_name': 'session',
'session.permanent_lifetime': timedelta(days=31),
'use_x_sendfile': False
})
def __init__(self, import_name, config=None):
_PackageBoundObject.__init__(self, import_name) _PackageBoundObject.__init__(self, import_name)
#: the debug flag. Set this to `True` to enable debugging of #: the configuration dictionary
#: the application. In debug mode the debugger will kick in self.config = self.default_config.copy()
#: when an unhandled exception ocurrs and the integrated server if config:
#: will automatically reload the application if changes in the self.config.update(config)
#: code are detected.
self.debug = False
#: a dictionary of all view functions registered. The keys will #: a dictionary of all view functions registered. The keys will
#: be function names which are also used to generate URLs and #: be function names which are also used to generate URLs and

Loading…
Cancel
Save